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