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 | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 26 | std::vector<LabeledMetricParameters> labeledMetricParams, bool enabled, |
| 27 | Readings readings) const |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 28 | { |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 29 | auto metrics = |
| 30 | utils::transform(labeledMetricParams, |
| 31 | [this](const LabeledMetricParameters& param) |
| 32 | -> std::shared_ptr<interfaces::Metric> { |
| 33 | namespace ts = utils::tstring; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 34 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 35 | return std::make_shared<Metric>( |
| 36 | getSensors(param.at_label<ts::SensorPath>()), |
| 37 | param.at_label<ts::OperationType>(), |
| 38 | param.at_label<ts::CollectionTimeScope>(), |
| 39 | param.at_label<ts::CollectionDuration>(), |
| 40 | std::make_unique<Clock>()); |
| 41 | }); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 42 | |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 43 | return std::make_unique<Report>( |
| 44 | bus->get_io_context(), objServer, id, name, reportingType, |
| 45 | reportActions, period, appendLimit, reportUpdates, reportManager, |
| 46 | reportStorage, std::move(metrics), *this, enabled, |
| 47 | std::make_unique<Clock>(), std::move(readings)); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void ReportFactory::updateMetrics( |
| 51 | std::vector<std::shared_ptr<interfaces::Metric>>& metrics, bool enabled, |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 52 | const std::vector<LabeledMetricParameters>& labeledMetricParams) const |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 53 | { |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 54 | std::vector<std::shared_ptr<interfaces::Metric>> oldMetrics = metrics; |
| 55 | std::vector<std::shared_ptr<interfaces::Metric>> newMetrics; |
| 56 | |
| 57 | for (const auto& labeledMetricParam : labeledMetricParams) |
| 58 | { |
| 59 | auto existing = std::find_if(oldMetrics.begin(), oldMetrics.end(), |
| 60 | [labeledMetricParam](auto metric) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 61 | return labeledMetricParam == metric->dumpConfiguration(); |
| 62 | }); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 63 | |
| 64 | if (existing != oldMetrics.end()) |
| 65 | { |
| 66 | newMetrics.emplace_back(*existing); |
| 67 | oldMetrics.erase(existing); |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | namespace ts = utils::tstring; |
| 72 | newMetrics.emplace_back(std::make_shared<Metric>( |
| 73 | getSensors(labeledMetricParam.at_label<ts::SensorPath>()), |
| 74 | labeledMetricParam.at_label<ts::OperationType>(), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 75 | labeledMetricParam.at_label<ts::CollectionTimeScope>(), |
| 76 | labeledMetricParam.at_label<ts::CollectionDuration>(), |
| 77 | std::make_unique<Clock>())); |
| 78 | |
| 79 | if (enabled) |
| 80 | { |
| 81 | newMetrics.back()->initialize(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (enabled) |
| 86 | { |
| 87 | for (auto& metric : oldMetrics) |
| 88 | { |
| 89 | metric->deinitialize(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | metrics = std::move(newMetrics); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 94 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 95 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 96 | Sensors ReportFactory::getSensors( |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 97 | const std::vector<LabeledSensorInfo>& sensorPaths) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 98 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 99 | using namespace utils::tstring; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 100 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 101 | return utils::transform( |
| 102 | sensorPaths, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 103 | [this](const LabeledSensorInfo& sensorPath) |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 104 | -> std::shared_ptr<interfaces::Sensor> { |
| 105 | return sensorCache.makeSensor<Sensor>( |
| 106 | sensorPath.at_label<Service>(), sensorPath.at_label<Path>(), |
| 107 | sensorPath.at_label<Metadata>(), bus->get_io_context(), bus); |
| 108 | }); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 111 | std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( |
| 112 | boost::asio::yield_context& yield, |
| 113 | const ReadingParameters& metricParams) const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 114 | { |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 115 | if (metricParams.empty()) |
| 116 | { |
| 117 | return {}; |
| 118 | } |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 119 | |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 120 | auto tree = utils::getSubTreeSensors(yield, bus); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 121 | return getMetricParamsFromSensorTree(metricParams, tree); |
| 122 | } |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 123 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 124 | std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( |
| 125 | const ReadingParameters& metricParams) const |
| 126 | { |
| 127 | if (metricParams.empty()) |
| 128 | { |
| 129 | return {}; |
| 130 | } |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 131 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 132 | auto tree = utils::getSubTreeSensors(bus); |
| 133 | return getMetricParamsFromSensorTree(metricParams, tree); |
| 134 | } |
| 135 | |
| 136 | std::vector<LabeledMetricParameters> |
| 137 | ReportFactory::getMetricParamsFromSensorTree( |
| 138 | const ReadingParameters& metricParams, |
| 139 | const std::vector<utils::SensorTree>& tree) const |
| 140 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 141 | try |
| 142 | { |
| 143 | return utils::transform(metricParams, [&tree](const auto& item) { |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 144 | auto [sensorPaths, operationType, collectionTimeScope, |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 145 | collectionDuration] = item; |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 146 | |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 147 | std::vector<LabeledSensorInfo> sensorParameters; |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 148 | |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 149 | for (const auto& [sensorPath, metadata] : sensorPaths) |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 150 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 151 | auto it = std::find_if(tree.begin(), tree.end(), |
| 152 | [path = sensorPath](const auto& v) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 153 | return v.first == path; |
| 154 | }); |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 155 | |
| 156 | if (it != tree.end() && it->second.size() == 1) |
| 157 | { |
| 158 | const auto& [service, ifaces] = it->second.front(); |
| 159 | sensorParameters.emplace_back(service, sensorPath, |
| 160 | metadata); |
| 161 | } |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 162 | } |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 163 | |
| 164 | if (sensorParameters.size() != sensorPaths.size()) |
| 165 | { |
| 166 | throw errors::InvalidArgument("ReadingParameters", |
| 167 | "Service not found."); |
| 168 | } |
| 169 | |
| 170 | if (operationType.empty()) |
| 171 | { |
| 172 | operationType = utils::enumToString(OperationType::avg); |
| 173 | } |
| 174 | else if (operationType == "SINGLE") |
| 175 | { |
| 176 | operationType = utils::enumToString(OperationType::avg); |
| 177 | collectionTimeScope = |
| 178 | utils::enumToString(CollectionTimeScope::point); |
| 179 | } |
| 180 | |
| 181 | if (collectionTimeScope.empty()) |
| 182 | { |
| 183 | collectionTimeScope = |
| 184 | utils::enumToString(CollectionTimeScope::point); |
| 185 | } |
| 186 | |
| 187 | return LabeledMetricParameters( |
| 188 | std::move(sensorParameters), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 189 | utils::toOperationType(operationType), |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 190 | utils::toCollectionTimeScope(collectionTimeScope), |
| 191 | CollectionDuration(Milliseconds(collectionDuration))); |
| 192 | }); |
| 193 | } |
| 194 | catch (const errors::InvalidArgument& e) |
| 195 | { |
| 196 | if (e.propertyName == "ReadingParameters") |
| 197 | { |
| 198 | throw; |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 201 | using namespace std::literals::string_literals; |
| 202 | throw errors::InvalidArgument("ReadingParameters."s + e.propertyName); |
| 203 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 204 | } |