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" |
| 6 | #include "utils/transform.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 7 | |
| 8 | ReportFactory::ReportFactory( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 9 | std::shared_ptr<sdbusplus::asio::connection> bus, |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 10 | const std::shared_ptr<sdbusplus::asio::object_server>& objServer) : |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 11 | bus(std::move(bus)), |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 12 | objServer(objServer) |
| 13 | {} |
| 14 | |
| 15 | std::unique_ptr<interfaces::Report> ReportFactory::make( |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 16 | boost::asio::yield_context& yield, const std::string& name, |
| 17 | const std::string& reportingType, bool emitsReadingsSignal, |
| 18 | bool logToMetricReportsCollection, std::chrono::milliseconds period, |
| 19 | const ReadingParameters& metricParams, |
| 20 | interfaces::ReportManager& reportManager, |
| 21 | interfaces::JsonStorage& reportStorage) const |
| 22 | { |
| 23 | return make(name, reportingType, emitsReadingsSignal, |
| 24 | logToMetricReportsCollection, period, metricParams, |
| 25 | reportManager, reportStorage, |
| 26 | convertMetricParams(yield, metricParams)); |
| 27 | } |
| 28 | |
| 29 | std::unique_ptr<interfaces::Report> ReportFactory::make( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 30 | const std::string& name, const std::string& reportingType, |
| 31 | bool emitsReadingsSignal, bool logToMetricReportsCollection, |
| 32 | std::chrono::milliseconds period, const ReadingParameters& metricParams, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 33 | interfaces::ReportManager& reportManager, |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 34 | interfaces::JsonStorage& reportStorage, |
| 35 | std::vector<LabeledMetricParameters> labeledMetricParams) const |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 36 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 37 | std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform( |
| 38 | labeledMetricParams, |
| 39 | [this](const LabeledMetricParameters& param) |
| 40 | -> std::shared_ptr<interfaces::Metric> { |
| 41 | return std::make_shared<Metric>( |
| 42 | getSensors(param.at_index<0>()), param.at_index<1>(), |
| 43 | param.at_index<2>(), param.at_index<3>()); |
| 44 | }); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 45 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 46 | return std::make_unique<Report>( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 47 | bus->get_io_context(), objServer, name, reportingType, |
| 48 | emitsReadingsSignal, logToMetricReportsCollection, period, metricParams, |
| 49 | reportManager, reportStorage, std::move(metrics)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 50 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 51 | |
| 52 | std::vector<std::shared_ptr<interfaces::Sensor>> ReportFactory::getSensors( |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 53 | const std::vector<LabeledSensorParameters>& sensorPaths) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 54 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 55 | return utils::transform(sensorPaths, |
| 56 | [this](const LabeledSensorParameters& param) |
| 57 | -> std::shared_ptr<interfaces::Sensor> { |
| 58 | using namespace utils::tstring; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 59 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 60 | return sensorCache.makeSensor<Sensor>( |
| 61 | param.at_label<Service>(), |
| 62 | param.at_label<Path>(), |
| 63 | bus->get_io_context(), bus); |
| 64 | }); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 67 | std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( |
| 68 | boost::asio::yield_context& yield, |
| 69 | const ReadingParameters& metricParams) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 70 | { |
| 71 | std::array<const char*, 1> interfaces = { |
| 72 | "xyz.openbmc_project.Sensor.Value"}; |
| 73 | boost::system::error_code ec; |
| 74 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 75 | auto tree = bus->yield_method_call<std::vector<SensorTree>>( |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 76 | yield, ec, "xyz.openbmc_project.ObjectMapper", |
| 77 | "/xyz/openbmc_project/object_mapper", |
| 78 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
| 79 | "/xyz/openbmc_project/sensors", 2, interfaces); |
| 80 | if (ec) |
| 81 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 82 | throw std::runtime_error("Failed to query ObjectMapper!"); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 83 | } |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 84 | |
| 85 | return utils::transform(metricParams, [&tree](const auto& item) { |
| 86 | std::vector<LabeledSensorParameters> sensors; |
| 87 | |
| 88 | for (const auto& sensorPath : std::get<0>(item)) |
| 89 | { |
| 90 | auto it = std::find_if( |
| 91 | tree.begin(), tree.end(), |
| 92 | [&sensorPath](const auto& v) { return v.first == sensorPath; }); |
| 93 | |
| 94 | if (it != tree.end()) |
| 95 | { |
| 96 | for (const auto& [service, ifaces] : it->second) |
| 97 | { |
| 98 | sensors.emplace_back(service, sensorPath); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return LabeledMetricParameters(std::move(sensors), std::get<1>(item), |
| 104 | std::get<2>(item), std::get<3>(item)); |
| 105 | }); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 106 | } |