blob: 2cd5da374bf8bbf6e713fc7b46610de1959c66cf [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"
Wludzik, Jozef1477fe62021-01-02 11:56:10 +01006#include "utils/dbus_mapper.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01007#include "utils/transform.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02008
9ReportFactory::ReportFactory(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010010 std::shared_ptr<sdbusplus::asio::connection> bus,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010011 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
12 SensorCache& sensorCache) :
Wludzik, Jozefe2362792020-10-27 17:23:55 +010013 bus(std::move(bus)),
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010014 objServer(objServer), sensorCache(sensorCache)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020015{}
16
17std::unique_ptr<interfaces::Report> ReportFactory::make(
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000018 boost::asio::yield_context& yield, const std::string& name,
19 const std::string& reportingType, bool emitsReadingsSignal,
20 bool logToMetricReportsCollection, std::chrono::milliseconds period,
21 const ReadingParameters& metricParams,
22 interfaces::ReportManager& reportManager,
23 interfaces::JsonStorage& reportStorage) const
24{
25 return make(name, reportingType, emitsReadingsSignal,
26 logToMetricReportsCollection, period, metricParams,
27 reportManager, reportStorage,
28 convertMetricParams(yield, metricParams));
29}
30
31std::unique_ptr<interfaces::Report> ReportFactory::make(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020032 const std::string& name, const std::string& reportingType,
33 bool emitsReadingsSignal, bool logToMetricReportsCollection,
34 std::chrono::milliseconds period, const ReadingParameters& metricParams,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010035 interfaces::ReportManager& reportManager,
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000036 interfaces::JsonStorage& reportStorage,
37 std::vector<LabeledMetricParameters> labeledMetricParams) const
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020038{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000039 std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform(
40 labeledMetricParams,
41 [this](const LabeledMetricParameters& param)
42 -> std::shared_ptr<interfaces::Metric> {
43 return std::make_shared<Metric>(
44 getSensors(param.at_index<0>()), param.at_index<1>(),
45 param.at_index<2>(), param.at_index<3>());
46 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020047
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020048 return std::make_unique<Report>(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010049 bus->get_io_context(), objServer, name, reportingType,
50 emitsReadingsSignal, logToMetricReportsCollection, period, metricParams,
51 reportManager, reportStorage, std::move(metrics));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020052}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010053
54std::vector<std::shared_ptr<interfaces::Sensor>> ReportFactory::getSensors(
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000055 const std::vector<LabeledSensorParameters>& sensorPaths) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010056{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000057 return utils::transform(sensorPaths,
58 [this](const LabeledSensorParameters& param)
59 -> std::shared_ptr<interfaces::Sensor> {
60 using namespace utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010061
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000062 return sensorCache.makeSensor<Sensor>(
63 param.at_label<Service>(),
64 param.at_label<Path>(),
65 bus->get_io_context(), bus);
66 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010067}
68
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000069std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
70 boost::asio::yield_context& yield,
71 const ReadingParameters& metricParams) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010072{
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010073 auto tree = utils::getSubTreeSensors(yield, bus);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000074
75 return utils::transform(metricParams, [&tree](const auto& item) {
76 std::vector<LabeledSensorParameters> sensors;
77
78 for (const auto& sensorPath : std::get<0>(item))
79 {
80 auto it = std::find_if(
81 tree.begin(), tree.end(),
82 [&sensorPath](const auto& v) { return v.first == sensorPath; });
83
84 if (it != tree.end())
85 {
86 for (const auto& [service, ifaces] : it->second)
87 {
88 sensors.emplace_back(service, sensorPath);
89 }
90 }
91 }
92
93 return LabeledMetricParameters(std::move(sensors), std::get<1>(item),
94 std::get<2>(item), std::get<3>(item));
95 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010096}