blob: 3d86521203bc821a506c0c3fa6ea41f4a33508b9 [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 Grobelny80697712021-03-04 09:49:27 +00006#include "utils/clock.hpp"
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +00007#include "utils/conversion.hpp"
Wludzik, Jozef1477fe62021-01-02 11:56:10 +01008#include "utils/dbus_mapper.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01009#include "utils/transform.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020010
11ReportFactory::ReportFactory(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010012 std::shared_ptr<sdbusplus::asio::connection> bus,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010013 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
14 SensorCache& sensorCache) :
Wludzik, Jozefe2362792020-10-27 17:23:55 +010015 bus(std::move(bus)),
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010016 objServer(objServer), sensorCache(sensorCache)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020017{}
18
19std::unique_ptr<interfaces::Report> ReportFactory::make(
20 const std::string& name, const std::string& reportingType,
21 bool emitsReadingsSignal, bool logToMetricReportsCollection,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000022 Milliseconds period, interfaces::ReportManager& reportManager,
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000023 interfaces::JsonStorage& reportStorage,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020024 std::vector<LabeledMetricParameters> labeledMetricParams,
25 bool enabled) const
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020026{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000027 std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform(
28 labeledMetricParams,
29 [this](const LabeledMetricParameters& param)
30 -> std::shared_ptr<interfaces::Metric> {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000031 namespace ts = utils::tstring;
32
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000033 return std::make_shared<Metric>(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000034 getSensors(param.at_label<ts::SensorPath>()),
35 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(),
36 param.at_label<ts::MetricMetadata>(),
37 param.at_label<ts::CollectionTimeScope>(),
Krzysztof Grobelny80697712021-03-04 09:49:27 +000038 param.at_label<ts::CollectionDuration>(),
39 std::make_unique<Clock>());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000040 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020041
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020042 return std::make_unique<Report>(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010043 bus->get_io_context(), objServer, name, reportingType,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000044 emitsReadingsSignal, logToMetricReportsCollection, period,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020045 reportManager, reportStorage, std::move(metrics), enabled);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020046}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010047
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000048Sensors ReportFactory::getSensors(
49 const std::vector<LabeledSensorParameters>& sensorPaths) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010050{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000051 using namespace utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010052
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000053 return utils::transform(sensorPaths,
54 [this](const LabeledSensorParameters& sensorPath)
55 -> std::shared_ptr<interfaces::Sensor> {
56 return sensorCache.makeSensor<Sensor>(
57 sensorPath.at_label<Service>(),
58 sensorPath.at_label<Path>(),
59 bus->get_io_context(), bus);
60 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010061}
62
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000063std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
64 boost::asio::yield_context& yield,
65 const ReadingParameters& metricParams) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010066{
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010067 auto tree = utils::getSubTreeSensors(yield, bus);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000068
69 return utils::transform(metricParams, [&tree](const auto& item) {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000070 const auto& [sensorPaths, operationType, id, metadata,
71 collectionTimeScope, collectionDuration] = item;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000072
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000073 std::vector<LabeledSensorParameters> sensorParameters;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000074
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000075 for (const auto& sensorPath : sensorPaths)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000076 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000077 auto it = std::find_if(
78 tree.begin(), tree.end(),
79 [&sensorPath](const auto& v) { return v.first == sensorPath; });
80
81 if (it != tree.end() && it->second.size() == 1)
82 {
83 const auto& [service, ifaces] = it->second.front();
84 sensorParameters.emplace_back(service, sensorPath);
85 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000086 }
87
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000088 if (sensorParameters.size() != sensorPaths.size())
89 {
90 throw sdbusplus::exception::SdBusError(
91 static_cast<int>(std::errc::invalid_argument),
92 "Could not find service for provided sensors");
93 }
94
95 return LabeledMetricParameters(
96 std::move(sensorParameters),
97 utils::stringToOperationType(operationType), id, metadata,
98 utils::stringToCollectionTimeScope(collectionTimeScope),
99 CollectionDuration(Milliseconds(collectionDuration)));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000100 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100101}