blob: df197a4cfcde4ada38018f1eedd98f0e0fd58251 [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(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010020 const std::string& id, const std::string& name,
21 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010022 const std::vector<ReportAction>& reportActions, Milliseconds period,
23 uint64_t appendLimit, const ReportUpdates reportUpdates,
Szymon Dompke3eb56862021-09-20 15:32:04 +020024 interfaces::ReportManager& reportManager,
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000025 interfaces::JsonStorage& reportStorage,
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010026 std::vector<LabeledMetricParameters> labeledMetricParams, bool enabled,
27 const std::vector<std::string>& triggerIds) 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>(),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000038 param.at_label<ts::CollectionTimeScope>(),
Krzysztof Grobelny80697712021-03-04 09:49:27 +000039 param.at_label<ts::CollectionDuration>(),
40 std::make_unique<Clock>());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000041 });
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020042
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010043 return std::make_unique<Report>(bus->get_io_context(), objServer, id, name,
44 reportingType, reportActions, period,
45 appendLimit, reportUpdates, reportManager,
46 reportStorage, std::move(metrics), enabled,
47 std::make_unique<Clock>(), triggerIds);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020048}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010049
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000050Sensors ReportFactory::getSensors(
Szymon Dompke94f71c52021-12-10 07:16:33 +010051 const std::vector<LabeledSensorInfo>& sensorPaths) const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010052{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000053 using namespace utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010054
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010055 return utils::transform(
56 sensorPaths,
Szymon Dompke94f71c52021-12-10 07:16:33 +010057 [this](const LabeledSensorInfo& sensorPath)
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010058 -> std::shared_ptr<interfaces::Sensor> {
59 return sensorCache.makeSensor<Sensor>(
60 sensorPath.at_label<Service>(), sensorPath.at_label<Path>(),
61 sensorPath.at_label<Metadata>(), bus->get_io_context(), bus);
62 });
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 Grobelnyb8cc78d2021-11-29 15:54:53 +010072 auto [sensorPaths, operationType, id, collectionTimeScope,
73 collectionDuration] = item;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000074
Szymon Dompke94f71c52021-12-10 07:16:33 +010075 std::vector<LabeledSensorInfo> sensorParameters;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000076
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010077 for (const auto& [sensorPath, metadata] : sensorPaths)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000078 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000079 auto it = std::find_if(
80 tree.begin(), tree.end(),
Krzysztof Grobelnyfbeb5bf2022-01-03 09:41:29 +010081 [path = sensorPath](const auto& v) { return v.first == path; });
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000082
83 if (it != tree.end() && it->second.size() == 1)
84 {
85 const auto& [service, ifaces] = it->second.front();
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010086 sensorParameters.emplace_back(service, sensorPath, metadata);
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000087 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000088 }
89
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000090 if (sensorParameters.size() != sensorPaths.size())
91 {
92 throw sdbusplus::exception::SdBusError(
93 static_cast<int>(std::errc::invalid_argument),
94 "Could not find service for provided sensors");
95 }
96
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010097 if (operationType.empty())
98 {
99 operationType = utils::enumToString(OperationType::avg);
100 }
101
102 if (collectionTimeScope.empty())
103 {
104 collectionTimeScope =
105 utils::enumToString(CollectionTimeScope::point);
106 }
107
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000108 return LabeledMetricParameters(
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100109 std::move(sensorParameters), utils::toOperationType(operationType),
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100110 id, utils::toCollectionTimeScope(collectionTimeScope),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000111 CollectionDuration(Milliseconds(collectionDuration)));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000112 });
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100113}