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