blob: fb6edf9bd714a1d18a62d8e9a0d424baca50bb4b [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(
19 const std::string& name, const std::string& reportingType,
20 bool emitsReadingsSignal, bool logToMetricReportsCollection,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000021 Milliseconds period, interfaces::ReportManager& reportManager,
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000022 interfaces::JsonStorage& reportStorage,
23 std::vector<LabeledMetricParameters> labeledMetricParams) const
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020024{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000025 std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform(
26 labeledMetricParams,
27 [this](const LabeledMetricParameters& param)
28 -> std::shared_ptr<interfaces::Metric> {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000029 namespace ts = utils::tstring;
30
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000031 return std::make_shared<Metric>(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000032 getSensors(param.at_label<ts::SensorPath>()),
33 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(),
34 param.at_label<ts::MetricMetadata>(),
35 param.at_label<ts::CollectionTimeScope>(),
36 param.at_label<ts::CollectionDuration>());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000037 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020038
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020039 return std::make_unique<Report>(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010040 bus->get_io_context(), objServer, name, reportingType,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000041 emitsReadingsSignal, logToMetricReportsCollection, period,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010042 reportManager, reportStorage, std::move(metrics));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020043}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010044
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000045Sensors ReportFactory::getSensors(
46 const std::vector<LabeledSensorParameters>& sensorPaths) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010047{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000048 using namespace utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010049
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000050 return utils::transform(sensorPaths,
51 [this](const LabeledSensorParameters& sensorPath)
52 -> std::shared_ptr<interfaces::Sensor> {
53 return sensorCache.makeSensor<Sensor>(
54 sensorPath.at_label<Service>(),
55 sensorPath.at_label<Path>(),
56 bus->get_io_context(), bus);
57 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010058}
59
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000060std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
61 boost::asio::yield_context& yield,
62 const ReadingParameters& metricParams) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010063{
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010064 auto tree = utils::getSubTreeSensors(yield, bus);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000065
66 return utils::transform(metricParams, [&tree](const auto& item) {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000067 const auto& [sensorPaths, operationType, id, metadata,
68 collectionTimeScope, collectionDuration] = item;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000069
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000070 std::vector<LabeledSensorParameters> sensorParameters;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000071
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000072 for (const auto& sensorPath : sensorPaths)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000073 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +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)
79 {
80 const auto& [service, ifaces] = it->second.front();
81 sensorParameters.emplace_back(service, sensorPath);
82 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000083 }
84
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000085 if (sensorParameters.size() != sensorPaths.size())
86 {
87 throw sdbusplus::exception::SdBusError(
88 static_cast<int>(std::errc::invalid_argument),
89 "Could not find service for provided sensors");
90 }
91
92 return LabeledMetricParameters(
93 std::move(sensorParameters),
94 utils::stringToOperationType(operationType), id, metadata,
95 utils::stringToCollectionTimeScope(collectionTimeScope),
96 CollectionDuration(Milliseconds(collectionDuration)));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000097 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010098}