Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 1 | #include "report.hpp" |
| 2 | |
| 3 | #include "report_manager.hpp" |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 4 | #include "utils/clock.hpp" |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 5 | #include "utils/contains.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 6 | #include "utils/transform.hpp" |
| 7 | |
| 8 | #include <phosphor-logging/log.hpp> |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 9 | #include <sdbusplus/vtable.hpp> |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 10 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 11 | #include <limits> |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 12 | #include <numeric> |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 13 | |
| 14 | Report::Report(boost::asio::io_context& ioc, |
| 15 | const std::shared_ptr<sdbusplus::asio::object_server>& objServer, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 16 | const std::string& reportId, const std::string& reportName, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 17 | const ReportingType reportingTypeIn, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 18 | std::vector<ReportAction> reportActionsIn, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 19 | const Milliseconds intervalIn, const uint64_t appendLimitIn, |
| 20 | const ReportUpdates reportUpdatesIn, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 21 | interfaces::ReportManager& reportManager, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 22 | interfaces::JsonStorage& reportStorageIn, |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 23 | std::vector<std::shared_ptr<interfaces::Metric>> metricsIn, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 24 | const bool enabledIn, std::unique_ptr<interfaces::Clock> clock) : |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 25 | id(reportId), |
| 26 | name(reportName), reportingType(reportingTypeIn), interval(intervalIn), |
| 27 | reportActions(std::move(reportActionsIn)), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 28 | sensorCount(getSensorCount(metricsIn)), |
| 29 | appendLimit(deduceAppendLimit(appendLimitIn)), |
| 30 | reportUpdates(reportUpdatesIn), |
| 31 | readingsBuffer(deduceBufferSize(reportUpdates, reportingType)), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 32 | objServer(objServer), metrics(std::move(metricsIn)), timer(ioc), |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 33 | reportStorage(reportStorageIn), enabled(enabledIn), clock(std::move(clock)) |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 34 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 35 | readingParameters = |
| 36 | toReadingParameters(utils::transform(metrics, [](const auto& metric) { |
| 37 | return metric->dumpConfiguration(); |
| 38 | })); |
| 39 | |
| 40 | readingParametersPastVersion = |
| 41 | utils::transform(readingParameters, [](const auto& item) { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 42 | const auto& [sensorData, operationType, id, collectionTimeScope, |
| 43 | collectionDuration] = item; |
| 44 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 45 | return ReadingParametersPastVersion::value_type( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 46 | std::get<0>(sensorData.front()), operationType, id, |
| 47 | std::get<1>(sensorData.front())); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 48 | }); |
| 49 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 50 | deleteIface = objServer->add_unique_interface( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 51 | getPath(), deleteIfaceName, |
| 52 | [this, &ioc, &reportManager](auto& dbusIface) { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 53 | dbusIface.register_method("Delete", [this, &ioc, &reportManager] { |
| 54 | if (persistency) |
| 55 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 56 | reportStorage.remove(fileName()); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 57 | } |
| 58 | boost::asio::post(ioc, [this, &reportManager] { |
| 59 | reportManager.removeReport(this); |
| 60 | }); |
| 61 | }); |
| 62 | }); |
| 63 | |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 64 | persistency = storeConfiguration(); |
| 65 | reportIface = makeReportInterface(); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 66 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 67 | if (reportingType == ReportingType::periodic) |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 68 | { |
| 69 | scheduleTimer(interval); |
| 70 | } |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 71 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 72 | if (enabled) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 73 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 74 | for (auto& metric : this->metrics) |
| 75 | { |
| 76 | metric->initialize(); |
| 77 | } |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 78 | } |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 81 | uint64_t Report::getSensorCount( |
| 82 | std::vector<std::shared_ptr<interfaces::Metric>>& metrics) |
| 83 | { |
| 84 | uint64_t sensorCount = 0; |
| 85 | for (auto& metric : metrics) |
| 86 | { |
| 87 | sensorCount += metric->sensorCount(); |
| 88 | } |
| 89 | return sensorCount; |
| 90 | } |
| 91 | |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame^] | 92 | std::optional<uint64_t> |
| 93 | Report::deduceAppendLimit(const uint64_t appendLimitIn) const |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 94 | { |
| 95 | if (appendLimitIn == std::numeric_limits<uint64_t>::max()) |
| 96 | { |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame^] | 97 | return std::nullopt; |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 98 | } |
| 99 | else |
| 100 | { |
| 101 | return appendLimitIn; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | uint64_t Report::deduceBufferSize(const ReportUpdates reportUpdatesIn, |
| 106 | const ReportingType reportingTypeIn) const |
| 107 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 108 | if (reportUpdatesIn == ReportUpdates::overwrite || |
| 109 | reportingTypeIn == ReportingType::onRequest) |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 110 | { |
| 111 | return sensorCount; |
| 112 | } |
| 113 | else |
| 114 | { |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame^] | 115 | return appendLimit.value_or(sensorCount); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | void Report::setReportUpdates(const ReportUpdates newReportUpdates) |
| 120 | { |
| 121 | if (reportUpdates != newReportUpdates) |
| 122 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 123 | if (reportingType != ReportingType::onRequest && |
| 124 | (reportUpdates == ReportUpdates::overwrite || |
| 125 | newReportUpdates == ReportUpdates::overwrite)) |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 126 | { |
| 127 | readingsBuffer.clearAndResize( |
| 128 | deduceBufferSize(newReportUpdates, reportingType)); |
| 129 | } |
| 130 | reportUpdates = newReportUpdates; |
| 131 | } |
| 132 | } |
| 133 | |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 134 | std::unique_ptr<sdbusplus::asio::dbus_interface> Report::makeReportInterface() |
| 135 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 136 | auto dbusIface = |
| 137 | objServer->add_unique_interface(getPath(), reportIfaceName); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 138 | dbusIface->register_property_rw( |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 139 | "Enabled", enabled, sdbusplus::vtable::property_::emits_change, |
| 140 | [this](bool newVal, const auto&) { |
| 141 | if (newVal != enabled) |
| 142 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 143 | if (true == newVal && ReportingType::periodic == reportingType) |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 144 | { |
| 145 | scheduleTimer(interval); |
| 146 | } |
| 147 | if (newVal) |
| 148 | { |
| 149 | for (auto& metric : metrics) |
| 150 | { |
| 151 | metric->initialize(); |
| 152 | } |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | for (auto& metric : metrics) |
| 157 | { |
| 158 | metric->deinitialize(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | enabled = newVal; |
| 163 | persistency = storeConfiguration(); |
| 164 | } |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 165 | return 1; |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 166 | }, |
| 167 | [this](const auto&) { return enabled; }); |
| 168 | dbusIface->register_property_rw( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 169 | "Interval", interval.count(), |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 170 | sdbusplus::vtable::property_::emits_change, |
| 171 | [this](uint64_t newVal, auto&) { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 172 | if (Milliseconds newValT{newVal}; |
| 173 | newValT >= ReportManager::minInterval) |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 174 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 175 | if (newValT != interval) |
| 176 | { |
| 177 | interval = newValT; |
| 178 | persistency = storeConfiguration(); |
| 179 | } |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 180 | return 1; |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 181 | } |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 182 | throw sdbusplus::exception::SdBusError( |
| 183 | static_cast<int>(std::errc::invalid_argument), |
| 184 | "Invalid interval"); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 185 | }, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 186 | [this](const auto&) { return interval.count(); }); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 187 | dbusIface->register_property_rw( |
| 188 | "Persistency", persistency, sdbusplus::vtable::property_::emits_change, |
| 189 | [this](bool newVal, const auto&) { |
| 190 | if (newVal == persistency) |
| 191 | { |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 192 | return 1; |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 193 | } |
| 194 | if (newVal) |
| 195 | { |
| 196 | persistency = storeConfiguration(); |
| 197 | } |
| 198 | else |
| 199 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 200 | reportStorage.remove(fileName()); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 201 | persistency = false; |
| 202 | } |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 203 | return 1; |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 204 | }, |
| 205 | [this](const auto&) { return persistency; }); |
| 206 | |
| 207 | auto readingsFlag = sdbusplus::vtable::property_::none; |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 208 | if (utils::contains(reportActions, ReportAction::emitsReadingsUpdate)) |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 209 | { |
| 210 | readingsFlag = sdbusplus::vtable::property_::emits_change; |
| 211 | } |
| 212 | dbusIface->register_property_r("Readings", readings, readingsFlag, |
| 213 | [this](const auto&) { return readings; }); |
| 214 | dbusIface->register_property_r( |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 215 | "ReportingType", std::string(), sdbusplus::vtable::property_::const_, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 216 | [this](const auto&) { return utils::enumToString(reportingType); }); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 217 | dbusIface->register_property_r( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 218 | "ReadingParameters", readingParametersPastVersion, |
| 219 | sdbusplus::vtable::property_::const_, |
| 220 | [this](const auto&) { return readingParametersPastVersion; }); |
| 221 | dbusIface->register_property_r( |
| 222 | "ReadingParametersFutureVersion", readingParameters, |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 223 | sdbusplus::vtable::property_::const_, |
| 224 | [this](const auto&) { return readingParameters; }); |
| 225 | dbusIface->register_property_r( |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 226 | "EmitsReadingsUpdate", bool{}, sdbusplus::vtable::property_::const_, |
| 227 | [this](const auto&) { |
| 228 | return utils::contains(reportActions, |
| 229 | ReportAction::emitsReadingsUpdate); |
| 230 | }); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 231 | dbusIface->register_property_r("Name", std::string{}, |
| 232 | sdbusplus::vtable::property_::const_, |
| 233 | [this](const auto&) { return name; }); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 234 | dbusIface->register_property_r( |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 235 | "LogToMetricReportsCollection", bool{}, |
| 236 | sdbusplus::vtable::property_::const_, [this](const auto&) { |
| 237 | return utils::contains(reportActions, |
| 238 | ReportAction::logToMetricReportsCollection); |
| 239 | }); |
| 240 | dbusIface->register_property_r( |
| 241 | "ReportActions", std::vector<std::string>{}, |
| 242 | sdbusplus::vtable::property_::const_, [this](const auto&) { |
| 243 | return utils::transform(reportActions, [](const auto reportAction) { |
| 244 | return utils::enumToString(reportAction); |
| 245 | }); |
| 246 | }); |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame^] | 247 | dbusIface->register_property_r( |
| 248 | "AppendLimit", appendLimit.value_or(sensorCount), |
| 249 | sdbusplus::vtable::property_::emits_change, |
| 250 | [this](const auto&) { return appendLimit.value_or(sensorCount); }); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 251 | dbusIface->register_property_rw( |
| 252 | "ReportUpdates", std::string(), |
| 253 | sdbusplus::vtable::property_::emits_change, |
| 254 | [this](auto newVal, auto& oldVal) { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 255 | setReportUpdates(utils::toReportUpdates(newVal)); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 256 | oldVal = newVal; |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 257 | return 1; |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 258 | }, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 259 | [this](const auto&) { return utils::enumToString(reportUpdates); }); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 260 | dbusIface->register_method("Update", [this] { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 261 | if (reportingType == ReportingType::onRequest) |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 262 | { |
| 263 | updateReadings(); |
| 264 | } |
| 265 | }); |
| 266 | constexpr bool skipPropertiesChangedSignal = true; |
| 267 | dbusIface->initialize(skipPropertiesChangedSignal); |
| 268 | return dbusIface; |
| 269 | } |
| 270 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 271 | void Report::timerProc(boost::system::error_code ec, Report& self) |
| 272 | { |
| 273 | if (ec) |
| 274 | { |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | self.updateReadings(); |
| 279 | self.scheduleTimer(self.interval); |
| 280 | } |
| 281 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 282 | void Report::scheduleTimer(Milliseconds timerInterval) |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 283 | { |
| 284 | timer.expires_after(timerInterval); |
| 285 | timer.async_wait( |
| 286 | [this](boost::system::error_code ec) { timerProc(ec, *this); }); |
| 287 | } |
| 288 | |
| 289 | void Report::updateReadings() |
| 290 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 291 | if (!enabled) |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 292 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 293 | return; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 294 | } |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 295 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 296 | if (reportUpdates == ReportUpdates::overwrite || |
| 297 | reportingType == ReportingType::onRequest) |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 298 | { |
| 299 | readingsBuffer.clear(); |
| 300 | } |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 301 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 302 | for (const auto& metric : metrics) |
| 303 | { |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 304 | for (const auto& [id, metadata, value, timestamp] : |
| 305 | metric->getReadings()) |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 306 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 307 | if (reportUpdates == ReportUpdates::appendStopsWhenFull && |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 308 | readingsBuffer.isFull()) |
| 309 | { |
| 310 | enabled = false; |
Krzysztof Grobelny | fbeb5bf | 2022-01-03 09:41:29 +0100 | [diff] [blame] | 311 | for (auto& m : metrics) |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 312 | { |
Krzysztof Grobelny | fbeb5bf | 2022-01-03 09:41:29 +0100 | [diff] [blame] | 313 | m->deinitialize(); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 314 | } |
| 315 | break; |
| 316 | } |
| 317 | readingsBuffer.emplace(id, metadata, value, timestamp); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 318 | } |
| 319 | } |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 320 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 321 | readings = { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 322 | std::chrono::duration_cast<Milliseconds>(clock->systemTimestamp()) |
| 323 | .count(), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 324 | std::vector<ReadingData>(readingsBuffer.begin(), readingsBuffer.end())}; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 325 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 326 | reportIface->signal_property("Readings"); |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 327 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 328 | |
| 329 | bool Report::storeConfiguration() const |
| 330 | { |
| 331 | try |
| 332 | { |
| 333 | nlohmann::json data; |
| 334 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 335 | data["Enabled"] = enabled; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 336 | data["Version"] = reportVersion; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 337 | data["Id"] = id; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 338 | data["Name"] = name; |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 339 | data["ReportingType"] = utils::toUnderlying(reportingType); |
| 340 | data["ReportActions"] = |
| 341 | utils::transform(reportActions, [](const auto reportAction) { |
| 342 | return utils::toUnderlying(reportAction); |
| 343 | }); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 344 | data["Interval"] = interval.count(); |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame^] | 345 | data["AppendLimit"] = |
| 346 | appendLimit.value_or(std::numeric_limits<uint64_t>::max()); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 347 | data["ReportUpdates"] = utils::toUnderlying(reportUpdates); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 348 | data["ReadingParameters"] = |
| 349 | utils::transform(metrics, [](const auto& metric) { |
| 350 | return metric->dumpConfiguration(); |
| 351 | }); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 352 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 353 | reportStorage.store(fileName(), data); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 354 | } |
| 355 | catch (const std::exception& e) |
| 356 | { |
| 357 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 358 | "Failed to store a report in storage", |
Wludzik, Jozef | 982c5b5 | 2021-01-02 12:05:21 +0100 | [diff] [blame] | 359 | phosphor::logging::entry("EXCEPTION_MSG=%s", e.what())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 360 | return false; |
| 361 | } |
| 362 | |
| 363 | return true; |
| 364 | } |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 365 | interfaces::JsonStorage::FilePath Report::fileName() const |
| 366 | { |
| 367 | return interfaces::JsonStorage::FilePath{ |
| 368 | std::to_string(std::hash<std::string>{}(id))}; |
| 369 | } |