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