Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 1 | #include "report.hpp" |
| 2 | |
| 3 | #include "report_manager.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 4 | #include "utils/transform.hpp" |
| 5 | |
| 6 | #include <phosphor-logging/log.hpp> |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 7 | #include <sdbusplus/vtable.hpp> |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 8 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 9 | #include <numeric> |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 10 | |
| 11 | Report::Report(boost::asio::io_context& ioc, |
| 12 | const std::shared_ptr<sdbusplus::asio::object_server>& objServer, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 13 | const std::string& reportName, |
| 14 | const std::string& reportingTypeIn, |
| 15 | const bool emitsReadingsUpdateIn, |
| 16 | const bool logToMetricReportsCollectionIn, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 17 | const Milliseconds intervalIn, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 18 | interfaces::ReportManager& reportManager, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 19 | interfaces::JsonStorage& reportStorageIn, |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 20 | std::vector<std::shared_ptr<interfaces::Metric>> metricsIn, |
| 21 | const bool enabledIn) : |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 22 | name(reportName), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 23 | path(reportDir + name), reportingType(reportingTypeIn), |
| 24 | interval(intervalIn), emitsReadingsUpdate(emitsReadingsUpdateIn), |
| 25 | logToMetricReportsCollection(logToMetricReportsCollectionIn), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 26 | objServer(objServer), metrics(std::move(metricsIn)), timer(ioc), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 27 | fileName(std::to_string(std::hash<std::string>{}(name))), |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 28 | reportStorage(reportStorageIn), enabled(enabledIn) |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 29 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 30 | readingParameters = |
| 31 | toReadingParameters(utils::transform(metrics, [](const auto& metric) { |
| 32 | return metric->dumpConfiguration(); |
| 33 | })); |
| 34 | |
| 35 | readingParametersPastVersion = |
| 36 | utils::transform(readingParameters, [](const auto& item) { |
| 37 | return ReadingParametersPastVersion::value_type( |
| 38 | std::get<0>(item).front(), std::get<1>(item), std::get<2>(item), |
| 39 | std::get<3>(item)); |
| 40 | }); |
| 41 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 42 | deleteIface = objServer->add_unique_interface( |
| 43 | path, deleteIfaceName, [this, &ioc, &reportManager](auto& dbusIface) { |
| 44 | dbusIface.register_method("Delete", [this, &ioc, &reportManager] { |
| 45 | if (persistency) |
| 46 | { |
| 47 | reportStorage.remove(fileName); |
| 48 | } |
| 49 | boost::asio::post(ioc, [this, &reportManager] { |
| 50 | reportManager.removeReport(this); |
| 51 | }); |
| 52 | }); |
| 53 | }); |
| 54 | |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 55 | persistency = storeConfiguration(); |
| 56 | reportIface = makeReportInterface(); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 57 | |
| 58 | if (reportingType == "Periodic") |
| 59 | { |
| 60 | scheduleTimer(interval); |
| 61 | } |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 62 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 63 | if (enabled) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 64 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 65 | for (auto& metric : this->metrics) |
| 66 | { |
| 67 | metric->initialize(); |
| 68 | } |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 69 | } |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 72 | std::unique_ptr<sdbusplus::asio::dbus_interface> Report::makeReportInterface() |
| 73 | { |
| 74 | auto dbusIface = objServer->add_unique_interface(path, reportIfaceName); |
| 75 | dbusIface->register_property_rw( |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 76 | "Enabled", enabled, sdbusplus::vtable::property_::emits_change, |
| 77 | [this](bool newVal, const auto&) { |
| 78 | if (newVal != enabled) |
| 79 | { |
| 80 | if (true == newVal && "Periodic" == reportingType) |
| 81 | { |
| 82 | scheduleTimer(interval); |
| 83 | } |
| 84 | if (newVal) |
| 85 | { |
| 86 | for (auto& metric : metrics) |
| 87 | { |
| 88 | metric->initialize(); |
| 89 | } |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | for (auto& metric : metrics) |
| 94 | { |
| 95 | metric->deinitialize(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | enabled = newVal; |
| 100 | persistency = storeConfiguration(); |
| 101 | } |
| 102 | return true; |
| 103 | }, |
| 104 | [this](const auto&) { return enabled; }); |
| 105 | dbusIface->register_property_rw( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 106 | "Interval", interval.count(), |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 107 | sdbusplus::vtable::property_::emits_change, |
| 108 | [this](uint64_t newVal, auto&) { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 109 | if (Milliseconds newValT{newVal}; |
| 110 | newValT >= ReportManager::minInterval) |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 111 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 112 | if (newValT != interval) |
| 113 | { |
| 114 | interval = newValT; |
| 115 | persistency = storeConfiguration(); |
| 116 | } |
| 117 | return true; |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 118 | } |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 119 | return false; |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 120 | }, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 121 | [this](const auto&) { return interval.count(); }); |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 122 | dbusIface->register_property_rw( |
| 123 | "Persistency", persistency, sdbusplus::vtable::property_::emits_change, |
| 124 | [this](bool newVal, const auto&) { |
| 125 | if (newVal == persistency) |
| 126 | { |
| 127 | return true; |
| 128 | } |
| 129 | if (newVal) |
| 130 | { |
| 131 | persistency = storeConfiguration(); |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | reportStorage.remove(fileName); |
| 136 | persistency = false; |
| 137 | } |
| 138 | return true; |
| 139 | }, |
| 140 | [this](const auto&) { return persistency; }); |
| 141 | |
| 142 | auto readingsFlag = sdbusplus::vtable::property_::none; |
| 143 | if (emitsReadingsUpdate) |
| 144 | { |
| 145 | readingsFlag = sdbusplus::vtable::property_::emits_change; |
| 146 | } |
| 147 | dbusIface->register_property_r("Readings", readings, readingsFlag, |
| 148 | [this](const auto&) { return readings; }); |
| 149 | dbusIface->register_property_r( |
| 150 | "ReportingType", reportingType, sdbusplus::vtable::property_::const_, |
| 151 | [this](const auto&) { return reportingType; }); |
| 152 | dbusIface->register_property_r( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 153 | "ReadingParameters", readingParametersPastVersion, |
| 154 | sdbusplus::vtable::property_::const_, |
| 155 | [this](const auto&) { return readingParametersPastVersion; }); |
| 156 | dbusIface->register_property_r( |
| 157 | "ReadingParametersFutureVersion", readingParameters, |
Krzysztof Grobelny | 85db8bd | 2021-05-28 12:13:23 +0000 | [diff] [blame] | 158 | sdbusplus::vtable::property_::const_, |
| 159 | [this](const auto&) { return readingParameters; }); |
| 160 | dbusIface->register_property_r( |
| 161 | "EmitsReadingsUpdate", emitsReadingsUpdate, |
| 162 | sdbusplus::vtable::property_::const_, |
| 163 | [this](const auto&) { return emitsReadingsUpdate; }); |
| 164 | dbusIface->register_property_r( |
| 165 | "LogToMetricReportsCollection", logToMetricReportsCollection, |
| 166 | sdbusplus::vtable::property_::const_, |
| 167 | [this](const auto&) { return logToMetricReportsCollection; }); |
| 168 | dbusIface->register_method("Update", [this] { |
| 169 | if (reportingType == "OnRequest") |
| 170 | { |
| 171 | updateReadings(); |
| 172 | } |
| 173 | }); |
| 174 | constexpr bool skipPropertiesChangedSignal = true; |
| 175 | dbusIface->initialize(skipPropertiesChangedSignal); |
| 176 | return dbusIface; |
| 177 | } |
| 178 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 179 | void Report::timerProc(boost::system::error_code ec, Report& self) |
| 180 | { |
| 181 | if (ec) |
| 182 | { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | self.updateReadings(); |
| 187 | self.scheduleTimer(self.interval); |
| 188 | } |
| 189 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 190 | void Report::scheduleTimer(Milliseconds timerInterval) |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 191 | { |
| 192 | timer.expires_after(timerInterval); |
| 193 | timer.async_wait( |
| 194 | [this](boost::system::error_code ec) { timerProc(ec, *this); }); |
| 195 | } |
| 196 | |
| 197 | void Report::updateReadings() |
| 198 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 199 | if (!enabled) |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 200 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 201 | return; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 202 | } |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 203 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 204 | using ReadingsTimestamp = std::tuple_element_t<0, Readings>; |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 205 | using ReadingsValue = std::tuple_element_t<1, Readings>; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 206 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 207 | std::get<ReadingsValue>(cachedReadings).clear(); |
| 208 | for (const auto& metric : metrics) |
| 209 | { |
| 210 | for (const auto& [id, meta, val, timestamp] : metric->getReadings()) |
| 211 | { |
| 212 | std::get<ReadingsValue>(cachedReadings) |
| 213 | .emplace_back(id, meta, val, timestamp); |
| 214 | } |
| 215 | } |
| 216 | std::get<ReadingsTimestamp>(cachedReadings) = std::time(0); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 217 | std::swap(readings, cachedReadings); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 218 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 219 | reportIface->signal_property("Readings"); |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 220 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 221 | |
| 222 | bool Report::storeConfiguration() const |
| 223 | { |
| 224 | try |
| 225 | { |
| 226 | nlohmann::json data; |
| 227 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 228 | data["Enabled"] = enabled; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 229 | data["Version"] = reportVersion; |
| 230 | data["Name"] = name; |
| 231 | data["ReportingType"] = reportingType; |
| 232 | data["EmitsReadingsUpdate"] = emitsReadingsUpdate; |
| 233 | data["LogToMetricReportsCollection"] = logToMetricReportsCollection; |
| 234 | data["Interval"] = interval.count(); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 235 | data["ReadingParameters"] = |
| 236 | utils::transform(metrics, [](const auto& metric) { |
| 237 | return metric->dumpConfiguration(); |
| 238 | }); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 239 | |
| 240 | reportStorage.store(fileName, data); |
| 241 | } |
| 242 | catch (const std::exception& e) |
| 243 | { |
| 244 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 245 | "Failed to store a report in storage", |
Wludzik, Jozef | 982c5b5 | 2021-01-02 12:05:21 +0100 | [diff] [blame] | 246 | phosphor::logging::entry("EXCEPTION_MSG=%s", e.what())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 247 | return false; |
| 248 | } |
| 249 | |
| 250 | return true; |
| 251 | } |