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, |
| 17 | const std::chrono::milliseconds intervalIn, |
| 18 | const ReadingParameters& readingParametersIn, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 19 | interfaces::ReportManager& reportManager, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 20 | interfaces::JsonStorage& reportStorageIn, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 21 | std::vector<std::shared_ptr<interfaces::Metric>> metrics) : |
| 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), |
| 26 | readingParameters(readingParametersIn), objServer(objServer), |
| 27 | metrics(std::move(metrics)), timer(ioc), |
| 28 | fileName(std::to_string(std::hash<std::string>{}(name))), |
| 29 | reportStorage(reportStorageIn) |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 30 | { |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 31 | for (auto& metric : this->metrics) |
| 32 | { |
| 33 | metric->initialize(); |
| 34 | } |
| 35 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 36 | deleteIface = objServer->add_unique_interface( |
| 37 | path, deleteIfaceName, [this, &ioc, &reportManager](auto& dbusIface) { |
| 38 | dbusIface.register_method("Delete", [this, &ioc, &reportManager] { |
| 39 | if (persistency) |
| 40 | { |
| 41 | reportStorage.remove(fileName); |
| 42 | } |
| 43 | boost::asio::post(ioc, [this, &reportManager] { |
| 44 | reportManager.removeReport(this); |
| 45 | }); |
| 46 | }); |
| 47 | }); |
| 48 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 49 | reportIface = objServer->add_unique_interface( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 50 | path, reportIfaceName, [this](auto& dbusIface) { |
| 51 | dbusIface.register_property_rw( |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 52 | "Interval", static_cast<uint64_t>(interval.count()), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 53 | sdbusplus::vtable::property_::emits_change, |
| 54 | [this](uint64_t newVal, auto&) { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 55 | std::chrono::milliseconds newValT(newVal); |
| 56 | if (newValT < ReportManager::minInterval) |
| 57 | { |
| 58 | return false; |
| 59 | } |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 60 | interval = newValT; |
| 61 | return true; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 62 | }, |
| 63 | [this](const auto&) { |
| 64 | return static_cast<uint64_t>(interval.count()); |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 65 | }); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 66 | persistency = storeConfiguration(); |
| 67 | dbusIface.register_property_rw( |
| 68 | "Persistency", persistency, |
| 69 | sdbusplus::vtable::property_::emits_change, |
| 70 | [this](bool newVal, const auto&) { |
| 71 | if (newVal == persistency) |
| 72 | { |
| 73 | return true; |
| 74 | } |
| 75 | if (newVal) |
| 76 | { |
| 77 | persistency = storeConfiguration(); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | reportStorage.remove(fileName); |
| 82 | persistency = false; |
| 83 | } |
| 84 | return true; |
| 85 | }, |
| 86 | [this](const auto&) { return persistency; }); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame^] | 87 | |
| 88 | auto readingsFlag = sdbusplus::vtable::property_::none; |
| 89 | if (emitsReadingsUpdate) |
| 90 | { |
| 91 | readingsFlag = sdbusplus::vtable::property_::emits_change; |
| 92 | } |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 93 | dbusIface.register_property_r( |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame^] | 94 | "Readings", readings, readingsFlag, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 95 | [this](const auto&) { return readings; }); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 96 | dbusIface.register_property_r( |
| 97 | "ReportingType", reportingType, |
| 98 | sdbusplus::vtable::property_::const_, |
| 99 | [this](const auto&) { return reportingType; }); |
| 100 | dbusIface.register_property_r( |
| 101 | "ReadingParameters", readingParameters, |
| 102 | sdbusplus::vtable::property_::const_, |
| 103 | [this](const auto&) { return readingParameters; }); |
| 104 | dbusIface.register_property_r( |
| 105 | "EmitsReadingsUpdate", emitsReadingsUpdate, |
| 106 | sdbusplus::vtable::property_::const_, |
| 107 | [this](const auto&) { return emitsReadingsUpdate; }); |
| 108 | dbusIface.register_property_r( |
| 109 | "LogToMetricReportsCollection", logToMetricReportsCollection, |
| 110 | sdbusplus::vtable::property_::const_, |
| 111 | [this](const auto&) { return logToMetricReportsCollection; }); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 112 | dbusIface.register_method("Update", [this] { |
| 113 | if (reportingType == "OnRequest") |
| 114 | { |
| 115 | updateReadings(); |
| 116 | } |
| 117 | }); |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 118 | }); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 119 | |
| 120 | if (reportingType == "Periodic") |
| 121 | { |
| 122 | scheduleTimer(interval); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void Report::timerProc(boost::system::error_code ec, Report& self) |
| 127 | { |
| 128 | if (ec) |
| 129 | { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | self.updateReadings(); |
| 134 | self.scheduleTimer(self.interval); |
| 135 | } |
| 136 | |
| 137 | void Report::scheduleTimer(std::chrono::milliseconds timerInterval) |
| 138 | { |
| 139 | timer.expires_after(timerInterval); |
| 140 | timer.async_wait( |
| 141 | [this](boost::system::error_code ec) { timerProc(ec, *this); }); |
| 142 | } |
| 143 | |
| 144 | void Report::updateReadings() |
| 145 | { |
| 146 | auto numElements = std::accumulate( |
| 147 | metrics.begin(), metrics.end(), 0u, [](auto sum, const auto& metric) { |
| 148 | return sum + metric->getReadings().size(); |
| 149 | }); |
| 150 | |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 151 | std::tuple_element_t<1, Readings> readingsCache(numElements); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 152 | |
| 153 | auto it = readingsCache.begin(); |
| 154 | |
| 155 | for (const auto& metric : metrics) |
| 156 | { |
| 157 | for (const auto& reading : metric->getReadings()) |
| 158 | { |
| 159 | *(it++) = std::make_tuple(reading.id, reading.metadata, |
| 160 | reading.value, reading.timestamp); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | std::get<0>(readings) = std::time(0); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 165 | std::get<1>(readings) = std::move(readingsCache); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 166 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 167 | reportIface->signal_property("Readings"); |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 168 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 169 | |
| 170 | bool Report::storeConfiguration() const |
| 171 | { |
| 172 | try |
| 173 | { |
| 174 | nlohmann::json data; |
| 175 | |
| 176 | data["Version"] = reportVersion; |
| 177 | data["Name"] = name; |
| 178 | data["ReportingType"] = reportingType; |
| 179 | data["EmitsReadingsUpdate"] = emitsReadingsUpdate; |
| 180 | data["LogToMetricReportsCollection"] = logToMetricReportsCollection; |
| 181 | data["Interval"] = interval.count(); |
| 182 | data["ReadingParameters"] = utils::transform( |
| 183 | metrics, [](const auto& metric) { return metric->to_json(); }); |
| 184 | |
| 185 | reportStorage.store(fileName, data); |
| 186 | } |
| 187 | catch (const std::exception& e) |
| 188 | { |
| 189 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 190 | "Failed to store a report in storage", |
| 191 | phosphor::logging::entry("msg=", e.what())); |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | return true; |
| 196 | } |