xref: /openbmc/telemetry/src/report_factory.cpp (revision 7e098e93)
12f9f9b87SWludzik, Jozef #include "report_factory.hpp"
22f9f9b87SWludzik, Jozef 
36ccfcbf5SKrzysztof Grobelny #include "metric.hpp"
42f9f9b87SWludzik, Jozef #include "report.hpp"
5e2362796SWludzik, Jozef #include "sensor.hpp"
68069771cSKrzysztof Grobelny #include "utils/clock.hpp"
7e8fc5751SKrzysztof Grobelny #include "utils/conversion.hpp"
81477fe6aSWludzik, Jozef #include "utils/dbus_mapper.hpp"
9e2362796SWludzik, Jozef #include "utils/transform.hpp"
102f9f9b87SWludzik, Jozef 
112f9f9b87SWludzik, Jozef ReportFactory::ReportFactory(
12e2362796SWludzik, Jozef     std::shared_ptr<sdbusplus::asio::connection> bus,
131477fe6aSWludzik, Jozef     const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
141477fe6aSWludzik, Jozef     SensorCache& sensorCache) :
15e2362796SWludzik, Jozef     bus(std::move(bus)),
161477fe6aSWludzik, Jozef     objServer(objServer), sensorCache(sensorCache)
172f9f9b87SWludzik, Jozef {}
182f9f9b87SWludzik, Jozef 
192f9f9b87SWludzik, Jozef std::unique_ptr<interfaces::Report> ReportFactory::make(
202f9f9b87SWludzik, Jozef     const std::string& name, const std::string& reportingType,
212f9f9b87SWludzik, Jozef     bool emitsReadingsSignal, bool logToMetricReportsCollection,
22dcc4e193SKrzysztof Grobelny     Milliseconds period, interfaces::ReportManager& reportManager,
23d2238194SKrzysztof Grobelny     interfaces::JsonStorage& reportStorage,
24*7e098e93SLukasz Kazmierczak     std::vector<LabeledMetricParameters> labeledMetricParams,
25*7e098e93SLukasz Kazmierczak     bool enabled) const
262f9f9b87SWludzik, Jozef {
27d2238194SKrzysztof Grobelny     std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform(
28d2238194SKrzysztof Grobelny         labeledMetricParams,
29d2238194SKrzysztof Grobelny         [this](const LabeledMetricParameters& param)
30d2238194SKrzysztof Grobelny             -> std::shared_ptr<interfaces::Metric> {
31dcc4e193SKrzysztof Grobelny             namespace ts = utils::tstring;
32dcc4e193SKrzysztof Grobelny 
33d2238194SKrzysztof Grobelny             return std::make_shared<Metric>(
34dcc4e193SKrzysztof Grobelny                 getSensors(param.at_label<ts::SensorPath>()),
35dcc4e193SKrzysztof Grobelny                 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(),
36dcc4e193SKrzysztof Grobelny                 param.at_label<ts::MetricMetadata>(),
37dcc4e193SKrzysztof Grobelny                 param.at_label<ts::CollectionTimeScope>(),
388069771cSKrzysztof Grobelny                 param.at_label<ts::CollectionDuration>(),
398069771cSKrzysztof Grobelny                 std::make_unique<Clock>());
40d2238194SKrzysztof Grobelny         });
41c8e3a64aSKrzysztof Grobelny 
422f9f9b87SWludzik, Jozef     return std::make_unique<Report>(
43e2362796SWludzik, Jozef         bus->get_io_context(), objServer, name, reportingType,
44dcc4e193SKrzysztof Grobelny         emitsReadingsSignal, logToMetricReportsCollection, period,
45*7e098e93SLukasz Kazmierczak         reportManager, reportStorage, std::move(metrics), enabled);
462f9f9b87SWludzik, Jozef }
476ccfcbf5SKrzysztof Grobelny 
48dcc4e193SKrzysztof Grobelny Sensors ReportFactory::getSensors(
49dcc4e193SKrzysztof Grobelny     const std::vector<LabeledSensorParameters>& sensorPaths) const
506ccfcbf5SKrzysztof Grobelny {
51d2238194SKrzysztof Grobelny     using namespace utils::tstring;
52d2238194SKrzysztof Grobelny 
53dcc4e193SKrzysztof Grobelny     return utils::transform(sensorPaths,
54dcc4e193SKrzysztof Grobelny                             [this](const LabeledSensorParameters& sensorPath)
55dcc4e193SKrzysztof Grobelny                                 -> std::shared_ptr<interfaces::Sensor> {
56dcc4e193SKrzysztof Grobelny                                 return sensorCache.makeSensor<Sensor>(
57dcc4e193SKrzysztof Grobelny                                     sensorPath.at_label<Service>(),
58e8fc5751SKrzysztof Grobelny                                     sensorPath.at_label<Path>(),
59d2238194SKrzysztof Grobelny                                     bus->get_io_context(), bus);
60dcc4e193SKrzysztof Grobelny                             });
616ccfcbf5SKrzysztof Grobelny }
626ccfcbf5SKrzysztof Grobelny 
63d2238194SKrzysztof Grobelny std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
64d2238194SKrzysztof Grobelny     boost::asio::yield_context& yield,
65d2238194SKrzysztof Grobelny     const ReadingParameters& metricParams) const
666ccfcbf5SKrzysztof Grobelny {
671477fe6aSWludzik, Jozef     auto tree = utils::getSubTreeSensors(yield, bus);
68d2238194SKrzysztof Grobelny 
69d2238194SKrzysztof Grobelny     return utils::transform(metricParams, [&tree](const auto& item) {
70dcc4e193SKrzysztof Grobelny         const auto& [sensorPaths, operationType, id, metadata,
71dcc4e193SKrzysztof Grobelny                      collectionTimeScope, collectionDuration] = item;
72d2238194SKrzysztof Grobelny 
73dcc4e193SKrzysztof Grobelny         std::vector<LabeledSensorParameters> sensorParameters;
74dcc4e193SKrzysztof Grobelny 
75dcc4e193SKrzysztof Grobelny         for (const auto& sensorPath : sensorPaths)
76dcc4e193SKrzysztof Grobelny         {
77d2238194SKrzysztof Grobelny             auto it = std::find_if(
78d2238194SKrzysztof Grobelny                 tree.begin(), tree.end(),
79d2238194SKrzysztof Grobelny                 [&sensorPath](const auto& v) { return v.first == sensorPath; });
80d2238194SKrzysztof Grobelny 
81e8fc5751SKrzysztof Grobelny             if (it != tree.end() && it->second.size() == 1)
82d2238194SKrzysztof Grobelny             {
83e8fc5751SKrzysztof Grobelny                 const auto& [service, ifaces] = it->second.front();
84dcc4e193SKrzysztof Grobelny                 sensorParameters.emplace_back(service, sensorPath);
85dcc4e193SKrzysztof Grobelny             }
86d2238194SKrzysztof Grobelny         }
87d2238194SKrzysztof Grobelny 
88dcc4e193SKrzysztof Grobelny         if (sensorParameters.size() != sensorPaths.size())
89dcc4e193SKrzysztof Grobelny         {
90e8fc5751SKrzysztof Grobelny             throw sdbusplus::exception::SdBusError(
91e8fc5751SKrzysztof Grobelny                 static_cast<int>(std::errc::invalid_argument),
92e8fc5751SKrzysztof Grobelny                 "Could not find service for provided sensors");
93dcc4e193SKrzysztof Grobelny         }
94dcc4e193SKrzysztof Grobelny 
95dcc4e193SKrzysztof Grobelny         return LabeledMetricParameters(
96dcc4e193SKrzysztof Grobelny             std::move(sensorParameters),
97dcc4e193SKrzysztof Grobelny             utils::stringToOperationType(operationType), id, metadata,
98dcc4e193SKrzysztof Grobelny             utils::stringToCollectionTimeScope(collectionTimeScope),
99dcc4e193SKrzysztof Grobelny             CollectionDuration(Milliseconds(collectionDuration)));
100d2238194SKrzysztof Grobelny     });
1016ccfcbf5SKrzysztof Grobelny }
102