blob: 2d7cd405abe44210a19d6cea53830809e1da1400 [file] [log] [blame]
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02001#include "report_factory.hpp"
2
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01003#include "metric.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02004#include "report.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01005#include "sensor.hpp"
Krzysztof Grobelny80697712021-03-04 09:49:27 +00006#include "utils/clock.hpp"
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +00007#include "utils/conversion.hpp"
Wludzik, Jozef1477fe62021-01-02 11:56:10 +01008#include "utils/dbus_mapper.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01009#include "utils/transform.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020010
11ReportFactory::ReportFactory(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010012 std::shared_ptr<sdbusplus::asio::connection> bus,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010013 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
14 SensorCache& sensorCache) :
Wludzik, Jozefe2362792020-10-27 17:23:55 +010015 bus(std::move(bus)),
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010016 objServer(objServer), sensorCache(sensorCache)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020017{}
18
19std::unique_ptr<interfaces::Report> ReportFactory::make(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010020 const std::string& id, const std::string& name,
21 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010022 const std::vector<ReportAction>& reportActions, Milliseconds period,
23 uint64_t appendLimit, const ReportUpdates reportUpdates,
Szymon Dompke3eb56862021-09-20 15:32:04 +020024 interfaces::ReportManager& reportManager,
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000025 interfaces::JsonStorage& reportStorage,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010026 std::vector<LabeledMetricParameters> labeledMetricParams, bool enabled,
27 Readings readings) const
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020028{
Patrick Williamsc7935fa2023-10-20 11:19:30 -050029 auto metrics = utils::transform(labeledMetricParams,
30 [this](const LabeledMetricParameters& param)
31 -> std::shared_ptr<interfaces::Metric> {
32 namespace ts = utils::tstring;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000033
Patrick Williamsc7935fa2023-10-20 11:19:30 -050034 return std::make_shared<Metric>(
35 getSensors(param.at_label<ts::SensorPath>()),
36 param.at_label<ts::OperationType>(),
37 param.at_label<ts::CollectionTimeScope>(),
38 param.at_label<ts::CollectionDuration>(),
39 std::make_unique<Clock>());
40 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020041
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010042 return std::make_unique<Report>(
43 bus->get_io_context(), objServer, id, name, reportingType,
44 reportActions, period, appendLimit, reportUpdates, reportManager,
45 reportStorage, std::move(metrics), *this, enabled,
46 std::make_unique<Clock>(), std::move(readings));
Szymon Dompkefdb06a12022-02-11 11:04:44 +010047}
48
49void ReportFactory::updateMetrics(
50 std::vector<std::shared_ptr<interfaces::Metric>>& metrics, bool enabled,
Szymon Dompke32305f12022-07-05 15:37:21 +020051 const std::vector<LabeledMetricParameters>& labeledMetricParams) const
Szymon Dompkefdb06a12022-02-11 11:04:44 +010052{
Szymon Dompkefdb06a12022-02-11 11:04:44 +010053 std::vector<std::shared_ptr<interfaces::Metric>> oldMetrics = metrics;
54 std::vector<std::shared_ptr<interfaces::Metric>> newMetrics;
55
56 for (const auto& labeledMetricParam : labeledMetricParams)
57 {
58 auto existing = std::find_if(oldMetrics.begin(), oldMetrics.end(),
59 [labeledMetricParam](auto metric) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050060 return labeledMetricParam == metric->dumpConfiguration();
61 });
Szymon Dompkefdb06a12022-02-11 11:04:44 +010062
63 if (existing != oldMetrics.end())
64 {
65 newMetrics.emplace_back(*existing);
66 oldMetrics.erase(existing);
67 continue;
68 }
69
70 namespace ts = utils::tstring;
71 newMetrics.emplace_back(std::make_shared<Metric>(
72 getSensors(labeledMetricParam.at_label<ts::SensorPath>()),
73 labeledMetricParam.at_label<ts::OperationType>(),
Szymon Dompkefdb06a12022-02-11 11:04:44 +010074 labeledMetricParam.at_label<ts::CollectionTimeScope>(),
75 labeledMetricParam.at_label<ts::CollectionDuration>(),
76 std::make_unique<Clock>()));
77
78 if (enabled)
79 {
80 newMetrics.back()->initialize();
81 }
82 }
83
84 if (enabled)
85 {
86 for (auto& metric : oldMetrics)
87 {
88 metric->deinitialize();
89 }
90 }
91
92 metrics = std::move(newMetrics);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020093}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010094
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000095Sensors ReportFactory::getSensors(
Szymon Dompke94f71c52021-12-10 07:16:33 +010096 const std::vector<LabeledSensorInfo>& sensorPaths) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010097{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000098 using namespace utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010099
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500100 return utils::transform(sensorPaths,
101 [this](const LabeledSensorInfo& sensorPath)
102 -> std::shared_ptr<interfaces::Sensor> {
103 return sensorCache.makeSensor<Sensor>(
104 sensorPath.at_label<Service>(), sensorPath.at_label<Path>(),
105 sensorPath.at_label<Metadata>(), bus->get_io_context(), bus);
106 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100107}
108
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000109std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
110 boost::asio::yield_context& yield,
111 const ReadingParameters& metricParams) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100112{
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100113 if (metricParams.empty())
114 {
115 return {};
116 }
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200117
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100118 auto tree = utils::getSubTreeSensors(yield, bus);
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100119 return getMetricParamsFromSensorTree(metricParams, tree);
120}
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000121
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100122std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
123 const ReadingParameters& metricParams) const
124{
125 if (metricParams.empty())
126 {
127 return {};
128 }
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200129
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100130 auto tree = utils::getSubTreeSensors(bus);
131 return getMetricParamsFromSensorTree(metricParams, tree);
132}
133
134std::vector<LabeledMetricParameters>
135 ReportFactory::getMetricParamsFromSensorTree(
136 const ReadingParameters& metricParams,
137 const std::vector<utils::SensorTree>& tree) const
138{
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200139 try
140 {
141 return utils::transform(metricParams, [&tree](const auto& item) {
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000142 auto [sensorPaths, operationType, collectionTimeScope,
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200143 collectionDuration] = item;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000144
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200145 std::vector<LabeledSensorInfo> sensorParameters;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000146
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200147 for (const auto& [sensorPath, metadata] : sensorPaths)
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000148 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200149 auto it = std::find_if(tree.begin(), tree.end(),
150 [path = sensorPath](const auto& v) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500151 return v.first == path;
152 });
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200153
154 if (it != tree.end() && it->second.size() == 1)
155 {
156 const auto& [service, ifaces] = it->second.front();
157 sensorParameters.emplace_back(service, sensorPath,
158 metadata);
159 }
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000160 }
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200161
162 if (sensorParameters.size() != sensorPaths.size())
163 {
164 throw errors::InvalidArgument("ReadingParameters",
165 "Service not found.");
166 }
167
168 if (operationType.empty())
169 {
170 operationType = utils::enumToString(OperationType::avg);
171 }
172 else if (operationType == "SINGLE")
173 {
174 operationType = utils::enumToString(OperationType::avg);
175 collectionTimeScope =
176 utils::enumToString(CollectionTimeScope::point);
177 }
178
179 if (collectionTimeScope.empty())
180 {
181 collectionTimeScope =
182 utils::enumToString(CollectionTimeScope::point);
183 }
184
185 return LabeledMetricParameters(
186 std::move(sensorParameters),
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000187 utils::toOperationType(operationType),
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200188 utils::toCollectionTimeScope(collectionTimeScope),
189 CollectionDuration(Milliseconds(collectionDuration)));
190 });
191 }
192 catch (const errors::InvalidArgument& e)
193 {
194 if (e.propertyName == "ReadingParameters")
195 {
196 throw;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000197 }
198
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200199 using namespace std::literals::string_literals;
200 throw errors::InvalidArgument("ReadingParameters."s + e.propertyName);
201 }
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100202}