blob: 9dfdbb07b680ee594e0b494cd02a784bc8c2caf6 [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 Grobelnyd2238192020-12-02 09:27:28 +000046 param.at_index<2>(), param.at_index<3>());
47 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020048
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020049 return std::make_unique<Report>(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010050 bus->get_io_context(), objServer, name, reportingType,
51 emitsReadingsSignal, logToMetricReportsCollection, period, metricParams,
52 reportManager, reportStorage, std::move(metrics));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020053}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010054
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000055std::shared_ptr<interfaces::Sensor>
56 ReportFactory::getSensor(const LabeledSensorParameters& sensorPath) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010057{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000058 using namespace utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010059
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000060 return sensorCache.makeSensor<Sensor>(sensorPath.at_label<Service>(),
61 sensorPath.at_label<Path>(),
62 bus->get_io_context(), bus);
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010063}
64
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000065std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
66 boost::asio::yield_context& yield,
67 const ReadingParameters& metricParams) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010068{
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010069 auto tree = utils::getSubTreeSensors(yield, bus);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000070
71 return utils::transform(metricParams, [&tree](const auto& item) {
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000072 const auto& [sensorPath, operationType, id, metadata] = item;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000073
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000074 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)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000079 {
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000080 const auto& [service, ifaces] = it->second.front();
81 return LabeledMetricParameters(
82 LabeledSensorParameters(service, sensorPath),
83 utils::stringToOperationType(operationType), id, metadata);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000084 }
85
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000086 throw sdbusplus::exception::SdBusError(
87 static_cast<int>(std::errc::invalid_argument),
88 "Could not find service for provided sensors");
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000089 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010090}