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