blob: 12d88c7bb8aa121ec37950a8fa794903077ffc4e [file] [log] [blame]
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +02001#include "report.hpp"
2
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01003#include "messages/collect_trigger_id.hpp"
4#include "messages/trigger_presence_changed_ind.hpp"
5#include "messages/update_report_ind.hpp"
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +02006#include "report_manager.hpp"
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +01007#include "utils/clock.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +01008#include "utils/contains.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01009#include "utils/transform.hpp"
10
11#include <phosphor-logging/log.hpp>
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +020012#include <sdbusplus/vtable.hpp>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020013
Szymon Dompke3eb56862021-09-20 15:32:04 +020014#include <limits>
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020015#include <numeric>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020016
17Report::Report(boost::asio::io_context& ioc,
18 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010019 const std::string& reportId, const std::string& reportName,
Szymon Dompke3eb56862021-09-20 15:32:04 +020020 const ReportingType reportingTypeIn,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010021 std::vector<ReportAction> reportActionsIn,
Szymon Dompke3eb56862021-09-20 15:32:04 +020022 const Milliseconds intervalIn, const uint64_t appendLimitIn,
23 const ReportUpdates reportUpdatesIn,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020024 interfaces::ReportManager& reportManager,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010025 interfaces::JsonStorage& reportStorageIn,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020026 std::vector<std::shared_ptr<interfaces::Metric>> metricsIn,
Szymon Dompkefdb06a12022-02-11 11:04:44 +010027 const interfaces::ReportFactory& reportFactory,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010028 const bool enabledIn, std::unique_ptr<interfaces::Clock> clock) :
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010029 id(reportId),
30 name(reportName), reportingType(reportingTypeIn), interval(intervalIn),
Szymon Dompkefdb06a12022-02-11 11:04:44 +010031 reportActions(reportActionsIn.begin(), reportActionsIn.end()),
Szymon Dompke3eb56862021-09-20 15:32:04 +020032 sensorCount(getSensorCount(metricsIn)),
33 appendLimit(deduceAppendLimit(appendLimitIn)),
34 reportUpdates(reportUpdatesIn),
35 readingsBuffer(deduceBufferSize(reportUpdates, reportingType)),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000036 objServer(objServer), metrics(std::move(metricsIn)), timer(ioc),
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010037 triggerIds(collectTriggerIds(ioc)), reportStorage(reportStorageIn),
38 enabled(enabledIn), clock(std::move(clock)), messanger(ioc)
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020039{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000040 readingParameters =
41 toReadingParameters(utils::transform(metrics, [](const auto& metric) {
42 return metric->dumpConfiguration();
43 }));
44
45 readingParametersPastVersion =
46 utils::transform(readingParameters, [](const auto& item) {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010047 const auto& [sensorData, operationType, id, collectionTimeScope,
48 collectionDuration] = item;
49
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000050 return ReadingParametersPastVersion::value_type(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010051 std::get<0>(sensorData.front()), operationType, id,
52 std::get<1>(sensorData.front()));
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000053 });
54
Szymon Dompkefdb06a12022-02-11 11:04:44 +010055 reportActions.insert(ReportAction::logToMetricReportsCollection);
56
Wludzik, Jozefe2362792020-10-27 17:23:55 +010057 deleteIface = objServer->add_unique_interface(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010058 getPath(), deleteIfaceName,
59 [this, &ioc, &reportManager](auto& dbusIface) {
Wludzik, Jozefe2362792020-10-27 17:23:55 +010060 dbusIface.register_method("Delete", [this, &ioc, &reportManager] {
61 if (persistency)
62 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010063 reportStorage.remove(fileName());
Wludzik, Jozefe2362792020-10-27 17:23:55 +010064 }
65 boost::asio::post(ioc, [this, &reportManager] {
66 reportManager.removeReport(this);
67 });
68 });
69 });
70
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +000071 persistency = storeConfiguration();
Szymon Dompkefdb06a12022-02-11 11:04:44 +010072 reportIface = makeReportInterface(reportFactory);
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020073
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010074 if (reportingType == ReportingType::periodic)
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020075 {
76 scheduleTimer(interval);
77 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000078
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020079 if (enabled)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000080 {
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020081 for (auto& metric : this->metrics)
82 {
83 metric->initialize();
84 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000085 }
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010086
87 messanger.on_receive<messages::TriggerPresenceChangedInd>(
88 [this](const auto& msg) {
89 const auto oldSize = triggerIds.size();
90
91 if (msg.presence == messages::Presence::Exist)
92 {
93 if (utils::contains(msg.reportIds, id))
94 {
95 triggerIds.insert(msg.triggerId);
96 }
97 else if (!utils::contains(msg.reportIds, id))
98 {
99 triggerIds.erase(msg.triggerId);
100 }
101 }
102 else if (msg.presence == messages::Presence::Removed)
103 {
104 triggerIds.erase(msg.triggerId);
105 }
106
107 if (triggerIds.size() != oldSize)
108 {
109 reportIface->signal_property("TriggerIds");
110 }
111 });
112
113 messanger.on_receive<messages::UpdateReportInd>([this](const auto& msg) {
114 if (utils::contains(msg.reportIds, id))
115 {
116 updateReadings();
117 }
118 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200119}
120
Szymon Dompke3eb56862021-09-20 15:32:04 +0200121uint64_t Report::getSensorCount(
122 std::vector<std::shared_ptr<interfaces::Metric>>& metrics)
123{
124 uint64_t sensorCount = 0;
125 for (auto& metric : metrics)
126 {
127 sensorCount += metric->sensorCount();
128 }
129 return sensorCount;
130}
131
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100132std::optional<uint64_t>
133 Report::deduceAppendLimit(const uint64_t appendLimitIn) const
Szymon Dompke3eb56862021-09-20 15:32:04 +0200134{
135 if (appendLimitIn == std::numeric_limits<uint64_t>::max())
136 {
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100137 return std::nullopt;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200138 }
139 else
140 {
141 return appendLimitIn;
142 }
143}
144
145uint64_t Report::deduceBufferSize(const ReportUpdates reportUpdatesIn,
146 const ReportingType reportingTypeIn) const
147{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100148 if (reportUpdatesIn == ReportUpdates::overwrite ||
149 reportingTypeIn == ReportingType::onRequest)
Szymon Dompke3eb56862021-09-20 15:32:04 +0200150 {
151 return sensorCount;
152 }
153 else
154 {
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100155 return appendLimit.value_or(sensorCount);
Szymon Dompke3eb56862021-09-20 15:32:04 +0200156 }
157}
158
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100159void Report::setReadingBuffer(const ReportUpdates newReportUpdates)
160{
161 if (reportingType != ReportingType::onRequest &&
162 (reportUpdates == ReportUpdates::overwrite ||
163 newReportUpdates == ReportUpdates::overwrite))
164 {
165 readingsBuffer.clearAndResize(
166 deduceBufferSize(newReportUpdates, reportingType));
167 }
168}
169
Szymon Dompke3eb56862021-09-20 15:32:04 +0200170void Report::setReportUpdates(const ReportUpdates newReportUpdates)
171{
172 if (reportUpdates != newReportUpdates)
173 {
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100174 setReadingBuffer(newReportUpdates);
Szymon Dompke3eb56862021-09-20 15:32:04 +0200175 reportUpdates = newReportUpdates;
176 }
177}
178
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100179std::unique_ptr<sdbusplus::asio::dbus_interface>
180 Report::makeReportInterface(const interfaces::ReportFactory& reportFactory)
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000181{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100182 auto dbusIface =
183 objServer->add_unique_interface(getPath(), reportIfaceName);
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000184 dbusIface->register_property_rw(
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200185 "Enabled", enabled, sdbusplus::vtable::property_::emits_change,
186 [this](bool newVal, const auto&) {
187 if (newVal != enabled)
188 {
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100189 if (true == newVal && ReportingType::periodic == reportingType)
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200190 {
191 scheduleTimer(interval);
192 }
193 if (newVal)
194 {
195 for (auto& metric : metrics)
196 {
197 metric->initialize();
198 }
199 }
200 else
201 {
202 for (auto& metric : metrics)
203 {
204 metric->deinitialize();
205 }
206 }
207
208 enabled = newVal;
209 persistency = storeConfiguration();
210 }
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +0100211 return 1;
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200212 },
213 [this](const auto&) { return enabled; });
214 dbusIface->register_property_rw(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000215 "Interval", interval.count(),
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000216 sdbusplus::vtable::property_::emits_change,
217 [this](uint64_t newVal, auto&) {
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200218 if (Milliseconds newValT{newVal};
219 newValT >= ReportManager::minInterval)
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000220 {
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200221 if (newValT != interval)
222 {
223 interval = newValT;
224 persistency = storeConfiguration();
225 }
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +0100226 return 1;
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000227 }
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +0100228 throw sdbusplus::exception::SdBusError(
229 static_cast<int>(std::errc::invalid_argument),
230 "Invalid interval");
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000231 },
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000232 [this](const auto&) { return interval.count(); });
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000233 dbusIface->register_property_rw(
234 "Persistency", persistency, sdbusplus::vtable::property_::emits_change,
235 [this](bool newVal, const auto&) {
236 if (newVal == persistency)
237 {
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +0100238 return 1;
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000239 }
240 if (newVal)
241 {
242 persistency = storeConfiguration();
243 }
244 else
245 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100246 reportStorage.remove(fileName());
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000247 persistency = false;
248 }
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +0100249 return 1;
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000250 },
251 [this](const auto&) { return persistency; });
252
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100253 dbusIface->register_property_r("Readings", readings,
254 sdbusplus::vtable::property_::emits_change,
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000255 [this](const auto&) { return readings; });
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100256 dbusIface->register_property_rw(
257 "ReportingType", std::string(),
258 sdbusplus::vtable::property_::emits_change,
259 [this](auto newVal, auto& oldVal) {
260 ReportingType tmp = utils::toReportingType(newVal);
261 if (tmp != reportingType)
262 {
263 if (tmp == ReportingType::onChange)
264 {
265 throw sdbusplus::exception::SdBusError(
266 static_cast<int>(std::errc::invalid_argument),
267 "Invalid reportingType");
268 }
269 if (tmp == ReportingType::periodic)
270 {
271 if (interval < ReportManager::minInterval)
272 {
273 throw sdbusplus::exception::SdBusError(
274 static_cast<int>(std::errc::invalid_argument),
275 "Invalid interval");
276 }
277 if (enabled == true)
278 {
279 scheduleTimer(interval);
280 }
281 }
282 else
283 {
284 timer.cancel();
285 }
286 reportingType = tmp;
287 setReadingBuffer(reportUpdates);
288 persistency = storeConfiguration();
289 }
290 oldVal = std::move(newVal);
291 return 1;
292 },
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100293 [this](const auto&) { return utils::enumToString(reportingType); });
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000294 dbusIface->register_property_r(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000295 "ReadingParameters", readingParametersPastVersion,
296 sdbusplus::vtable::property_::const_,
297 [this](const auto&) { return readingParametersPastVersion; });
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100298 dbusIface->register_property_rw(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000299 "ReadingParametersFutureVersion", readingParameters,
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100300 sdbusplus::vtable::property_::emits_change,
301 [this, &reportFactory](auto newVal, auto& oldVal) {
302 reportFactory.updateMetrics(metrics, enabled, newVal);
303 readingParameters = toReadingParameters(
304 utils::transform(metrics, [](const auto& metric) {
305 return metric->dumpConfiguration();
306 }));
307 persistency = storeConfiguration();
308 oldVal = std::move(newVal);
309 return 1;
310 },
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000311 [this](const auto&) { return readingParameters; });
312 dbusIface->register_property_r(
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100313 "EmitsReadingsUpdate", bool{}, sdbusplus::vtable::property_::none,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100314 [this](const auto&) {
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100315 return reportActions.contains(ReportAction::emitsReadingsUpdate);
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100316 });
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100317 dbusIface->register_property_r("Name", std::string{},
318 sdbusplus::vtable::property_::const_,
319 [this](const auto&) { return name; });
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000320 dbusIface->register_property_r(
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100321 "LogToMetricReportsCollection", bool{},
322 sdbusplus::vtable::property_::const_, [this](const auto&) {
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100323 return reportActions.contains(
324 ReportAction::logToMetricReportsCollection);
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100325 });
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100326 dbusIface->register_property_rw(
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100327 "ReportActions", std::vector<std::string>{},
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100328 sdbusplus::vtable::property_::emits_change,
329 [this](auto newVal, auto& oldVal) {
330 auto tmp = utils::transform<std::unordered_set>(
331 newVal, [](const auto& reportAction) {
332 return utils::toReportAction(reportAction);
333 });
334 tmp.insert(ReportAction::logToMetricReportsCollection);
335
336 if (tmp != reportActions)
337 {
338 reportActions = tmp;
339 persistency = storeConfiguration();
340 oldVal = std::move(newVal);
341 }
342 return 1;
343 },
344 [this](const auto&) {
345 return utils::transform<std::vector>(
346 reportActions, [](const auto reportAction) {
347 return utils::enumToString(reportAction);
348 });
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100349 });
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100350 dbusIface->register_property_r(
351 "AppendLimit", appendLimit.value_or(sensorCount),
352 sdbusplus::vtable::property_::emits_change,
353 [this](const auto&) { return appendLimit.value_or(sensorCount); });
Szymon Dompke3eb56862021-09-20 15:32:04 +0200354 dbusIface->register_property_rw(
355 "ReportUpdates", std::string(),
356 sdbusplus::vtable::property_::emits_change,
357 [this](auto newVal, auto& oldVal) {
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100358 setReportUpdates(utils::toReportUpdates(newVal));
Szymon Dompke3eb56862021-09-20 15:32:04 +0200359 oldVal = newVal;
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +0100360 return 1;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200361 },
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100362 [this](const auto&) { return utils::enumToString(reportUpdates); });
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100363 dbusIface->register_property_r(
364 "TriggerIds", std::vector<std::string>{},
365 sdbusplus::vtable::property_::emits_change, [this](const auto&) {
366 return std::vector<std::string>(triggerIds.begin(),
367 triggerIds.end());
368 });
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000369 dbusIface->register_method("Update", [this] {
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100370 if (reportingType == ReportingType::onRequest)
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000371 {
372 updateReadings();
373 }
374 });
375 constexpr bool skipPropertiesChangedSignal = true;
376 dbusIface->initialize(skipPropertiesChangedSignal);
377 return dbusIface;
378}
379
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200380void Report::timerProc(boost::system::error_code ec, Report& self)
381{
382 if (ec)
383 {
384 return;
385 }
386
387 self.updateReadings();
388 self.scheduleTimer(self.interval);
389}
390
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000391void Report::scheduleTimer(Milliseconds timerInterval)
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200392{
393 timer.expires_after(timerInterval);
394 timer.async_wait(
395 [this](boost::system::error_code ec) { timerProc(ec, *this); });
396}
397
398void Report::updateReadings()
399{
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200400 if (!enabled)
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000401 {
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200402 return;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000403 }
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200404
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100405 if (reportUpdates == ReportUpdates::overwrite ||
406 reportingType == ReportingType::onRequest)
Szymon Dompke3eb56862021-09-20 15:32:04 +0200407 {
408 readingsBuffer.clear();
409 }
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000410
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200411 for (const auto& metric : metrics)
412 {
Szymon Dompke3eb56862021-09-20 15:32:04 +0200413 for (const auto& [id, metadata, value, timestamp] :
414 metric->getReadings())
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200415 {
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100416 if (reportUpdates == ReportUpdates::appendStopsWhenFull &&
Szymon Dompke3eb56862021-09-20 15:32:04 +0200417 readingsBuffer.isFull())
418 {
419 enabled = false;
Krzysztof Grobelnyfbeb5bf2022-01-03 09:41:29 +0100420 for (auto& m : metrics)
Szymon Dompke3eb56862021-09-20 15:32:04 +0200421 {
Krzysztof Grobelnyfbeb5bf2022-01-03 09:41:29 +0100422 m->deinitialize();
Szymon Dompke3eb56862021-09-20 15:32:04 +0200423 }
424 break;
425 }
426 readingsBuffer.emplace(id, metadata, value, timestamp);
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200427 }
428 }
Szymon Dompke3eb56862021-09-20 15:32:04 +0200429
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100430 readings = {
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100431 std::chrono::duration_cast<Milliseconds>(clock->systemTimestamp())
432 .count(),
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100433 std::vector<ReadingData>(readingsBuffer.begin(), readingsBuffer.end())};
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100434
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100435 if (utils::contains(reportActions, ReportAction::emitsReadingsUpdate))
436 {
437 reportIface->signal_property("Readings");
438 }
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200439}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100440
441bool Report::storeConfiguration() const
442{
443 try
444 {
445 nlohmann::json data;
446
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200447 data["Enabled"] = enabled;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100448 data["Version"] = reportVersion;
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100449 data["Id"] = id;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100450 data["Name"] = name;
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100451 data["ReportingType"] = utils::toUnderlying(reportingType);
452 data["ReportActions"] =
453 utils::transform(reportActions, [](const auto reportAction) {
454 return utils::toUnderlying(reportAction);
455 });
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100456 data["Interval"] = interval.count();
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100457 data["AppendLimit"] =
458 appendLimit.value_or(std::numeric_limits<uint64_t>::max());
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100459 data["ReportUpdates"] = utils::toUnderlying(reportUpdates);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000460 data["ReadingParameters"] =
461 utils::transform(metrics, [](const auto& metric) {
462 return metric->dumpConfiguration();
463 });
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100464
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100465 reportStorage.store(fileName(), data);
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100466 }
467 catch (const std::exception& e)
468 {
469 phosphor::logging::log<phosphor::logging::level::ERR>(
470 "Failed to store a report in storage",
Wludzik, Jozef982c5b52021-01-02 12:05:21 +0100471 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100472 return false;
473 }
474
475 return true;
476}
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100477
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100478interfaces::JsonStorage::FilePath Report::fileName() const
479{
480 return interfaces::JsonStorage::FilePath{
481 std::to_string(std::hash<std::string>{}(id))};
482}
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100483
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100484std::unordered_set<std::string>
485 Report::collectTriggerIds(boost::asio::io_context& ioc) const
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100486{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100487 utils::Messanger tmp(ioc);
488
489 auto result = std::unordered_set<std::string>();
490
491 tmp.on_receive<messages::CollectTriggerIdResp>(
492 [&result](const auto& msg) { result.insert(msg.triggerId); });
493
494 tmp.send(messages::CollectTriggerIdReq{id});
495
496 return result;
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100497}