blob: 091739c778baa3fdec1c511e955a7c0b03433a2b [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,
24 std::vector<LabeledMetricParameters> labeledMetricParams) const
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020025{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000026 std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform(
27 labeledMetricParams,
28 [this](const LabeledMetricParameters& param)
29 -> std::shared_ptr<interfaces::Metric> {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000030 namespace ts = utils::tstring;
31
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000032 return std::make_shared<Metric>(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000033 getSensors(param.at_label<ts::SensorPath>()),
34 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(),
35 param.at_label<ts::MetricMetadata>(),
36 param.at_label<ts::CollectionTimeScope>(),
Krzysztof Grobelny80697712021-03-04 09:49:27 +000037 param.at_label<ts::CollectionDuration>(),
38 std::make_unique<Clock>());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000039 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020040
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020041 return std::make_unique<Report>(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010042 bus->get_io_context(), objServer, name, reportingType,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000043 emitsReadingsSignal, logToMetricReportsCollection, period,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010044 reportManager, reportStorage, std::move(metrics));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020045}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010046
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000047Sensors ReportFactory::getSensors(
48 const std::vector<LabeledSensorParameters>& sensorPaths) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010049{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000050 using namespace utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010051
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000052 return utils::transform(sensorPaths,
53 [this](const LabeledSensorParameters& sensorPath)
54 -> std::shared_ptr<interfaces::Sensor> {
55 return sensorCache.makeSensor<Sensor>(
56 sensorPath.at_label<Service>(),
57 sensorPath.at_label<Path>(),
58 bus->get_io_context(), bus);
59 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010060}
61
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000062std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
63 boost::asio::yield_context& yield,
64 const ReadingParameters& metricParams) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010065{
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010066 auto tree = utils::getSubTreeSensors(yield, bus);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000067
68 return utils::transform(metricParams, [&tree](const auto& item) {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000069 const auto& [sensorPaths, operationType, id, metadata,
70 collectionTimeScope, collectionDuration] = item;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000071
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000072 std::vector<LabeledSensorParameters> sensorParameters;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000073
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000074 for (const auto& sensorPath : sensorPaths)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000075 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +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)
81 {
82 const auto& [service, ifaces] = it->second.front();
83 sensorParameters.emplace_back(service, sensorPath);
84 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000085 }
86
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000087 if (sensorParameters.size() != sensorPaths.size())
88 {
89 throw sdbusplus::exception::SdBusError(
90 static_cast<int>(std::errc::invalid_argument),
91 "Could not find service for provided sensors");
92 }
93
94 return LabeledMetricParameters(
95 std::move(sensorParameters),
96 utils::stringToOperationType(operationType), id, metadata,
97 utils::stringToCollectionTimeScope(collectionTimeScope),
98 CollectionDuration(Milliseconds(collectionDuration)));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000099 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100100}