Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 1 | #include "report_factory.hpp" |
| 2 | |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 3 | #include "metric.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 4 | #include "report.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 5 | #include "sensor.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 6 | #include "utils/conversion.hpp" |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 7 | #include "utils/dbus_mapper.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 8 | #include "utils/transform.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 9 | |
| 10 | ReportFactory::ReportFactory( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 11 | std::shared_ptr<sdbusplus::asio::connection> bus, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 12 | const std::shared_ptr<sdbusplus::asio::object_server>& objServer, |
| 13 | SensorCache& sensorCache) : |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 14 | bus(std::move(bus)), |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 15 | objServer(objServer), sensorCache(sensorCache) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 16 | {} |
| 17 | |
| 18 | std::unique_ptr<interfaces::Report> ReportFactory::make( |
| 19 | const std::string& name, const std::string& reportingType, |
| 20 | bool emitsReadingsSignal, bool logToMetricReportsCollection, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame^] | 21 | Milliseconds period, interfaces::ReportManager& reportManager, |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 22 | interfaces::JsonStorage& reportStorage, |
| 23 | std::vector<LabeledMetricParameters> labeledMetricParams) const |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 24 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 25 | std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform( |
| 26 | labeledMetricParams, |
| 27 | [this](const LabeledMetricParameters& param) |
| 28 | -> std::shared_ptr<interfaces::Metric> { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame^] | 29 | namespace ts = utils::tstring; |
| 30 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 31 | return std::make_shared<Metric>( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame^] | 32 | 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 Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 37 | }); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 38 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 39 | return std::make_unique<Report>( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 40 | bus->get_io_context(), objServer, name, reportingType, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame^] | 41 | emitsReadingsSignal, logToMetricReportsCollection, period, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 42 | reportManager, reportStorage, std::move(metrics)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 43 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 44 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame^] | 45 | Sensors ReportFactory::getSensors( |
| 46 | const std::vector<LabeledSensorParameters>& sensorPaths) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 47 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 48 | using namespace utils::tstring; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 49 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame^] | 50 | 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 Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 60 | std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( |
| 61 | boost::asio::yield_context& yield, |
| 62 | const ReadingParameters& metricParams) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 63 | { |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 64 | auto tree = utils::getSubTreeSensors(yield, bus); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 65 | |
| 66 | return utils::transform(metricParams, [&tree](const auto& item) { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame^] | 67 | const auto& [sensorPaths, operationType, id, metadata, |
| 68 | collectionTimeScope, collectionDuration] = item; |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 69 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame^] | 70 | std::vector<LabeledSensorParameters> sensorParameters; |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 71 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame^] | 72 | for (const auto& sensorPath : sensorPaths) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 73 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame^] | 74 | 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 Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame^] | 85 | 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 Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 97 | }); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 98 | } |