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