blob: d3a9bb06e5b0f8b795ecd3fe8856b5f4c8829718 [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{
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010029 auto metrics = utils::transform(
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000030 labeledMetricParams,
31 [this](const LabeledMetricParameters& param)
32 -> std::shared_ptr<interfaces::Metric> {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000033 namespace ts = utils::tstring;
34
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000035 return std::make_shared<Metric>(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000036 getSensors(param.at_label<ts::SensorPath>()),
37 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000038 param.at_label<ts::CollectionTimeScope>(),
Krzysztof Grobelny80697712021-03-04 09:49:27 +000039 param.at_label<ts::CollectionDuration>(),
40 std::make_unique<Clock>());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000041 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020042
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010043 return std::make_unique<Report>(
44 bus->get_io_context(), objServer, id, name, reportingType,
45 reportActions, period, appendLimit, reportUpdates, reportManager,
46 reportStorage, std::move(metrics), *this, enabled,
47 std::make_unique<Clock>(), std::move(readings));
Szymon Dompkefdb06a12022-02-11 11:04:44 +010048}
49
50void ReportFactory::updateMetrics(
51 std::vector<std::shared_ptr<interfaces::Metric>>& metrics, bool enabled,
Szymon Dompke32305f12022-07-05 15:37:21 +020052 const std::vector<LabeledMetricParameters>& labeledMetricParams) const
Szymon Dompkefdb06a12022-02-11 11:04:44 +010053{
Szymon Dompkefdb06a12022-02-11 11:04:44 +010054 std::vector<std::shared_ptr<interfaces::Metric>> oldMetrics = metrics;
55 std::vector<std::shared_ptr<interfaces::Metric>> newMetrics;
56
57 for (const auto& labeledMetricParam : labeledMetricParams)
58 {
59 auto existing = std::find_if(oldMetrics.begin(), oldMetrics.end(),
60 [labeledMetricParam](auto metric) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050061 return labeledMetricParam == metric->dumpConfiguration();
62 });
Szymon Dompkefdb06a12022-02-11 11:04:44 +010063
64 if (existing != oldMetrics.end())
65 {
66 newMetrics.emplace_back(*existing);
67 oldMetrics.erase(existing);
68 continue;
69 }
70
71 namespace ts = utils::tstring;
72 newMetrics.emplace_back(std::make_shared<Metric>(
73 getSensors(labeledMetricParam.at_label<ts::SensorPath>()),
74 labeledMetricParam.at_label<ts::OperationType>(),
75 labeledMetricParam.at_label<ts::Id>(),
76 labeledMetricParam.at_label<ts::CollectionTimeScope>(),
77 labeledMetricParam.at_label<ts::CollectionDuration>(),
78 std::make_unique<Clock>()));
79
80 if (enabled)
81 {
82 newMetrics.back()->initialize();
83 }
84 }
85
86 if (enabled)
87 {
88 for (auto& metric : oldMetrics)
89 {
90 metric->deinitialize();
91 }
92 }
93
94 metrics = std::move(newMetrics);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020095}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010096
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000097Sensors ReportFactory::getSensors(
Szymon Dompke94f71c52021-12-10 07:16:33 +010098 const std::vector<LabeledSensorInfo>& sensorPaths) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010099{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000100 using namespace utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100101
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100102 return utils::transform(
103 sensorPaths,
Szymon Dompke94f71c52021-12-10 07:16:33 +0100104 [this](const LabeledSensorInfo& sensorPath)
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100105 -> std::shared_ptr<interfaces::Sensor> {
106 return sensorCache.makeSensor<Sensor>(
107 sensorPath.at_label<Service>(), sensorPath.at_label<Path>(),
108 sensorPath.at_label<Metadata>(), bus->get_io_context(), bus);
109 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100110}
111
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000112std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
113 boost::asio::yield_context& yield,
114 const ReadingParameters& metricParams) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100115{
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100116 if (metricParams.empty())
117 {
118 return {};
119 }
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200120
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100121 auto tree = utils::getSubTreeSensors(yield, bus);
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100122 return getMetricParamsFromSensorTree(metricParams, tree);
123}
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000124
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100125std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
126 const ReadingParameters& metricParams) const
127{
128 if (metricParams.empty())
129 {
130 return {};
131 }
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200132
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100133 auto tree = utils::getSubTreeSensors(bus);
134 return getMetricParamsFromSensorTree(metricParams, tree);
135}
136
137std::vector<LabeledMetricParameters>
138 ReportFactory::getMetricParamsFromSensorTree(
139 const ReadingParameters& metricParams,
140 const std::vector<utils::SensorTree>& tree) const
141{
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200142 try
143 {
144 return utils::transform(metricParams, [&tree](const auto& item) {
145 auto [sensorPaths, operationType, id, collectionTimeScope,
146 collectionDuration] = item;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000147
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200148 std::vector<LabeledSensorInfo> sensorParameters;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000149
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200150 for (const auto& [sensorPath, metadata] : sensorPaths)
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000151 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200152 auto it = std::find_if(tree.begin(), tree.end(),
153 [path = sensorPath](const auto& v) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500154 return v.first == path;
155 });
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200156
157 if (it != tree.end() && it->second.size() == 1)
158 {
159 const auto& [service, ifaces] = it->second.front();
160 sensorParameters.emplace_back(service, sensorPath,
161 metadata);
162 }
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000163 }
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200164
165 if (sensorParameters.size() != sensorPaths.size())
166 {
167 throw errors::InvalidArgument("ReadingParameters",
168 "Service not found.");
169 }
170
171 if (operationType.empty())
172 {
173 operationType = utils::enumToString(OperationType::avg);
174 }
175 else if (operationType == "SINGLE")
176 {
177 operationType = utils::enumToString(OperationType::avg);
178 collectionTimeScope =
179 utils::enumToString(CollectionTimeScope::point);
180 }
181
182 if (collectionTimeScope.empty())
183 {
184 collectionTimeScope =
185 utils::enumToString(CollectionTimeScope::point);
186 }
187
188 return LabeledMetricParameters(
189 std::move(sensorParameters),
190 utils::toOperationType(operationType), id,
191 utils::toCollectionTimeScope(collectionTimeScope),
192 CollectionDuration(Milliseconds(collectionDuration)));
193 });
194 }
195 catch (const errors::InvalidArgument& e)
196 {
197 if (e.propertyName == "ReadingParameters")
198 {
199 throw;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000200 }
201
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200202 using namespace std::literals::string_literals;
203 throw errors::InvalidArgument("ReadingParameters."s + e.propertyName);
204 }
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100205}