Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 1 | #include "metric.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 2 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 3 | #include "metrics/collection_data.hpp" |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 4 | #include "types/report_types.hpp" |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 5 | #include "types/sensor_types.hpp" |
Szymon Dompke | 3a61702 | 2021-07-19 18:23:02 +0200 | [diff] [blame] | 6 | #include "utils/labeled_tuple.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 7 | #include "utils/transform.hpp" |
| 8 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 9 | #include <sdbusplus/exception.hpp> |
| 10 | |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 11 | #include <algorithm> |
| 12 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 13 | Metric::Metric(Sensors sensorsIn, OperationType operationTypeIn, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 14 | std::string idIn, CollectionTimeScope timeScopeIn, |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 15 | CollectionDuration collectionDurationIn, |
| 16 | std::unique_ptr<interfaces::Clock> clockIn) : |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 17 | id(std::move(idIn)), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 18 | sensors(std::move(sensorsIn)), operationType(operationTypeIn), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 19 | collectionTimeScope(timeScopeIn), collectionDuration(collectionDurationIn), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 20 | collectionAlgorithms( |
| 21 | metrics::makeCollectionData(sensors.size(), operationType, |
| 22 | collectionTimeScope, collectionDuration)), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 23 | clock(std::move(clockIn)) |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 24 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 25 | readings = utils::transform(sensors, [this](const auto& sensor) { |
| 26 | return MetricValue{id, sensor->metadata(), 0.0, 0u}; |
| 27 | }); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 28 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 29 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 30 | void Metric::registerForUpdates(interfaces::MetricListener& listener) |
| 31 | { |
| 32 | listeners.emplace_back(listener); |
| 33 | } |
| 34 | |
| 35 | void Metric::unregisterFromUpdates(interfaces::MetricListener& listener) |
| 36 | { |
| 37 | listeners.erase( |
| 38 | std::remove_if(listeners.begin(), listeners.end(), |
| 39 | [&listener](const interfaces::MetricListener& item) { |
| 40 | return &item == &listener; |
| 41 | }), |
| 42 | listeners.end()); |
| 43 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 44 | |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 45 | void Metric::initialize() |
| 46 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 47 | for (const auto& sensor : sensors) |
| 48 | { |
| 49 | sensor->registerForUpdates(weak_from_this()); |
| 50 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 53 | void Metric::deinitialize() |
| 54 | { |
| 55 | for (const auto& sensor : sensors) |
| 56 | { |
| 57 | sensor->unregisterFromUpdates(weak_from_this()); |
| 58 | } |
| 59 | } |
| 60 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 61 | std::vector<MetricValue> Metric::getReadings() const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 62 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 63 | const auto steadyTimestamp = clock->steadyTimestamp(); |
| 64 | const auto systemTimestamp = clock->systemTimestamp(); |
| 65 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 66 | auto resultReadings = readings; |
| 67 | |
| 68 | for (size_t i = 0; i < resultReadings.size(); ++i) |
| 69 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 70 | if (const auto value = collectionAlgorithms[i]->update(steadyTimestamp)) |
| 71 | { |
| 72 | resultReadings[i].timestamp = |
| 73 | std::chrono::duration_cast<Milliseconds>(systemTimestamp) |
| 74 | .count(); |
| 75 | resultReadings[i].value = *value; |
| 76 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return resultReadings; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 82 | void Metric::sensorUpdated(interfaces::Sensor& notifier, Milliseconds timestamp, |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 83 | double value) |
| 84 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 85 | auto& data = findAssociatedData(notifier); |
| 86 | double newValue = data.update(timestamp, value); |
| 87 | |
| 88 | if (data.updateLastValue(newValue)) |
| 89 | { |
| 90 | for (interfaces::MetricListener& listener : listeners) |
| 91 | { |
| 92 | listener.metricUpdated(); |
| 93 | } |
| 94 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 97 | metrics::CollectionData& |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 98 | Metric::findAssociatedData(const interfaces::Sensor& notifier) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 99 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 100 | auto it = std::find_if( |
| 101 | sensors.begin(), sensors.end(), |
| 102 | [¬ifier](const auto& sensor) { return sensor.get() == ¬ifier; }); |
| 103 | auto index = std::distance(sensors.begin(), it); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 104 | return *collectionAlgorithms.at(index); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 107 | LabeledMetricParameters Metric::dumpConfiguration() const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 108 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 109 | auto sensorPath = utils::transform(sensors, [this](const auto& sensor) { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 110 | return LabeledSensorInfo(sensor->id().service, sensor->id().path, |
| 111 | sensor->metadata()); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 112 | }); |
| 113 | |
| 114 | return LabeledMetricParameters(std::move(sensorPath), operationType, id, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 115 | collectionTimeScope, collectionDuration); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 118 | uint64_t Metric::sensorCount() const |
| 119 | { |
| 120 | return sensors.size(); |
| 121 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 122 | |
| 123 | void Metric::updateReadings(Milliseconds timestamp) |
| 124 | { |
| 125 | for (auto& data : collectionAlgorithms) |
| 126 | { |
| 127 | if (std::optional<double> newValue = data->update(timestamp)) |
| 128 | { |
| 129 | if (data->updateLastValue(*newValue)) |
| 130 | { |
| 131 | for (interfaces::MetricListener& listener : listeners) |
| 132 | { |
| 133 | listener.metricUpdated(); |
| 134 | } |
| 135 | return; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | bool Metric::isTimerRequired() const |
| 142 | { |
| 143 | if (collectionTimeScope == CollectionTimeScope::point) |
| 144 | { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | if (collectionTimeScope == CollectionTimeScope::startup && |
| 149 | (operationType == OperationType::min || |
| 150 | operationType == OperationType::max)) |
| 151 | { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | return true; |
| 156 | } |