blob: b8b551828dff84c419ed7bc841060e1946cc4c90 [file] [log] [blame]
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02001#include "report_factory.hpp"
2
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01003#include "metric.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02004#include "report.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01005#include "sensor.hpp"
6#include "utils/transform.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02007
8ReportFactory::ReportFactory(
Wludzik, Jozefe2362792020-10-27 17:23:55 +01009 std::shared_ptr<sdbusplus::asio::connection> bus,
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020010 const std::shared_ptr<sdbusplus::asio::object_server>& objServer) :
Wludzik, Jozefe2362792020-10-27 17:23:55 +010011 bus(std::move(bus)),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020012 objServer(objServer)
13{}
14
15std::unique_ptr<interfaces::Report> ReportFactory::make(
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000016 boost::asio::yield_context& yield, const std::string& name,
17 const std::string& reportingType, bool emitsReadingsSignal,
18 bool logToMetricReportsCollection, std::chrono::milliseconds period,
19 const ReadingParameters& metricParams,
20 interfaces::ReportManager& reportManager,
21 interfaces::JsonStorage& reportStorage) const
22{
23 return make(name, reportingType, emitsReadingsSignal,
24 logToMetricReportsCollection, period, metricParams,
25 reportManager, reportStorage,
26 convertMetricParams(yield, metricParams));
27}
28
29std::unique_ptr<interfaces::Report> ReportFactory::make(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020030 const std::string& name, const std::string& reportingType,
31 bool emitsReadingsSignal, bool logToMetricReportsCollection,
32 std::chrono::milliseconds period, const ReadingParameters& metricParams,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010033 interfaces::ReportManager& reportManager,
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000034 interfaces::JsonStorage& reportStorage,
35 std::vector<LabeledMetricParameters> labeledMetricParams) const
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020036{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000037 std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform(
38 labeledMetricParams,
39 [this](const LabeledMetricParameters& param)
40 -> std::shared_ptr<interfaces::Metric> {
41 return std::make_shared<Metric>(
42 getSensors(param.at_index<0>()), param.at_index<1>(),
43 param.at_index<2>(), param.at_index<3>());
44 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020045
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020046 return std::make_unique<Report>(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010047 bus->get_io_context(), objServer, name, reportingType,
48 emitsReadingsSignal, logToMetricReportsCollection, period, metricParams,
49 reportManager, reportStorage, std::move(metrics));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020050}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010051
52std::vector<std::shared_ptr<interfaces::Sensor>> ReportFactory::getSensors(
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000053 const std::vector<LabeledSensorParameters>& sensorPaths) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010054{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000055 return utils::transform(sensorPaths,
56 [this](const LabeledSensorParameters& param)
57 -> std::shared_ptr<interfaces::Sensor> {
58 using namespace utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010059
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000060 return sensorCache.makeSensor<Sensor>(
61 param.at_label<Service>(),
62 param.at_label<Path>(),
63 bus->get_io_context(), bus);
64 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010065}
66
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000067std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
68 boost::asio::yield_context& yield,
69 const ReadingParameters& metricParams) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010070{
71 std::array<const char*, 1> interfaces = {
72 "xyz.openbmc_project.Sensor.Value"};
73 boost::system::error_code ec;
74
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000075 auto tree = bus->yield_method_call<std::vector<SensorTree>>(
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010076 yield, ec, "xyz.openbmc_project.ObjectMapper",
77 "/xyz/openbmc_project/object_mapper",
78 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
79 "/xyz/openbmc_project/sensors", 2, interfaces);
80 if (ec)
81 {
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000082 throw std::runtime_error("Failed to query ObjectMapper!");
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010083 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000084
85 return utils::transform(metricParams, [&tree](const auto& item) {
86 std::vector<LabeledSensorParameters> sensors;
87
88 for (const auto& sensorPath : std::get<0>(item))
89 {
90 auto it = std::find_if(
91 tree.begin(), tree.end(),
92 [&sensorPath](const auto& v) { return v.first == sensorPath; });
93
94 if (it != tree.end())
95 {
96 for (const auto& [service, ifaces] : it->second)
97 {
98 sensors.emplace_back(service, sensorPath);
99 }
100 }
101 }
102
103 return LabeledMetricParameters(std::move(sensors), std::get<1>(item),
104 std::get<2>(item), std::get<3>(item));
105 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100106}