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