blob: 6c61cbe78eb7ba623f04626d3601e627be42259f [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(
Szymon Dompke3eb56862021-09-20 15:32:04 +020020 const std::string& name, const std::string& reportingTypeStr,
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020021 bool emitsReadingsSignal, bool logToMetricReportsCollection,
Szymon Dompke3eb56862021-09-20 15:32:04 +020022 Milliseconds period, uint64_t appendLimit,
23 const std::string& reportUpdatesStr,
24 interfaces::ReportManager& reportManager,
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000025 interfaces::JsonStorage& reportStorage,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020026 std::vector<LabeledMetricParameters> labeledMetricParams,
27 bool enabled) const
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020028{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000029 std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform(
30 labeledMetricParams,
31 [this](const LabeledMetricParameters& param)
32 -> std::shared_ptr<interfaces::Metric> {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000033 namespace ts = utils::tstring;
34
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000035 return std::make_shared<Metric>(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000036 getSensors(param.at_label<ts::SensorPath>()),
37 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(),
38 param.at_label<ts::MetricMetadata>(),
39 param.at_label<ts::CollectionTimeScope>(),
Krzysztof Grobelny80697712021-03-04 09:49:27 +000040 param.at_label<ts::CollectionDuration>(),
41 std::make_unique<Clock>());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000042 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020043
Szymon Dompke3eb56862021-09-20 15:32:04 +020044 const ReportingType reportingType = stringToReportingType(reportingTypeStr);
45 const ReportUpdates reportUpdates = stringToReportUpdates(reportUpdatesStr);
46
47 return std::make_unique<Report>(bus->get_io_context(), objServer, name,
48 reportingType, emitsReadingsSignal,
49 logToMetricReportsCollection, period,
50 appendLimit, reportUpdates, reportManager,
51 reportStorage, std::move(metrics), enabled);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020052}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010053
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000054Sensors ReportFactory::getSensors(
55 const std::vector<LabeledSensorParameters>& sensorPaths) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010056{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000057 using namespace utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010058
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000059 return utils::transform(sensorPaths,
60 [this](const LabeledSensorParameters& sensorPath)
61 -> std::shared_ptr<interfaces::Sensor> {
62 return sensorCache.makeSensor<Sensor>(
63 sensorPath.at_label<Service>(),
64 sensorPath.at_label<Path>(),
65 bus->get_io_context(), bus);
66 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010067}
68
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000069std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams(
70 boost::asio::yield_context& yield,
71 const ReadingParameters& metricParams) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010072{
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010073 auto tree = utils::getSubTreeSensors(yield, bus);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000074
75 return utils::transform(metricParams, [&tree](const auto& item) {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000076 const auto& [sensorPaths, operationType, id, metadata,
77 collectionTimeScope, collectionDuration] = item;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000078
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000079 std::vector<LabeledSensorParameters> sensorParameters;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000080
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000081 for (const auto& sensorPath : sensorPaths)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000082 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000083 auto it = std::find_if(
84 tree.begin(), tree.end(),
85 [&sensorPath](const auto& v) { return v.first == sensorPath; });
86
87 if (it != tree.end() && it->second.size() == 1)
88 {
89 const auto& [service, ifaces] = it->second.front();
90 sensorParameters.emplace_back(service, sensorPath);
91 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000092 }
93
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000094 if (sensorParameters.size() != sensorPaths.size())
95 {
96 throw sdbusplus::exception::SdBusError(
97 static_cast<int>(std::errc::invalid_argument),
98 "Could not find service for provided sensors");
99 }
100
101 return LabeledMetricParameters(
102 std::move(sensorParameters),
103 utils::stringToOperationType(operationType), id, metadata,
104 utils::stringToCollectionTimeScope(collectionTimeScope),
105 CollectionDuration(Milliseconds(collectionDuration)));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000106 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100107}