Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 1 | #include "report.hpp" |
| 2 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 3 | #include "messages/collect_trigger_id.hpp" |
| 4 | #include "messages/trigger_presence_changed_ind.hpp" |
| 5 | #include "messages/update_report_ind.hpp" |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 6 | #include "report_manager.hpp" |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 7 | #include "utils/clock.hpp" |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 8 | #include "utils/contains.hpp" |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 9 | #include "utils/dbus_path_utils.hpp" |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 10 | #include "utils/ensure.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 11 | #include "utils/transform.hpp" |
| 12 | |
| 13 | #include <phosphor-logging/log.hpp> |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 14 | #include <sdbusplus/vtable.hpp> |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 15 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 16 | #include <limits> |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 17 | #include <numeric> |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 18 | #include <optional> |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 19 | |
| 20 | Report::Report(boost::asio::io_context& ioc, |
| 21 | const std::shared_ptr<sdbusplus::asio::object_server>& objServer, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 22 | const std::string& reportId, const std::string& reportName, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 23 | const ReportingType reportingTypeIn, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 24 | std::vector<ReportAction> reportActionsIn, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 25 | const Milliseconds intervalIn, const uint64_t appendLimitIn, |
| 26 | const ReportUpdates reportUpdatesIn, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 27 | interfaces::ReportManager& reportManager, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 28 | interfaces::JsonStorage& reportStorageIn, |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 29 | std::vector<std::shared_ptr<interfaces::Metric>> metricsIn, |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 30 | const interfaces::ReportFactory& reportFactory, |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 31 | const bool enabledIn, std::unique_ptr<interfaces::Clock> clock, |
| 32 | Readings readingsIn) : |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 33 | id(reportId), |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 34 | path(utils::pathAppend(utils::constants::reportDirPath, id)), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 35 | name(reportName), reportingType(reportingTypeIn), interval(intervalIn), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 36 | reportActions(reportActionsIn.begin(), reportActionsIn.end()), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 37 | sensorCount(getSensorCount(metricsIn)), |
| 38 | appendLimit(deduceAppendLimit(appendLimitIn)), |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 39 | reportUpdates(reportUpdatesIn), readings(std::move(readingsIn)), |
| 40 | readingsBuffer(std::get<1>(readings), |
| 41 | deduceBufferSize(reportUpdates, reportingType)), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 42 | objServer(objServer), metrics(std::move(metricsIn)), timer(ioc), |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 43 | triggerIds(collectTriggerIds(ioc)), reportStorage(reportStorageIn), |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 44 | clock(std::move(clock)), messanger(ioc) |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 45 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 46 | readingParameters = |
| 47 | toReadingParameters(utils::transform(metrics, [](const auto& metric) { |
| 48 | return metric->dumpConfiguration(); |
| 49 | })); |
| 50 | |
| 51 | readingParametersPastVersion = |
| 52 | utils::transform(readingParameters, [](const auto& item) { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 53 | const auto& [sensorData, operationType, id, collectionTimeScope, |
| 54 | collectionDuration] = item; |
| 55 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 56 | return ReadingParametersPastVersion::value_type( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 57 | std::get<0>(sensorData.front()), operationType, id, |
| 58 | std::get<1>(sensorData.front())); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 59 | }); |
| 60 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 61 | reportActions.insert(ReportAction::logToMetricReportsCollection); |
| 62 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 63 | deleteIface = objServer->add_unique_interface( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 64 | getPath(), deleteIfaceName, |
| 65 | [this, &ioc, &reportManager](auto& dbusIface) { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 66 | dbusIface.register_method("Delete", [this, &ioc, &reportManager] { |
| 67 | if (persistency) |
| 68 | { |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 69 | persistency = false; |
| 70 | |
| 71 | reportIface->signal_property("Persistency"); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 72 | } |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 73 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 74 | boost::asio::post(ioc, [this, &reportManager] { |
| 75 | reportManager.removeReport(this); |
| 76 | }); |
| 77 | }); |
| 78 | }); |
| 79 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 80 | errorMessages = verify(); |
| 81 | state.set<ReportFlags::enabled, ReportFlags::valid>(enabledIn, |
| 82 | errorMessages.empty()); |
| 83 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 84 | reportIface = makeReportInterface(reportFactory); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 85 | persistency = storeConfiguration(); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 86 | |
| 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 | { |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 109 | reportIface->signal_property("Triggers"); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 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 Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 121 | Report::~Report() |
| 122 | { |
| 123 | if (persistency) |
| 124 | { |
| 125 | if (shouldStoreMetricValues()) |
| 126 | { |
| 127 | storeConfiguration(); |
| 128 | } |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | reportStorage.remove(reportFileName()); |
| 133 | } |
| 134 | } |
| 135 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 136 | void Report::activate() |
| 137 | { |
| 138 | for (auto& metric : this->metrics) |
| 139 | { |
| 140 | metric->initialize(); |
| 141 | } |
| 142 | |
| 143 | scheduleTimer(); |
| 144 | |
| 145 | if (reportIface) |
| 146 | { |
| 147 | reportIface->signal_property("errors"); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void Report::deactivate() |
| 152 | { |
| 153 | for (auto& metric : metrics) |
| 154 | { |
| 155 | metric->deinitialize(); |
| 156 | } |
| 157 | |
| 158 | unregisterFromMetrics = nullptr; |
| 159 | timer.cancel(); |
| 160 | |
| 161 | if (reportIface) |
| 162 | { |
| 163 | reportIface->signal_property("Errors"); |
| 164 | } |
| 165 | } |
| 166 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 167 | uint64_t Report::getSensorCount( |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 168 | const std::vector<std::shared_ptr<interfaces::Metric>>& metrics) |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 169 | { |
| 170 | uint64_t sensorCount = 0; |
| 171 | for (auto& metric : metrics) |
| 172 | { |
| 173 | sensorCount += metric->sensorCount(); |
| 174 | } |
| 175 | return sensorCount; |
| 176 | } |
| 177 | |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame] | 178 | std::optional<uint64_t> |
| 179 | Report::deduceAppendLimit(const uint64_t appendLimitIn) const |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 180 | { |
| 181 | if (appendLimitIn == std::numeric_limits<uint64_t>::max()) |
| 182 | { |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame] | 183 | return std::nullopt; |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 184 | } |
| 185 | else |
| 186 | { |
| 187 | return appendLimitIn; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | uint64_t Report::deduceBufferSize(const ReportUpdates reportUpdatesIn, |
| 192 | const ReportingType reportingTypeIn) const |
| 193 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 194 | if (reportUpdatesIn == ReportUpdates::overwrite || |
| 195 | reportingTypeIn == ReportingType::onRequest) |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 196 | { |
| 197 | return sensorCount; |
| 198 | } |
| 199 | else |
| 200 | { |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame] | 201 | return appendLimit.value_or(sensorCount); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 205 | void Report::setReadingBuffer(const ReportUpdates newReportUpdates) |
| 206 | { |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame^] | 207 | const auto newBufferSize = |
| 208 | deduceBufferSize(newReportUpdates, reportingType); |
| 209 | if (readingsBuffer.size() != newBufferSize) |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 210 | { |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame^] | 211 | readingsBuffer.clearAndResize(newBufferSize); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 215 | void Report::setReportUpdates(const ReportUpdates newReportUpdates) |
| 216 | { |
| 217 | if (reportUpdates != newReportUpdates) |
| 218 | { |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 219 | setReadingBuffer(newReportUpdates); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 220 | reportUpdates = newReportUpdates; |
| 221 | } |
| 222 | } |
| 223 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 224 | std::unique_ptr<sdbusplus::asio::dbus_interface> |
| 225 | Report::makeReportInterface(const interfaces::ReportFactory& reportFactory) |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 226 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 227 | auto dbusIface = |
| 228 | objServer->add_unique_interface(getPath(), reportIfaceName); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 229 | dbusIface->register_property_rw<bool>( |
| 230 | "Enabled", sdbusplus::vtable::property_::emits_change, |
| 231 | [this](bool newVal, auto& oldValue) { |
| 232 | if (newVal != state.get<ReportFlags::enabled>()) |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 233 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 234 | state.set<ReportFlags::enabled>(oldValue = newVal); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 235 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 236 | persistency = storeConfiguration(); |
| 237 | } |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 238 | return 1; |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 239 | }, |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 240 | [this](const auto&) { return state.get<ReportFlags::enabled>(); }); |
| 241 | dbusIface->register_property_r< |
| 242 | std::vector<std::tuple<std::string, std::string>>>( |
| 243 | "ErrorMessages", sdbusplus::vtable::property_::emits_change, |
| 244 | [this](const auto&) { |
| 245 | return utils::transform(errorMessages, [](const auto& em) { |
| 246 | return std::tuple<std::string, std::string>( |
| 247 | utils::enumToString(em.error), em.arg0); |
| 248 | }); |
| 249 | }); |
| 250 | dbusIface->register_property_rw<uint64_t>( |
| 251 | "Interval", sdbusplus::vtable::property_::emits_change, |
| 252 | [this](uint64_t newVal, auto& oldVal) { |
| 253 | const Milliseconds newValT{newVal}; |
| 254 | if (newValT < ReportManager::minInterval && |
| 255 | newValT != Milliseconds{0}) |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 256 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 257 | throw sdbusplus::exception::SdBusError( |
| 258 | static_cast<int>(std::errc::invalid_argument), |
| 259 | "Invalid interval"); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 260 | } |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 261 | |
| 262 | if (newValT != interval) |
| 263 | { |
| 264 | oldVal = newVal; |
| 265 | interval = newValT; |
| 266 | |
| 267 | errorMessages = verify(); |
| 268 | if (state.set<ReportFlags::valid>(errorMessages.empty()) == |
| 269 | StateEvent::active) |
| 270 | { |
| 271 | scheduleTimer(); |
| 272 | } |
| 273 | |
| 274 | persistency = storeConfiguration(); |
| 275 | } |
| 276 | return 1; |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 277 | }, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 278 | [this](const auto&) { return interval.count(); }); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 279 | dbusIface->register_property_rw<bool>( |
| 280 | "Persistency", sdbusplus::vtable::property_::emits_change, |
| 281 | [this](bool newVal, auto& oldVal) { |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 282 | if (newVal == persistency) |
| 283 | { |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 284 | return 1; |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 285 | } |
| 286 | if (newVal) |
| 287 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 288 | persistency = oldVal = storeConfiguration(); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 289 | } |
| 290 | else |
| 291 | { |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 292 | reportStorage.remove(reportFileName()); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 293 | persistency = oldVal = false; |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 294 | } |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 295 | return 1; |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 296 | }, |
| 297 | [this](const auto&) { return persistency; }); |
| 298 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 299 | dbusIface->register_property_r("Readings", readings, |
| 300 | sdbusplus::vtable::property_::emits_change, |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 301 | [this](const auto&) { return readings; }); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 302 | dbusIface->register_property_rw<std::string>( |
| 303 | "ReportingType", sdbusplus::vtable::property_::emits_change, |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 304 | [this](auto newVal, auto& oldVal) { |
| 305 | ReportingType tmp = utils::toReportingType(newVal); |
| 306 | if (tmp != reportingType) |
| 307 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 308 | reportingType = tmp; |
| 309 | oldVal = std::move(newVal); |
| 310 | |
| 311 | errorMessages = verify(); |
| 312 | if (state.set<ReportFlags::valid>(errorMessages.empty()) == |
| 313 | StateEvent::active) |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 314 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 315 | scheduleTimer(); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 316 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 317 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 318 | persistency = storeConfiguration(); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 319 | |
| 320 | setReadingBuffer(reportUpdates); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 321 | } |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 322 | return 1; |
| 323 | }, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 324 | [this](const auto&) { return utils::enumToString(reportingType); }); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 325 | dbusIface->register_property_r( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 326 | "ReadingParameters", readingParametersPastVersion, |
| 327 | sdbusplus::vtable::property_::const_, |
| 328 | [this](const auto&) { return readingParametersPastVersion; }); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 329 | dbusIface->register_property_rw( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 330 | "ReadingParametersFutureVersion", readingParameters, |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 331 | sdbusplus::vtable::property_::emits_change, |
| 332 | [this, &reportFactory](auto newVal, auto& oldVal) { |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 333 | auto labeledMetricParams = |
| 334 | reportFactory.convertMetricParams(newVal); |
| 335 | ReportManager::verifyMetricParameters(labeledMetricParams); |
| 336 | reportFactory.updateMetrics(metrics, |
| 337 | state.get<ReportFlags::enabled>(), |
| 338 | labeledMetricParams); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 339 | readingParameters = toReadingParameters( |
| 340 | utils::transform(metrics, [](const auto& metric) { |
| 341 | return metric->dumpConfiguration(); |
| 342 | })); |
| 343 | persistency = storeConfiguration(); |
| 344 | oldVal = std::move(newVal); |
| 345 | return 1; |
| 346 | }, |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 347 | [this](const auto&) { return readingParameters; }); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 348 | dbusIface->register_property_r<bool>( |
| 349 | "EmitsReadingsUpdate", sdbusplus::vtable::property_::none, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 350 | [this](const auto&) { |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 351 | return reportActions.contains(ReportAction::emitsReadingsUpdate); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 352 | }); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 353 | dbusIface->register_property_r<std::string>( |
| 354 | "Name", sdbusplus::vtable::property_::const_, |
| 355 | [this](const auto&) { return name; }); |
| 356 | dbusIface->register_property_r<bool>( |
| 357 | "LogToMetricReportsCollection", sdbusplus::vtable::property_::const_, |
| 358 | [this](const auto&) { |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 359 | return reportActions.contains( |
| 360 | ReportAction::logToMetricReportsCollection); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 361 | }); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 362 | dbusIface->register_property_rw<std::vector<std::string>>( |
| 363 | "ReportActions", sdbusplus::vtable::property_::emits_change, |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 364 | [this](auto newVal, auto& oldVal) { |
| 365 | auto tmp = utils::transform<std::unordered_set>( |
| 366 | newVal, [](const auto& reportAction) { |
| 367 | return utils::toReportAction(reportAction); |
| 368 | }); |
| 369 | tmp.insert(ReportAction::logToMetricReportsCollection); |
| 370 | |
| 371 | if (tmp != reportActions) |
| 372 | { |
| 373 | reportActions = tmp; |
| 374 | persistency = storeConfiguration(); |
| 375 | oldVal = std::move(newVal); |
| 376 | } |
| 377 | return 1; |
| 378 | }, |
| 379 | [this](const auto&) { |
| 380 | return utils::transform<std::vector>( |
| 381 | reportActions, [](const auto reportAction) { |
| 382 | return utils::enumToString(reportAction); |
| 383 | }); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 384 | }); |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame] | 385 | dbusIface->register_property_r( |
| 386 | "AppendLimit", appendLimit.value_or(sensorCount), |
| 387 | sdbusplus::vtable::property_::emits_change, |
| 388 | [this](const auto&) { return appendLimit.value_or(sensorCount); }); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 389 | dbusIface->register_property_rw( |
| 390 | "ReportUpdates", std::string(), |
| 391 | sdbusplus::vtable::property_::emits_change, |
| 392 | [this](auto newVal, auto& oldVal) { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 393 | setReportUpdates(utils::toReportUpdates(newVal)); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 394 | oldVal = newVal; |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 395 | return 1; |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 396 | }, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 397 | [this](const auto&) { return utils::enumToString(reportUpdates); }); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 398 | dbusIface->register_property_r( |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 399 | "Triggers", std::vector<sdbusplus::message::object_path>{}, |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 400 | sdbusplus::vtable::property_::emits_change, [this](const auto&) { |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 401 | return utils::transform<std::vector>( |
| 402 | triggerIds, [](const auto& triggerId) { |
| 403 | return utils::pathAppend(utils::constants::triggerDirPath, |
| 404 | triggerId); |
| 405 | }); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 406 | }); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 407 | dbusIface->register_method("Update", [this] { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 408 | if (reportingType == ReportingType::onRequest) |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 409 | { |
| 410 | updateReadings(); |
| 411 | } |
| 412 | }); |
| 413 | constexpr bool skipPropertiesChangedSignal = true; |
| 414 | dbusIface->initialize(skipPropertiesChangedSignal); |
| 415 | return dbusIface; |
| 416 | } |
| 417 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 418 | void Report::timerProcForPeriodicReport(boost::system::error_code ec, |
| 419 | Report& self) |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 420 | { |
| 421 | if (ec) |
| 422 | { |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | self.updateReadings(); |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 427 | self.scheduleTimerForPeriodicReport(self.interval); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 430 | void Report::timerProcForOnChangeReport(boost::system::error_code ec, |
| 431 | Report& self) |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 432 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 433 | if (ec) |
| 434 | { |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | const auto ensure = |
| 439 | utils::Ensure{[&self] { self.onChangeContext = std::nullopt; }}; |
| 440 | |
| 441 | self.onChangeContext.emplace(self); |
| 442 | |
| 443 | const auto steadyTimestamp = self.clock->steadyTimestamp(); |
| 444 | |
| 445 | for (auto& metric : self.metrics) |
| 446 | { |
| 447 | metric->updateReadings(steadyTimestamp); |
| 448 | } |
| 449 | |
| 450 | self.scheduleTimerForOnChangeReport(); |
| 451 | } |
| 452 | |
| 453 | void Report::scheduleTimerForPeriodicReport(Milliseconds timerInterval) |
| 454 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 455 | timer.expires_after(timerInterval); |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 456 | timer.async_wait([this](boost::system::error_code ec) { |
| 457 | timerProcForPeriodicReport(ec, *this); |
| 458 | }); |
| 459 | } |
| 460 | |
| 461 | void Report::scheduleTimerForOnChangeReport() |
| 462 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 463 | constexpr Milliseconds timerInterval{100}; |
| 464 | |
| 465 | timer.expires_after(timerInterval); |
| 466 | timer.async_wait([this](boost::system::error_code ec) { |
| 467 | timerProcForOnChangeReport(ec, *this); |
| 468 | }); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void Report::updateReadings() |
| 472 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 473 | if (!state.isActive()) |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 474 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 475 | return; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 476 | } |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 477 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 478 | if (reportUpdates == ReportUpdates::overwrite || |
| 479 | reportingType == ReportingType::onRequest) |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 480 | { |
| 481 | readingsBuffer.clear(); |
| 482 | } |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 483 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 484 | for (const auto& metric : metrics) |
| 485 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 486 | if (!state.isActive()) |
| 487 | { |
| 488 | break; |
| 489 | } |
| 490 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 491 | for (const auto& [id, metadata, value, timestamp] : |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 492 | metric->getUpdatedReadings()) |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 493 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 494 | if (reportUpdates == ReportUpdates::appendStopsWhenFull && |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 495 | readingsBuffer.isFull()) |
| 496 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 497 | state.set<ReportFlags::enabled>(false); |
| 498 | reportIface->signal_property("Enabled"); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 499 | break; |
| 500 | } |
| 501 | readingsBuffer.emplace(id, metadata, value, timestamp); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 502 | } |
| 503 | } |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 504 | |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 505 | std::get<0>(readings) = |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 506 | std::chrono::duration_cast<Milliseconds>(clock->systemTimestamp()) |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 507 | .count(); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 508 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 509 | if (utils::contains(reportActions, ReportAction::emitsReadingsUpdate)) |
| 510 | { |
| 511 | reportIface->signal_property("Readings"); |
| 512 | } |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 513 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 514 | |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 515 | bool Report::shouldStoreMetricValues() const |
| 516 | { |
| 517 | return reportingType != ReportingType::onRequest && |
| 518 | reportUpdates == ReportUpdates::appendStopsWhenFull; |
| 519 | } |
| 520 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 521 | bool Report::storeConfiguration() const |
| 522 | { |
| 523 | try |
| 524 | { |
| 525 | nlohmann::json data; |
| 526 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 527 | data["Enabled"] = state.get<ReportFlags::enabled>(); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 528 | data["Version"] = reportVersion; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 529 | data["Id"] = id; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 530 | data["Name"] = name; |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 531 | data["ReportingType"] = utils::toUnderlying(reportingType); |
| 532 | data["ReportActions"] = |
| 533 | utils::transform(reportActions, [](const auto reportAction) { |
| 534 | return utils::toUnderlying(reportAction); |
| 535 | }); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 536 | data["Interval"] = interval.count(); |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame] | 537 | data["AppendLimit"] = |
| 538 | appendLimit.value_or(std::numeric_limits<uint64_t>::max()); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 539 | data["ReportUpdates"] = utils::toUnderlying(reportUpdates); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 540 | data["ReadingParameters"] = |
| 541 | utils::transform(metrics, [](const auto& metric) { |
| 542 | return metric->dumpConfiguration(); |
| 543 | }); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 544 | |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 545 | if (shouldStoreMetricValues()) |
| 546 | { |
| 547 | data["MetricValues"] = utils::toLabeledReadings(readings); |
| 548 | } |
| 549 | |
| 550 | reportStorage.store(reportFileName(), data); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 551 | } |
| 552 | catch (const std::exception& e) |
| 553 | { |
| 554 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 555 | "Failed to store a report in storage", |
Wludzik, Jozef | 982c5b5 | 2021-01-02 12:05:21 +0100 | [diff] [blame] | 556 | phosphor::logging::entry("EXCEPTION_MSG=%s", e.what())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 557 | return false; |
| 558 | } |
| 559 | |
| 560 | return true; |
| 561 | } |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 562 | |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 563 | interfaces::JsonStorage::FilePath Report::reportFileName() const |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 564 | { |
| 565 | return interfaces::JsonStorage::FilePath{ |
| 566 | std::to_string(std::hash<std::string>{}(id))}; |
| 567 | } |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 568 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 569 | std::unordered_set<std::string> |
| 570 | Report::collectTriggerIds(boost::asio::io_context& ioc) const |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 571 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 572 | utils::Messanger tmp(ioc); |
| 573 | |
| 574 | auto result = std::unordered_set<std::string>(); |
| 575 | |
| 576 | tmp.on_receive<messages::CollectTriggerIdResp>( |
| 577 | [&result](const auto& msg) { result.insert(msg.triggerId); }); |
| 578 | |
| 579 | tmp.send(messages::CollectTriggerIdReq{id}); |
| 580 | |
| 581 | return result; |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 582 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 583 | |
| 584 | void Report::metricUpdated() |
| 585 | { |
| 586 | if (onChangeContext) |
| 587 | { |
| 588 | onChangeContext->metricUpdated(); |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | updateReadings(); |
| 593 | } |
| 594 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 595 | void Report::scheduleTimer() |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 596 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 597 | switch (reportingType) |
| 598 | { |
| 599 | case ReportingType::periodic: |
| 600 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 601 | unregisterFromMetrics = nullptr; |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 602 | scheduleTimerForPeriodicReport(interval); |
| 603 | break; |
| 604 | } |
| 605 | case ReportingType::onChange: |
| 606 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 607 | if (!unregisterFromMetrics) |
| 608 | { |
| 609 | unregisterFromMetrics = [this] { |
| 610 | for (auto& metric : metrics) |
| 611 | { |
| 612 | metric->unregisterFromUpdates(*this); |
| 613 | } |
| 614 | }; |
| 615 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 616 | for (auto& metric : metrics) |
| 617 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 618 | metric->registerForUpdates(*this); |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 619 | } |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 620 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 621 | |
| 622 | bool isTimerRequired = false; |
| 623 | |
| 624 | for (auto& metric : metrics) |
| 625 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 626 | if (metric->isTimerRequired()) |
| 627 | { |
| 628 | isTimerRequired = true; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | if (isTimerRequired) |
| 633 | { |
| 634 | scheduleTimerForOnChangeReport(); |
| 635 | } |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 636 | else |
| 637 | { |
| 638 | timer.cancel(); |
| 639 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | default: |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 643 | unregisterFromMetrics = nullptr; |
| 644 | timer.cancel(); |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 645 | break; |
| 646 | } |
| 647 | } |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 648 | |
| 649 | std::vector<ErrorMessage> Report::verify() const |
| 650 | { |
| 651 | std::vector<ErrorMessage> result; |
| 652 | |
| 653 | if ((reportingType == ReportingType::periodic && |
| 654 | interval == Milliseconds{0}) || |
| 655 | (reportingType != ReportingType::periodic && |
| 656 | interval != Milliseconds{0})) |
| 657 | { |
| 658 | result.emplace_back(ErrorType::propertyConflict, "Interval"); |
| 659 | result.emplace_back(ErrorType::propertyConflict, "ReportingType"); |
| 660 | } |
| 661 | |
| 662 | return result; |
| 663 | } |