xref: /openbmc/telemetry/src/report_factory.cpp (revision 60fee077)
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(
20b8cc78ddSKrzysztof Grobelny     const std::string& id, const std::string& name,
21b8cc78ddSKrzysztof Grobelny     const ReportingType reportingType,
2251497a0cSKrzysztof Grobelny     const std::vector<ReportAction>& reportActions, Milliseconds period,
2351497a0cSKrzysztof Grobelny     uint64_t appendLimit, const ReportUpdates reportUpdates,
243eb56865SSzymon Dompke     interfaces::ReportManager& reportManager,
25d2238194SKrzysztof Grobelny     interfaces::JsonStorage& reportStorage,
26e6d48874SKrzysztof Grobelny     std::vector<LabeledMetricParameters> labeledMetricParams,
27e6d48874SKrzysztof Grobelny     bool enabled) const
282f9f9b87SWludzik, Jozef {
29d2238194SKrzysztof Grobelny     std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform(
30d2238194SKrzysztof Grobelny         labeledMetricParams,
31d2238194SKrzysztof Grobelny         [this](const LabeledMetricParameters& param)
32d2238194SKrzysztof Grobelny             -> std::shared_ptr<interfaces::Metric> {
33dcc4e193SKrzysztof Grobelny             namespace ts = utils::tstring;
34dcc4e193SKrzysztof Grobelny 
35d2238194SKrzysztof Grobelny             return std::make_shared<Metric>(
36dcc4e193SKrzysztof Grobelny                 getSensors(param.at_label<ts::SensorPath>()),
37dcc4e193SKrzysztof Grobelny                 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(),
38dcc4e193SKrzysztof Grobelny                 param.at_label<ts::CollectionTimeScope>(),
398069771cSKrzysztof Grobelny                 param.at_label<ts::CollectionDuration>(),
408069771cSKrzysztof Grobelny                 std::make_unique<Clock>());
41d2238194SKrzysztof Grobelny         });
42c8e3a64aSKrzysztof Grobelny 
43e6d48874SKrzysztof Grobelny     return std::make_unique<Report>(
44e6d48874SKrzysztof Grobelny         bus->get_io_context(), objServer, id, name, reportingType,
45e6d48874SKrzysztof Grobelny         reportActions, period, appendLimit, reportUpdates, reportManager,
46e6d48874SKrzysztof Grobelny         reportStorage, std::move(metrics), enabled, std::make_unique<Clock>());
472f9f9b87SWludzik, Jozef }
486ccfcbf5SKrzysztof Grobelny 
49dcc4e193SKrzysztof Grobelny Sensors ReportFactory::getSensors(
5094f71c51SSzymon Dompke     const std::vector<LabeledSensorInfo>& sensorPaths) const
516ccfcbf5SKrzysztof Grobelny {
52d2238194SKrzysztof Grobelny     using namespace utils::tstring;
53d2238194SKrzysztof Grobelny 
54b8cc78ddSKrzysztof Grobelny     return utils::transform(
55b8cc78ddSKrzysztof Grobelny         sensorPaths,
5694f71c51SSzymon Dompke         [this](const LabeledSensorInfo& sensorPath)
57dcc4e193SKrzysztof Grobelny             -> std::shared_ptr<interfaces::Sensor> {
58dcc4e193SKrzysztof Grobelny             return sensorCache.makeSensor<Sensor>(
59b8cc78ddSKrzysztof Grobelny                 sensorPath.at_label<Service>(), sensorPath.at_label<Path>(),
60b8cc78ddSKrzysztof Grobelny                 sensorPath.at_label<Metadata>(), bus->get_io_context(), bus);
61dcc4e193SKrzysztof Grobelny         });
626ccfcbf5SKrzysztof Grobelny }
636ccfcbf5SKrzysztof Grobelny 
64d2238194SKrzysztof Grobelny std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
65d2238194SKrzysztof Grobelny     boost::asio::yield_context& yield,
66d2238194SKrzysztof Grobelny     const ReadingParameters& metricParams) const
676ccfcbf5SKrzysztof Grobelny {
681477fe6aSWludzik, Jozef     auto tree = utils::getSubTreeSensors(yield, bus);
69d2238194SKrzysztof Grobelny 
70d2238194SKrzysztof Grobelny     return utils::transform(metricParams, [&tree](const auto& item) {
71b8cc78ddSKrzysztof Grobelny         auto [sensorPaths, operationType, id, collectionTimeScope,
72b8cc78ddSKrzysztof Grobelny               collectionDuration] = item;
73d2238194SKrzysztof Grobelny 
7494f71c51SSzymon Dompke         std::vector<LabeledSensorInfo> sensorParameters;
75dcc4e193SKrzysztof Grobelny 
76b8cc78ddSKrzysztof Grobelny         for (const auto& [sensorPath, metadata] : sensorPaths)
77dcc4e193SKrzysztof Grobelny         {
78d2238194SKrzysztof Grobelny             auto it = std::find_if(
79d2238194SKrzysztof Grobelny                 tree.begin(), tree.end(),
80fbeb5bf4SKrzysztof Grobelny                 [path = sensorPath](const auto& v) { return v.first == path; });
81d2238194SKrzysztof Grobelny 
82e8fc5751SKrzysztof Grobelny             if (it != tree.end() && it->second.size() == 1)
83d2238194SKrzysztof Grobelny             {
84e8fc5751SKrzysztof Grobelny                 const auto& [service, ifaces] = it->second.front();
85b8cc78ddSKrzysztof Grobelny                 sensorParameters.emplace_back(service, sensorPath, metadata);
86dcc4e193SKrzysztof Grobelny             }
87d2238194SKrzysztof Grobelny         }
88d2238194SKrzysztof Grobelny 
89dcc4e193SKrzysztof Grobelny         if (sensorParameters.size() != sensorPaths.size())
90dcc4e193SKrzysztof Grobelny         {
91e8fc5751SKrzysztof Grobelny             throw sdbusplus::exception::SdBusError(
92e8fc5751SKrzysztof Grobelny                 static_cast<int>(std::errc::invalid_argument),
93e8fc5751SKrzysztof Grobelny                 "Could not find service for provided sensors");
94dcc4e193SKrzysztof Grobelny         }
95dcc4e193SKrzysztof Grobelny 
96b8cc78ddSKrzysztof Grobelny         if (operationType.empty())
97b8cc78ddSKrzysztof Grobelny         {
98b8cc78ddSKrzysztof Grobelny             operationType = utils::enumToString(OperationType::avg);
99b8cc78ddSKrzysztof Grobelny         }
100*60fee077SKrzysztof Grobelny         else if (operationType == "SINGLE")
101*60fee077SKrzysztof Grobelny         {
102*60fee077SKrzysztof Grobelny             operationType = utils::enumToString(OperationType::avg);
103*60fee077SKrzysztof Grobelny             collectionTimeScope =
104*60fee077SKrzysztof Grobelny                 utils::enumToString(CollectionTimeScope::point);
105*60fee077SKrzysztof Grobelny         }
106b8cc78ddSKrzysztof Grobelny 
107b8cc78ddSKrzysztof Grobelny         if (collectionTimeScope.empty())
108b8cc78ddSKrzysztof Grobelny         {
109b8cc78ddSKrzysztof Grobelny             collectionTimeScope =
110b8cc78ddSKrzysztof Grobelny                 utils::enumToString(CollectionTimeScope::point);
111b8cc78ddSKrzysztof Grobelny         }
112b8cc78ddSKrzysztof Grobelny 
113dcc4e193SKrzysztof Grobelny         return LabeledMetricParameters(
11451497a0cSKrzysztof Grobelny             std::move(sensorParameters), utils::toOperationType(operationType),
115b8cc78ddSKrzysztof Grobelny             id, utils::toCollectionTimeScope(collectionTimeScope),
116dcc4e193SKrzysztof Grobelny             CollectionDuration(Milliseconds(collectionDuration)));
117d2238194SKrzysztof Grobelny     });
1186ccfcbf5SKrzysztof Grobelny }
119