xref: /openbmc/telemetry/src/report_factory.cpp (revision 3eb56865714cd591b24d2bdef83307508aa072c5)
1 #include "report_factory.hpp"
2 
3 #include "metric.hpp"
4 #include "report.hpp"
5 #include "sensor.hpp"
6 #include "utils/clock.hpp"
7 #include "utils/conversion.hpp"
8 #include "utils/dbus_mapper.hpp"
9 #include "utils/transform.hpp"
10 
11 ReportFactory::ReportFactory(
12     std::shared_ptr<sdbusplus::asio::connection> bus,
13     const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
14     SensorCache& sensorCache) :
15     bus(std::move(bus)),
16     objServer(objServer), sensorCache(sensorCache)
17 {}
18 
19 std::unique_ptr<interfaces::Report> ReportFactory::make(
20     const std::string& name, const std::string& reportingTypeStr,
21     bool emitsReadingsSignal, bool logToMetricReportsCollection,
22     Milliseconds period, uint64_t appendLimit,
23     const std::string& reportUpdatesStr,
24     interfaces::ReportManager& reportManager,
25     interfaces::JsonStorage& reportStorage,
26     std::vector<LabeledMetricParameters> labeledMetricParams,
27     bool enabled) const
28 {
29     std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform(
30         labeledMetricParams,
31         [this](const LabeledMetricParameters& param)
32             -> std::shared_ptr<interfaces::Metric> {
33             namespace ts = utils::tstring;
34 
35             return std::make_shared<Metric>(
36                 getSensors(param.at_label<ts::SensorPath>()),
37                 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(),
38                 param.at_label<ts::MetricMetadata>(),
39                 param.at_label<ts::CollectionTimeScope>(),
40                 param.at_label<ts::CollectionDuration>(),
41                 std::make_unique<Clock>());
42         });
43 
44     const ReportingType reportingType = stringToReportingType(reportingTypeStr);
45     const ReportUpdates reportUpdates = stringToReportUpdates(reportUpdatesStr);
46 
47     return std::make_unique<Report>(bus->get_io_context(), objServer, name,
48                                     reportingType, emitsReadingsSignal,
49                                     logToMetricReportsCollection, period,
50                                     appendLimit, reportUpdates, reportManager,
51                                     reportStorage, std::move(metrics), enabled);
52 }
53 
54 Sensors ReportFactory::getSensors(
55     const std::vector<LabeledSensorParameters>& sensorPaths) const
56 {
57     using namespace utils::tstring;
58 
59     return utils::transform(sensorPaths,
60                             [this](const LabeledSensorParameters& sensorPath)
61                                 -> std::shared_ptr<interfaces::Sensor> {
62                                 return sensorCache.makeSensor<Sensor>(
63                                     sensorPath.at_label<Service>(),
64                                     sensorPath.at_label<Path>(),
65                                     bus->get_io_context(), bus);
66                             });
67 }
68 
69 std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
70     boost::asio::yield_context& yield,
71     const ReadingParameters& metricParams) const
72 {
73     auto tree = utils::getSubTreeSensors(yield, bus);
74 
75     return utils::transform(metricParams, [&tree](const auto& item) {
76         const auto& [sensorPaths, operationType, id, metadata,
77                      collectionTimeScope, collectionDuration] = item;
78 
79         std::vector<LabeledSensorParameters> sensorParameters;
80 
81         for (const auto& sensorPath : sensorPaths)
82         {
83             auto it = std::find_if(
84                 tree.begin(), tree.end(),
85                 [&sensorPath](const auto& v) { return v.first == sensorPath; });
86 
87             if (it != tree.end() && it->second.size() == 1)
88             {
89                 const auto& [service, ifaces] = it->second.front();
90                 sensorParameters.emplace_back(service, sensorPath);
91             }
92         }
93 
94         if (sensorParameters.size() != sensorPaths.size())
95         {
96             throw sdbusplus::exception::SdBusError(
97                 static_cast<int>(std::errc::invalid_argument),
98                 "Could not find service for provided sensors");
99         }
100 
101         return LabeledMetricParameters(
102             std::move(sensorParameters),
103             utils::stringToOperationType(operationType), id, metadata,
104             utils::stringToCollectionTimeScope(collectionTimeScope),
105             CollectionDuration(Milliseconds(collectionDuration)));
106     });
107 }
108