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 | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 6 | #include "utils/clock.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 7 | #include "utils/conversion.hpp" |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 8 | #include "utils/dbus_mapper.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 9 | #include "utils/transform.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 10 | |
| 11 | ReportFactory::ReportFactory( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 12 | std::shared_ptr<sdbusplus::asio::connection> bus, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 13 | const std::shared_ptr<sdbusplus::asio::object_server>& objServer, |
| 14 | SensorCache& sensorCache) : |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 15 | bus(std::move(bus)), |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 16 | objServer(objServer), sensorCache(sensorCache) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 17 | {} |
| 18 | |
| 19 | std::unique_ptr<interfaces::Report> ReportFactory::make( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 20 | const std::string& id, const std::string& name, |
| 21 | const ReportingType reportingType, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 22 | const std::vector<ReportAction>& reportActions, Milliseconds period, |
| 23 | uint64_t appendLimit, const ReportUpdates reportUpdates, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 24 | interfaces::ReportManager& reportManager, |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 25 | interfaces::JsonStorage& reportStorage, |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 26 | std::vector<LabeledMetricParameters> labeledMetricParams, |
| 27 | bool enabled) const |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 28 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 29 | std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform( |
| 30 | labeledMetricParams, |
| 31 | [this](const LabeledMetricParameters& param) |
| 32 | -> std::shared_ptr<interfaces::Metric> { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 33 | namespace ts = utils::tstring; |
| 34 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 35 | return std::make_shared<Metric>( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 36 | getSensors(param.at_label<ts::SensorPath>()), |
| 37 | param.at_label<ts::OperationType>(), param.at_label<ts::Id>(), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 38 | param.at_label<ts::CollectionTimeScope>(), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 39 | param.at_label<ts::CollectionDuration>(), |
| 40 | std::make_unique<Clock>()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 41 | }); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 42 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame^] | 43 | 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), *this, |
| 47 | enabled, std::make_unique<Clock>()); |
| 48 | } |
| 49 | |
| 50 | void ReportFactory::updateMetrics( |
| 51 | std::vector<std::shared_ptr<interfaces::Metric>>& metrics, bool enabled, |
| 52 | const ReadingParameters& metricParams) const |
| 53 | { |
| 54 | auto labeledMetricParams = convertMetricParams(metricParams); |
| 55 | std::vector<std::shared_ptr<interfaces::Metric>> oldMetrics = metrics; |
| 56 | std::vector<std::shared_ptr<interfaces::Metric>> newMetrics; |
| 57 | |
| 58 | for (const auto& labeledMetricParam : labeledMetricParams) |
| 59 | { |
| 60 | auto existing = std::find_if(oldMetrics.begin(), oldMetrics.end(), |
| 61 | [labeledMetricParam](auto metric) { |
| 62 | return labeledMetricParam == |
| 63 | metric->dumpConfiguration(); |
| 64 | }); |
| 65 | |
| 66 | if (existing != oldMetrics.end()) |
| 67 | { |
| 68 | newMetrics.emplace_back(*existing); |
| 69 | oldMetrics.erase(existing); |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | namespace ts = utils::tstring; |
| 74 | newMetrics.emplace_back(std::make_shared<Metric>( |
| 75 | getSensors(labeledMetricParam.at_label<ts::SensorPath>()), |
| 76 | labeledMetricParam.at_label<ts::OperationType>(), |
| 77 | labeledMetricParam.at_label<ts::Id>(), |
| 78 | labeledMetricParam.at_label<ts::CollectionTimeScope>(), |
| 79 | labeledMetricParam.at_label<ts::CollectionDuration>(), |
| 80 | std::make_unique<Clock>())); |
| 81 | |
| 82 | if (enabled) |
| 83 | { |
| 84 | newMetrics.back()->initialize(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (enabled) |
| 89 | { |
| 90 | for (auto& metric : oldMetrics) |
| 91 | { |
| 92 | metric->deinitialize(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | metrics = std::move(newMetrics); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 97 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 98 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 99 | Sensors ReportFactory::getSensors( |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 100 | const std::vector<LabeledSensorInfo>& sensorPaths) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 101 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 102 | using namespace utils::tstring; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 103 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 104 | return utils::transform( |
| 105 | sensorPaths, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 106 | [this](const LabeledSensorInfo& sensorPath) |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 107 | -> std::shared_ptr<interfaces::Sensor> { |
| 108 | return sensorCache.makeSensor<Sensor>( |
| 109 | sensorPath.at_label<Service>(), sensorPath.at_label<Path>(), |
| 110 | sensorPath.at_label<Metadata>(), bus->get_io_context(), bus); |
| 111 | }); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 114 | std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( |
| 115 | boost::asio::yield_context& yield, |
| 116 | const ReadingParameters& metricParams) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 117 | { |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame^] | 118 | if (metricParams.empty()) |
| 119 | { |
| 120 | return {}; |
| 121 | } |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 122 | auto tree = utils::getSubTreeSensors(yield, bus); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame^] | 123 | return getMetricParamsFromSensorTree(metricParams, tree); |
| 124 | } |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 125 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame^] | 126 | std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( |
| 127 | const ReadingParameters& metricParams) const |
| 128 | { |
| 129 | if (metricParams.empty()) |
| 130 | { |
| 131 | return {}; |
| 132 | } |
| 133 | auto tree = utils::getSubTreeSensors(bus); |
| 134 | return getMetricParamsFromSensorTree(metricParams, tree); |
| 135 | } |
| 136 | |
| 137 | std::vector<LabeledMetricParameters> |
| 138 | ReportFactory::getMetricParamsFromSensorTree( |
| 139 | const ReadingParameters& metricParams, |
| 140 | const std::vector<utils::SensorTree>& tree) const |
| 141 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 142 | return utils::transform(metricParams, [&tree](const auto& item) { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 143 | auto [sensorPaths, operationType, id, collectionTimeScope, |
| 144 | collectionDuration] = item; |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 145 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 146 | std::vector<LabeledSensorInfo> sensorParameters; |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 147 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 148 | for (const auto& [sensorPath, metadata] : sensorPaths) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 149 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 150 | auto it = std::find_if( |
| 151 | tree.begin(), tree.end(), |
Krzysztof Grobelny | fbeb5bf | 2022-01-03 09:41:29 +0100 | [diff] [blame] | 152 | [path = sensorPath](const auto& v) { return v.first == path; }); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 153 | |
| 154 | if (it != tree.end() && it->second.size() == 1) |
| 155 | { |
| 156 | const auto& [service, ifaces] = it->second.front(); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 157 | sensorParameters.emplace_back(service, sensorPath, metadata); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 158 | } |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 161 | if (sensorParameters.size() != sensorPaths.size()) |
| 162 | { |
| 163 | throw sdbusplus::exception::SdBusError( |
| 164 | static_cast<int>(std::errc::invalid_argument), |
| 165 | "Could not find service for provided sensors"); |
| 166 | } |
| 167 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 168 | if (operationType.empty()) |
| 169 | { |
| 170 | operationType = utils::enumToString(OperationType::avg); |
| 171 | } |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame] | 172 | else if (operationType == "SINGLE") |
| 173 | { |
| 174 | operationType = utils::enumToString(OperationType::avg); |
| 175 | collectionTimeScope = |
| 176 | utils::enumToString(CollectionTimeScope::point); |
| 177 | } |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 178 | |
| 179 | if (collectionTimeScope.empty()) |
| 180 | { |
| 181 | collectionTimeScope = |
| 182 | utils::enumToString(CollectionTimeScope::point); |
| 183 | } |
| 184 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 185 | return LabeledMetricParameters( |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 186 | std::move(sensorParameters), utils::toOperationType(operationType), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 187 | id, utils::toCollectionTimeScope(collectionTimeScope), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 188 | CollectionDuration(Milliseconds(collectionDuration))); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 189 | }); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 190 | } |