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