blob: 0e598f30762dbbdf2cbe71dc9d3641f013a52955 [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"
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +00006#include "utils/conversion.hpp"
Wludzik, Jozef1477fe62021-01-02 11:56:10 +01007#include "utils/dbus_mapper.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01008#include "utils/transform.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02009
10ReportFactory::ReportFactory(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010011 std::shared_ptr<sdbusplus::asio::connection> bus,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010012 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
13 SensorCache& sensorCache) :
Wludzik, Jozefe2362792020-10-27 17:23:55 +010014 bus(std::move(bus)),
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010015 objServer(objServer), sensorCache(sensorCache)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020016{}
17
18std::unique_ptr<interfaces::Report> ReportFactory::make(
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000019 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
32std::unique_ptr<interfaces::Report> ReportFactory::make(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020033 const std::string& name, const std::string& reportingType,
34 bool emitsReadingsSignal, bool logToMetricReportsCollection,
35 std::chrono::milliseconds period, const ReadingParameters& metricParams,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010036 interfaces::ReportManager& reportManager,
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000037 interfaces::JsonStorage& reportStorage,
38 std::vector<LabeledMetricParameters> labeledMetricParams) const
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020039{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000040 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>(
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000045 getSensor(param.at_index<0>()), param.at_index<1>(),
Krzysztof Grobelny753e4b32021-02-11 12:58:58 +000046 param.at_index<2>(), param.at_index<3>(), param.at_index<4>(),
47 param.at_index<5>());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000048 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020049
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020050 return std::make_unique<Report>(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010051 bus->get_io_context(), objServer, name, reportingType,
52 emitsReadingsSignal, logToMetricReportsCollection, period, metricParams,
53 reportManager, reportStorage, std::move(metrics));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020054}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010055
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000056std::shared_ptr<interfaces::Sensor>
57 ReportFactory::getSensor(const LabeledSensorParameters& sensorPath) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010058{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000059 using namespace utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010060
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000061 return sensorCache.makeSensor<Sensor>(sensorPath.at_label<Service>(),
62 sensorPath.at_label<Path>(),
63 bus->get_io_context(), bus);
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010064}
65
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000066std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
67 boost::asio::yield_context& yield,
68 const ReadingParameters& metricParams) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010069{
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010070 auto tree = utils::getSubTreeSensors(yield, bus);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000071
72 return utils::transform(metricParams, [&tree](const auto& item) {
Krzysztof Grobelny753e4b32021-02-11 12:58:58 +000073 const auto& [sensorPath, operationType, id, metadata,
74 collectionTimeScope, collectionDuration] = item;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000075
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000076 auto it = std::find_if(
77 tree.begin(), tree.end(),
78 [&sensorPath](const auto& v) { return v.first == sensorPath; });
79
80 if (it != tree.end() && it->second.size() == 1)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000081 {
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000082 const auto& [service, ifaces] = it->second.front();
83 return LabeledMetricParameters(
84 LabeledSensorParameters(service, sensorPath),
Krzysztof Grobelny753e4b32021-02-11 12:58:58 +000085 utils::stringToOperationType(operationType), id, metadata,
86 utils::stringToCollectionTimeScope(collectionTimeScope),
87 CollectionDuration(
88 std::chrono::milliseconds(collectionDuration)));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000089 }
90
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000091 throw sdbusplus::exception::SdBusError(
92 static_cast<int>(std::errc::invalid_argument),
93 "Could not find service for provided sensors");
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000094 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010095}