xref: /openbmc/telemetry/src/report_factory.cpp (revision 8069771c)
12f9f9b87SWludzik, Jozef #include "report_factory.hpp"
22f9f9b87SWludzik, Jozef 
36ccfcbf5SKrzysztof Grobelny #include "metric.hpp"
42f9f9b87SWludzik, Jozef #include "report.hpp"
5e2362796SWludzik, Jozef #include "sensor.hpp"
6*8069771cSKrzysztof 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,
24d2238194SKrzysztof Grobelny     std::vector<LabeledMetricParameters> labeledMetricParams) const
252f9f9b87SWludzik, Jozef {
26d2238194SKrzysztof Grobelny     std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform(
27d2238194SKrzysztof Grobelny         labeledMetricParams,
28d2238194SKrzysztof Grobelny         [this](const LabeledMetricParameters& param)
29d2238194SKrzysztof Grobelny             -> std::shared_ptr<interfaces::Metric> {
30dcc4e193SKrzysztof Grobelny             namespace ts = utils::tstring;
31dcc4e193SKrzysztof Grobelny 
32d2238194SKrzysztof Grobelny             return std::make_shared<Metric>(
33dcc4e193SKrzysztof Grobelny                 getSensors(param.at_label<ts::SensorPath>()),
34dcc4e193SKrzysztof Grobelny                 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(),
35dcc4e193SKrzysztof Grobelny                 param.at_label<ts::MetricMetadata>(),
36dcc4e193SKrzysztof Grobelny                 param.at_label<ts::CollectionTimeScope>(),
37*8069771cSKrzysztof Grobelny                 param.at_label<ts::CollectionDuration>(),
38*8069771cSKrzysztof Grobelny                 std::make_unique<Clock>());
39d2238194SKrzysztof Grobelny         });
40c8e3a64aSKrzysztof Grobelny 
412f9f9b87SWludzik, Jozef     return std::make_unique<Report>(
42e2362796SWludzik, Jozef         bus->get_io_context(), objServer, name, reportingType,
43dcc4e193SKrzysztof Grobelny         emitsReadingsSignal, logToMetricReportsCollection, period,
44e2362796SWludzik, Jozef         reportManager, reportStorage, std::move(metrics));
452f9f9b87SWludzik, Jozef }
466ccfcbf5SKrzysztof Grobelny 
47dcc4e193SKrzysztof Grobelny Sensors ReportFactory::getSensors(
48dcc4e193SKrzysztof Grobelny     const std::vector<LabeledSensorParameters>& sensorPaths) const
496ccfcbf5SKrzysztof Grobelny {
50d2238194SKrzysztof Grobelny     using namespace utils::tstring;
51d2238194SKrzysztof Grobelny 
52dcc4e193SKrzysztof Grobelny     return utils::transform(sensorPaths,
53dcc4e193SKrzysztof Grobelny                             [this](const LabeledSensorParameters& sensorPath)
54dcc4e193SKrzysztof Grobelny                                 -> std::shared_ptr<interfaces::Sensor> {
55dcc4e193SKrzysztof Grobelny                                 return sensorCache.makeSensor<Sensor>(
56dcc4e193SKrzysztof Grobelny                                     sensorPath.at_label<Service>(),
57e8fc5751SKrzysztof Grobelny                                     sensorPath.at_label<Path>(),
58d2238194SKrzysztof Grobelny                                     bus->get_io_context(), bus);
59dcc4e193SKrzysztof Grobelny                             });
606ccfcbf5SKrzysztof Grobelny }
616ccfcbf5SKrzysztof Grobelny 
62d2238194SKrzysztof Grobelny std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
63d2238194SKrzysztof Grobelny     boost::asio::yield_context& yield,
64d2238194SKrzysztof Grobelny     const ReadingParameters& metricParams) const
656ccfcbf5SKrzysztof Grobelny {
661477fe6aSWludzik, Jozef     auto tree = utils::getSubTreeSensors(yield, bus);
67d2238194SKrzysztof Grobelny 
68d2238194SKrzysztof Grobelny     return utils::transform(metricParams, [&tree](const auto& item) {
69dcc4e193SKrzysztof Grobelny         const auto& [sensorPaths, operationType, id, metadata,
70dcc4e193SKrzysztof Grobelny                      collectionTimeScope, collectionDuration] = item;
71d2238194SKrzysztof Grobelny 
72dcc4e193SKrzysztof Grobelny         std::vector<LabeledSensorParameters> sensorParameters;
73dcc4e193SKrzysztof Grobelny 
74dcc4e193SKrzysztof Grobelny         for (const auto& sensorPath : sensorPaths)
75dcc4e193SKrzysztof Grobelny         {
76d2238194SKrzysztof Grobelny             auto it = std::find_if(
77d2238194SKrzysztof Grobelny                 tree.begin(), tree.end(),
78d2238194SKrzysztof Grobelny                 [&sensorPath](const auto& v) { return v.first == sensorPath; });
79d2238194SKrzysztof Grobelny 
80e8fc5751SKrzysztof Grobelny             if (it != tree.end() && it->second.size() == 1)
81d2238194SKrzysztof Grobelny             {
82e8fc5751SKrzysztof Grobelny                 const auto& [service, ifaces] = it->second.front();
83dcc4e193SKrzysztof Grobelny                 sensorParameters.emplace_back(service, sensorPath);
84dcc4e193SKrzysztof Grobelny             }
85d2238194SKrzysztof Grobelny         }
86d2238194SKrzysztof Grobelny 
87dcc4e193SKrzysztof Grobelny         if (sensorParameters.size() != sensorPaths.size())
88dcc4e193SKrzysztof Grobelny         {
89e8fc5751SKrzysztof Grobelny             throw sdbusplus::exception::SdBusError(
90e8fc5751SKrzysztof Grobelny                 static_cast<int>(std::errc::invalid_argument),
91e8fc5751SKrzysztof Grobelny                 "Could not find service for provided sensors");
92dcc4e193SKrzysztof Grobelny         }
93dcc4e193SKrzysztof Grobelny 
94dcc4e193SKrzysztof Grobelny         return LabeledMetricParameters(
95dcc4e193SKrzysztof Grobelny             std::move(sensorParameters),
96dcc4e193SKrzysztof Grobelny             utils::stringToOperationType(operationType), id, metadata,
97dcc4e193SKrzysztof Grobelny             utils::stringToCollectionTimeScope(collectionTimeScope),
98dcc4e193SKrzysztof Grobelny             CollectionDuration(Milliseconds(collectionDuration)));
99d2238194SKrzysztof Grobelny     });
1006ccfcbf5SKrzysztof Grobelny }
101