This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonReportGenerator.cpp
199 lines (161 loc) · 6.89 KB
/
JsonReportGenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2023 Stephen Foulds
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "JsonReportGenerator.h"
#include <utility>
JsonReportGenerator::JsonReportGenerator(std::shared_ptr<Metadata> metadata,
std::optional<std::shared_ptr<GroupManager>> groupManager)
: mMetadata(std::move(metadata)), mGroupManager(std::move(groupManager)), mJson()
{
mJson["processes"] = nlohmann::json::array();
mJson["metadata"] = {};
mJson["grandTotal"]["linuxUsage"] = 0.0;
mJson["grandTotal"]["calculatedUsage"] = 0.0;
}
void JsonReportGenerator::addDataset(const std::string &name, const std::vector<dataItems> &data)
{
if (data.empty()) {
// no data, so no-op
return;
}
nlohmann::ordered_json dataSet;
dataSet["name"] = name;
dataSet["data"] = nlohmann::json::array();
dataSet["_columnOrder"] = nlohmann::json::array();
bool setColumnOrder = false;
// Inja uses the unordered JSON format, and changing to ordered_json breaks things, see
// https://github.com/pantor/inja/pull/214. To work around this, add an array that defines the order
// of the columns. The _columnOrder array will also be responsible for generating the table headings
for (const auto& item : data) {
nlohmann::ordered_json tmp;
for (const auto& value : item) {
std::visit(overload{
[&](const std::pair<std::string, std::string> &v)
{
tmp[v.first] = v.second;
if (!setColumnOrder) {
dataSet["_columnOrder"].emplace_back(v.first);
}
},
[&](const Measurement &v)
{
tmp[v.GetName()]["Min"] = v.GetMinRounded();
tmp[v.GetName()]["Max"] = v.GetMaxRounded();
tmp[v.GetName()]["Average"] = v.GetAverageRounded();
if (!setColumnOrder) {
dataSet["_columnOrder"].emplace_back(v.GetName() + " (Min)");
dataSet["_columnOrder"].emplace_back(v.GetName() + " (Max)");
dataSet["_columnOrder"].emplace_back(v.GetName() + " (Average)");
}
}
}, value);
}
dataSet["data"].emplace_back(tmp);
setColumnOrder = true;
}
mJson["data"].emplace_back(dataSet);
}
nlohmann::ordered_json JsonReportGenerator::getJson()
{
mJson["metadata"] = {
{"image", mMetadata->Image()},
{"platform", mMetadata->Platform()},
{"mac", mMetadata->Mac()},
{"timestamp", mMetadata->ReportTimestamp()},
{"duration", mMetadata->Duration()},
{"swapEnabled", mMetadata->SwapEnabled()}
};
return mJson;
}
void JsonReportGenerator::addProcesses(std::vector<processMeasurement> &processes)
{
// Sort by PSS desc
std::sort(processes.begin(), processes.end(), [](const processMeasurement &a, const processMeasurement &b)
{
return a.Pss.GetAverageRounded() > b.Pss.GetAverageRounded();
});
for (const auto &process: processes) {
nlohmann::json processJson;
processJson["pid"] = process.ProcessInfo.pid();
processJson["ppid"] = process.ProcessInfo.ppid();
processJson["name"] = process.ProcessInfo.name();
processJson["cmdline"] = process.ProcessInfo.cmdline();
processJson["systemdService"] = process.ProcessInfo.systemdService().has_value()
? process.ProcessInfo.systemdService().value() : "";
processJson["container"] = process.ProcessInfo.container().has_value() ? process.ProcessInfo.container().value()
: "";
if (mGroupManager.has_value()) {
processJson["group"] = process.ProcessInfo.group(mGroupManager.value()).has_value()
? process.ProcessInfo.group(
mGroupManager.value()).value() : "";
} else {
processJson["group"] = "";
}
processJson["rss"] = process.Rss.ToJson();
processJson["pss"] = process.Pss.ToJson();
processJson["uss"] = process.Uss.ToJson();
processJson["vss"] = process.Vss.ToJson();
processJson["swap"] = process.Swap.ToJson();
processJson["swapPss"] = process.SwapPss.ToJson();
processJson["swapZram"] = process.SwapZram.ToJson();
processJson["locked"] = process.Locked.ToJson();
mJson["processes"].emplace_back(processJson);
}
// Calculate PSS memory per group
if (mGroupManager.has_value()) {
std::map<std::string, long double> pssPerGroup;
mJson["pssByGroup"] = nlohmann::json::array();
for (const auto &process: processes) {
auto group = process.ProcessInfo.group(mGroupManager.value());
if (group.has_value()) {
pssPerGroup[group.value()] += process.Pss.GetAverage();
}
}
// Sort the map by PSS desc so the pie chart appears nicely
std::vector<std::pair<std::string, long double>> pairs;
pairs.reserve(pssPerGroup.size());
for (auto &itr: pssPerGroup) {
pairs.emplace_back(itr);
}
std::sort(pairs.begin(), pairs.end(),
[](std::pair<std::string, long double> &a, std::pair<std::string, long double> &b)
{
return a.second > b.second;
});
for (const auto &group: pairs) {
nlohmann::json tmp;
tmp["groupName"] = group.first;
tmp["pss"] = std::round((int) group.second);
mJson["pssByGroup"].emplace_back(tmp);
}
} else {
mJson["pssByGroup"] = nullptr;
}
}
void JsonReportGenerator::setAverageLinuxMemoryUsage(int valueKb)
{
// Store in MB
mJson["grandTotal"]["linuxUsage"] = valueKb / 1024.0L;
}
void JsonReportGenerator::addToAccumulatedMemoryUsage(long double valueKb)
{
// Store in MB
long double usage = mJson["grandTotal"]["calculatedUsage"];
usage += valueKb / 1024.0L;
mJson["grandTotal"]["calculatedUsage"] = usage;
}