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( |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 19 | boost::asio::yield_context& yield, const std::string& name, |
| 20 | const std::string& reportingType, bool emitsReadingsSignal, |
| 21 | bool logToMetricReportsCollection, std::chrono::milliseconds period, |
| 22 | const ReadingParameters& metricParams, |
| 23 | interfaces::ReportManager& reportManager, |
| 24 | interfaces::JsonStorage& reportStorage) const |
| 25 | { |
| 26 | return make(name, reportingType, emitsReadingsSignal, |
| 27 | logToMetricReportsCollection, period, metricParams, |
| 28 | reportManager, reportStorage, |
| 29 | convertMetricParams(yield, metricParams)); |
| 30 | } |
| 31 | |
| 32 | std::unique_ptr<interfaces::Report> ReportFactory::make( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 33 | const std::string& name, const std::string& reportingType, |
| 34 | bool emitsReadingsSignal, bool logToMetricReportsCollection, |
| 35 | std::chrono::milliseconds period, const ReadingParameters& metricParams, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 36 | interfaces::ReportManager& reportManager, |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 37 | interfaces::JsonStorage& reportStorage, |
| 38 | std::vector<LabeledMetricParameters> labeledMetricParams) const |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 39 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 40 | std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform( |
| 41 | labeledMetricParams, |
| 42 | [this](const LabeledMetricParameters& param) |
| 43 | -> std::shared_ptr<interfaces::Metric> { |
| 44 | return std::make_shared<Metric>( |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 45 | getSensor(param.at_index<0>()), param.at_index<1>(), |
Krzysztof Grobelny | 753e4b3 | 2021-02-11 12:58:58 +0000 | [diff] [blame] | 46 | param.at_index<2>(), param.at_index<3>(), param.at_index<4>(), |
| 47 | param.at_index<5>()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 48 | }); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 49 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 50 | return std::make_unique<Report>( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 51 | bus->get_io_context(), objServer, name, reportingType, |
| 52 | emitsReadingsSignal, logToMetricReportsCollection, period, metricParams, |
| 53 | reportManager, reportStorage, std::move(metrics)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 54 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 55 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 56 | std::shared_ptr<interfaces::Sensor> |
| 57 | ReportFactory::getSensor(const LabeledSensorParameters& sensorPath) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 58 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 59 | using namespace utils::tstring; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 60 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 61 | return sensorCache.makeSensor<Sensor>(sensorPath.at_label<Service>(), |
| 62 | sensorPath.at_label<Path>(), |
| 63 | bus->get_io_context(), bus); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 66 | std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( |
| 67 | boost::asio::yield_context& yield, |
| 68 | const ReadingParameters& metricParams) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 69 | { |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 70 | auto tree = utils::getSubTreeSensors(yield, bus); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 71 | |
| 72 | return utils::transform(metricParams, [&tree](const auto& item) { |
Krzysztof Grobelny | 753e4b3 | 2021-02-11 12:58:58 +0000 | [diff] [blame] | 73 | const auto& [sensorPath, operationType, id, metadata, |
| 74 | collectionTimeScope, collectionDuration] = item; |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 75 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 76 | 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) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 81 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 82 | const auto& [service, ifaces] = it->second.front(); |
| 83 | return LabeledMetricParameters( |
| 84 | LabeledSensorParameters(service, sensorPath), |
Krzysztof Grobelny | 753e4b3 | 2021-02-11 12:58:58 +0000 | [diff] [blame] | 85 | utils::stringToOperationType(operationType), id, metadata, |
| 86 | utils::stringToCollectionTimeScope(collectionTimeScope), |
| 87 | CollectionDuration( |
| 88 | std::chrono::milliseconds(collectionDuration))); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 91 | throw sdbusplus::exception::SdBusError( |
| 92 | static_cast<int>(std::errc::invalid_argument), |
| 93 | "Could not find service for provided sensors"); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 94 | }); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 95 | } |