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 | |
| 3 | #include "interfaces/types.hpp" |
| 4 | #include "utils/transform.hpp" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 8 | Metric::Metric(std::shared_ptr<interfaces::Sensor> sensor, |
| 9 | OperationType operationType, std::string id, |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 10 | std::string metadata) : |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 11 | sensor(std::move(sensor)), |
| 12 | operationType(std::move(operationType)), reading{std::move(id), |
| 13 | std::move(metadata), 0., |
| 14 | 0u} |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 15 | {} |
| 16 | |
| 17 | void Metric::initialize() |
| 18 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 19 | sensor->registerForUpdates(weak_from_this()); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 20 | } |
| 21 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 22 | const MetricValue& Metric::getReading() const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 23 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 24 | return reading; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 25 | } |
| 26 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 27 | void Metric::sensorUpdated(interfaces::Sensor& notifier, uint64_t timestamp) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 28 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 29 | MetricValue& mv = findMetric(notifier); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 30 | mv.timestamp = timestamp; |
| 31 | } |
| 32 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 33 | void Metric::sensorUpdated(interfaces::Sensor& notifier, uint64_t timestamp, |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 34 | double value) |
| 35 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 36 | MetricValue& mv = findMetric(notifier); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 37 | mv.timestamp = timestamp; |
| 38 | mv.value = value; |
| 39 | } |
| 40 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 41 | MetricValue& Metric::findMetric(interfaces::Sensor& notifier) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 42 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 43 | if (sensor.get() != ¬ifier) |
| 44 | { |
| 45 | throw std::out_of_range("unknown sensor"); |
| 46 | } |
| 47 | return reading; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 50 | LabeledMetricParameters Metric::dumpConfiguration() const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 51 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 52 | auto sensorPath = |
| 53 | LabeledSensorParameters(sensor->id().service, sensor->id().path); |
| 54 | return LabeledMetricParameters(std::move(sensorPath), operationType, |
| 55 | reading.id, reading.metadata); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 56 | } |