report.cpp (dcc4e1936173a93251a02066432bc2bcbc386240) | report.cpp (7e098e93ef0974739459d296f99ddfab54722c23) |
---|---|
1#include "report.hpp" 2 3#include "report_manager.hpp" 4#include "utils/transform.hpp" 5 6#include <phosphor-logging/log.hpp> 7#include <sdbusplus/vtable.hpp> 8 9#include <numeric> 10 11Report::Report(boost::asio::io_context& ioc, 12 const std::shared_ptr<sdbusplus::asio::object_server>& objServer, 13 const std::string& reportName, 14 const std::string& reportingTypeIn, 15 const bool emitsReadingsUpdateIn, 16 const bool logToMetricReportsCollectionIn, 17 const Milliseconds intervalIn, 18 interfaces::ReportManager& reportManager, 19 interfaces::JsonStorage& reportStorageIn, | 1#include "report.hpp" 2 3#include "report_manager.hpp" 4#include "utils/transform.hpp" 5 6#include <phosphor-logging/log.hpp> 7#include <sdbusplus/vtable.hpp> 8 9#include <numeric> 10 11Report::Report(boost::asio::io_context& ioc, 12 const std::shared_ptr<sdbusplus::asio::object_server>& objServer, 13 const std::string& reportName, 14 const std::string& reportingTypeIn, 15 const bool emitsReadingsUpdateIn, 16 const bool logToMetricReportsCollectionIn, 17 const Milliseconds intervalIn, 18 interfaces::ReportManager& reportManager, 19 interfaces::JsonStorage& reportStorageIn, |
20 std::vector<std::shared_ptr<interfaces::Metric>> metricsIn) : | 20 std::vector<std::shared_ptr<interfaces::Metric>> metricsIn, 21 const bool enabledIn) : |
21 name(reportName), 22 path(reportDir + name), reportingType(reportingTypeIn), 23 interval(intervalIn), emitsReadingsUpdate(emitsReadingsUpdateIn), 24 logToMetricReportsCollection(logToMetricReportsCollectionIn), 25 objServer(objServer), metrics(std::move(metricsIn)), timer(ioc), 26 fileName(std::to_string(std::hash<std::string>{}(name))), | 22 name(reportName), 23 path(reportDir + name), reportingType(reportingTypeIn), 24 interval(intervalIn), emitsReadingsUpdate(emitsReadingsUpdateIn), 25 logToMetricReportsCollection(logToMetricReportsCollectionIn), 26 objServer(objServer), metrics(std::move(metricsIn)), timer(ioc), 27 fileName(std::to_string(std::hash<std::string>{}(name))), |
27 reportStorage(reportStorageIn) | 28 reportStorage(reportStorageIn), enabled(enabledIn) |
28{ 29 readingParameters = 30 toReadingParameters(utils::transform(metrics, [](const auto& metric) { 31 return metric->dumpConfiguration(); 32 })); 33 34 readingParametersPastVersion = 35 utils::transform(readingParameters, [](const auto& item) { --- 18 unchanged lines hidden (view full) --- 54 persistency = storeConfiguration(); 55 reportIface = makeReportInterface(); 56 57 if (reportingType == "Periodic") 58 { 59 scheduleTimer(interval); 60 } 61 | 29{ 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) { --- 18 unchanged lines hidden (view full) --- 55 persistency = storeConfiguration(); 56 reportIface = makeReportInterface(); 57 58 if (reportingType == "Periodic") 59 { 60 scheduleTimer(interval); 61 } 62 |
62 for (auto& metric : this->metrics) | 63 if (enabled) |
63 { | 64 { |
64 metric->initialize(); | 65 for (auto& metric : this->metrics) 66 { 67 metric->initialize(); 68 } |
65 } 66} 67 68std::unique_ptr<sdbusplus::asio::dbus_interface> Report::makeReportInterface() 69{ 70 auto dbusIface = objServer->add_unique_interface(path, reportIfaceName); 71 dbusIface->register_property_rw( | 69 } 70} 71 72std::unique_ptr<sdbusplus::asio::dbus_interface> Report::makeReportInterface() 73{ 74 auto dbusIface = objServer->add_unique_interface(path, reportIfaceName); 75 dbusIface->register_property_rw( |
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( |
|
72 "Interval", interval.count(), 73 sdbusplus::vtable::property_::emits_change, 74 [this](uint64_t newVal, auto&) { | 106 "Interval", interval.count(), 107 sdbusplus::vtable::property_::emits_change, 108 [this](uint64_t newVal, auto&) { |
75 Milliseconds newValT(newVal); 76 if (newValT < ReportManager::minInterval) | 109 if (Milliseconds newValT{newVal}; 110 newValT >= ReportManager::minInterval) |
77 { | 111 { |
78 return false; | 112 if (newValT != interval) 113 { 114 interval = newValT; 115 persistency = storeConfiguration(); 116 } 117 return true; |
79 } | 118 } |
80 interval = newValT; 81 return true; | 119 return false; |
82 }, 83 [this](const auto&) { return interval.count(); }); 84 dbusIface->register_property_rw( 85 "Persistency", persistency, sdbusplus::vtable::property_::emits_change, 86 [this](bool newVal, const auto&) { 87 if (newVal == persistency) 88 { 89 return true; --- 63 unchanged lines hidden (view full) --- 153{ 154 timer.expires_after(timerInterval); 155 timer.async_wait( 156 [this](boost::system::error_code ec) { timerProc(ec, *this); }); 157} 158 159void Report::updateReadings() 160{ | 120 }, 121 [this](const auto&) { return interval.count(); }); 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; --- 63 unchanged lines hidden (view full) --- 191{ 192 timer.expires_after(timerInterval); 193 timer.async_wait( 194 [this](boost::system::error_code ec) { timerProc(ec, *this); }); 195} 196 197void Report::updateReadings() 198{ |
199 if (!enabled) 200 { 201 return; 202 } 203 204 using ReadingsTimestamp = std::tuple_element_t<0, Readings>; |
|
161 using ReadingsValue = std::tuple_element_t<1, Readings>; | 205 using ReadingsValue = std::tuple_element_t<1, Readings>; |
162 std::get<ReadingsValue>(cachedReadings).clear(); | |
163 | 206 |
207 std::get<ReadingsValue>(cachedReadings).clear(); |
|
164 for (const auto& metric : metrics) 165 { | 208 for (const auto& metric : metrics) 209 { |
166 for (const auto& reading : metric->getReadings()) | 210 for (const auto& [id, meta, val, timestamp] : metric->getReadings()) |
167 { | 211 { |
168 std::get<1>(cachedReadings) 169 .emplace_back(reading.id, reading.metadata, reading.value, 170 reading.timestamp); | 212 std::get<ReadingsValue>(cachedReadings) 213 .emplace_back(id, meta, val, timestamp); |
171 } 172 } | 214 } 215 } |
173 174 using ReadingsTimestamp = std::tuple_element_t<0, Readings>; | |
175 std::get<ReadingsTimestamp>(cachedReadings) = std::time(0); | 216 std::get<ReadingsTimestamp>(cachedReadings) = std::time(0); |
176 | |
177 std::swap(readings, cachedReadings); 178 179 reportIface->signal_property("Readings"); 180} 181 182bool Report::storeConfiguration() const 183{ 184 try 185 { 186 nlohmann::json data; 187 | 217 std::swap(readings, cachedReadings); 218 219 reportIface->signal_property("Readings"); 220} 221 222bool Report::storeConfiguration() const 223{ 224 try 225 { 226 nlohmann::json data; 227 |
228 data["Enabled"] = enabled; |
|
188 data["Version"] = reportVersion; 189 data["Name"] = name; 190 data["ReportingType"] = reportingType; 191 data["EmitsReadingsUpdate"] = emitsReadingsUpdate; 192 data["LogToMetricReportsCollection"] = logToMetricReportsCollection; 193 data["Interval"] = interval.count(); 194 data["ReadingParameters"] = 195 utils::transform(metrics, [](const auto& metric) { --- 15 unchanged lines hidden --- | 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(); 235 data["ReadingParameters"] = 236 utils::transform(metrics, [](const auto& metric) { --- 15 unchanged lines hidden --- |