xref: /openbmc/telemetry/src/report_factory.cpp (revision 1477fe6a)
12f9f9b87SWludzik, Jozef #include "report_factory.hpp"
22f9f9b87SWludzik, Jozef 
36ccfcbf5SKrzysztof Grobelny #include "metric.hpp"
42f9f9b87SWludzik, Jozef #include "report.hpp"
5e2362796SWludzik, Jozef #include "sensor.hpp"
6*1477fe6aSWludzik, Jozef #include "utils/dbus_mapper.hpp"
7e2362796SWludzik, Jozef #include "utils/transform.hpp"
82f9f9b87SWludzik, Jozef 
92f9f9b87SWludzik, Jozef ReportFactory::ReportFactory(
10e2362796SWludzik, Jozef     std::shared_ptr<sdbusplus::asio::connection> bus,
11*1477fe6aSWludzik, Jozef     const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
12*1477fe6aSWludzik, Jozef     SensorCache& sensorCache) :
13e2362796SWludzik, Jozef     bus(std::move(bus)),
14*1477fe6aSWludzik, Jozef     objServer(objServer), sensorCache(sensorCache)
152f9f9b87SWludzik, Jozef {}
162f9f9b87SWludzik, Jozef 
172f9f9b87SWludzik, Jozef std::unique_ptr<interfaces::Report> ReportFactory::make(
18d2238194SKrzysztof Grobelny     boost::asio::yield_context& yield, const std::string& name,
19d2238194SKrzysztof Grobelny     const std::string& reportingType, bool emitsReadingsSignal,
20d2238194SKrzysztof Grobelny     bool logToMetricReportsCollection, std::chrono::milliseconds period,
21d2238194SKrzysztof Grobelny     const ReadingParameters& metricParams,
22d2238194SKrzysztof Grobelny     interfaces::ReportManager& reportManager,
23d2238194SKrzysztof Grobelny     interfaces::JsonStorage& reportStorage) const
24d2238194SKrzysztof Grobelny {
25d2238194SKrzysztof Grobelny     return make(name, reportingType, emitsReadingsSignal,
26d2238194SKrzysztof Grobelny                 logToMetricReportsCollection, period, metricParams,
27d2238194SKrzysztof Grobelny                 reportManager, reportStorage,
28d2238194SKrzysztof Grobelny                 convertMetricParams(yield, metricParams));
29d2238194SKrzysztof Grobelny }
30d2238194SKrzysztof Grobelny 
31d2238194SKrzysztof Grobelny std::unique_ptr<interfaces::Report> ReportFactory::make(
322f9f9b87SWludzik, Jozef     const std::string& name, const std::string& reportingType,
332f9f9b87SWludzik, Jozef     bool emitsReadingsSignal, bool logToMetricReportsCollection,
342f9f9b87SWludzik, Jozef     std::chrono::milliseconds period, const ReadingParameters& metricParams,
35e2362796SWludzik, Jozef     interfaces::ReportManager& reportManager,
36d2238194SKrzysztof Grobelny     interfaces::JsonStorage& reportStorage,
37d2238194SKrzysztof Grobelny     std::vector<LabeledMetricParameters> labeledMetricParams) const
382f9f9b87SWludzik, Jozef {
39d2238194SKrzysztof Grobelny     std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform(
40d2238194SKrzysztof Grobelny         labeledMetricParams,
41d2238194SKrzysztof Grobelny         [this](const LabeledMetricParameters& param)
42d2238194SKrzysztof Grobelny             -> std::shared_ptr<interfaces::Metric> {
43d2238194SKrzysztof Grobelny             return std::make_shared<Metric>(
44d2238194SKrzysztof Grobelny                 getSensors(param.at_index<0>()), param.at_index<1>(),
45d2238194SKrzysztof Grobelny                 param.at_index<2>(), param.at_index<3>());
46d2238194SKrzysztof Grobelny         });
47c8e3a64aSKrzysztof Grobelny 
482f9f9b87SWludzik, Jozef     return std::make_unique<Report>(
49e2362796SWludzik, Jozef         bus->get_io_context(), objServer, name, reportingType,
50e2362796SWludzik, Jozef         emitsReadingsSignal, logToMetricReportsCollection, period, metricParams,
51e2362796SWludzik, Jozef         reportManager, reportStorage, std::move(metrics));
522f9f9b87SWludzik, Jozef }
536ccfcbf5SKrzysztof Grobelny 
546ccfcbf5SKrzysztof Grobelny std::vector<std::shared_ptr<interfaces::Sensor>> ReportFactory::getSensors(
55d2238194SKrzysztof Grobelny     const std::vector<LabeledSensorParameters>& sensorPaths) const
566ccfcbf5SKrzysztof Grobelny {
57d2238194SKrzysztof Grobelny     return utils::transform(sensorPaths,
58d2238194SKrzysztof Grobelny                             [this](const LabeledSensorParameters& param)
596ccfcbf5SKrzysztof Grobelny                                 -> std::shared_ptr<interfaces::Sensor> {
60d2238194SKrzysztof Grobelny                                 using namespace utils::tstring;
61d2238194SKrzysztof Grobelny 
626ccfcbf5SKrzysztof Grobelny                                 return sensorCache.makeSensor<Sensor>(
63d2238194SKrzysztof Grobelny                                     param.at_label<Service>(),
64d2238194SKrzysztof Grobelny                                     param.at_label<Path>(),
65d2238194SKrzysztof Grobelny                                     bus->get_io_context(), bus);
666ccfcbf5SKrzysztof Grobelny                             });
676ccfcbf5SKrzysztof Grobelny }
686ccfcbf5SKrzysztof Grobelny 
69d2238194SKrzysztof Grobelny std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
70d2238194SKrzysztof Grobelny     boost::asio::yield_context& yield,
71d2238194SKrzysztof Grobelny     const ReadingParameters& metricParams) const
726ccfcbf5SKrzysztof Grobelny {
73*1477fe6aSWludzik, Jozef     auto tree = utils::getSubTreeSensors(yield, bus);
74d2238194SKrzysztof Grobelny 
75d2238194SKrzysztof Grobelny     return utils::transform(metricParams, [&tree](const auto& item) {
76d2238194SKrzysztof Grobelny         std::vector<LabeledSensorParameters> sensors;
77d2238194SKrzysztof Grobelny 
78d2238194SKrzysztof Grobelny         for (const auto& sensorPath : std::get<0>(item))
79d2238194SKrzysztof Grobelny         {
80d2238194SKrzysztof Grobelny             auto it = std::find_if(
81d2238194SKrzysztof Grobelny                 tree.begin(), tree.end(),
82d2238194SKrzysztof Grobelny                 [&sensorPath](const auto& v) { return v.first == sensorPath; });
83d2238194SKrzysztof Grobelny 
84d2238194SKrzysztof Grobelny             if (it != tree.end())
85d2238194SKrzysztof Grobelny             {
86d2238194SKrzysztof Grobelny                 for (const auto& [service, ifaces] : it->second)
87d2238194SKrzysztof Grobelny                 {
88d2238194SKrzysztof Grobelny                     sensors.emplace_back(service, sensorPath);
89d2238194SKrzysztof Grobelny                 }
90d2238194SKrzysztof Grobelny             }
91d2238194SKrzysztof Grobelny         }
92d2238194SKrzysztof Grobelny 
93d2238194SKrzysztof Grobelny         return LabeledMetricParameters(std::move(sensors), std::get<1>(item),
94d2238194SKrzysztof Grobelny                                        std::get<2>(item), std::get<3>(item));
95d2238194SKrzysztof Grobelny     });
966ccfcbf5SKrzysztof Grobelny }
97