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