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 | 9f9ff90 | 2021-03-05 14:57:53 +0000 | [diff] [blame] | 46 | param.at_index<2>(), param.at_index<3>()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 47 | }); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 48 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 49 | return std::make_unique<Report>( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 50 | bus->get_io_context(), objServer, name, reportingType, |
| 51 | emitsReadingsSignal, logToMetricReportsCollection, period, metricParams, |
| 52 | reportManager, reportStorage, std::move(metrics)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 53 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 54 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 55 | std::shared_ptr<interfaces::Sensor> |
| 56 | ReportFactory::getSensor(const LabeledSensorParameters& sensorPath) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 57 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 58 | using namespace utils::tstring; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 59 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 60 | return sensorCache.makeSensor<Sensor>(sensorPath.at_label<Service>(), |
| 61 | sensorPath.at_label<Path>(), |
| 62 | bus->get_io_context(), bus); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 65 | std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( |
| 66 | boost::asio::yield_context& yield, |
| 67 | const ReadingParameters& metricParams) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 68 | { |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 69 | auto tree = utils::getSubTreeSensors(yield, bus); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 70 | |
| 71 | return utils::transform(metricParams, [&tree](const auto& item) { |
Krzysztof Grobelny | 9f9ff90 | 2021-03-05 14:57:53 +0000 | [diff] [blame] | 72 | const auto& [sensorPath, operationType, id, metadata] = item; |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 73 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +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) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 79 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 80 | const auto& [service, ifaces] = it->second.front(); |
| 81 | return LabeledMetricParameters( |
| 82 | LabeledSensorParameters(service, sensorPath), |
Krzysztof Grobelny | 9f9ff90 | 2021-03-05 14:57:53 +0000 | [diff] [blame] | 83 | utils::stringToOperationType(operationType), id, metadata); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 86 | throw sdbusplus::exception::SdBusError( |
| 87 | static_cast<int>(std::errc::invalid_argument), |
| 88 | "Could not find service for provided sensors"); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 89 | }); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 90 | } |