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 | |
| 8 | Metric::Metric(std::vector<std::shared_ptr<interfaces::Sensor>> sensors, |
| 9 | std::string operationType, std::string id, |
| 10 | std::string metadata) : |
| 11 | sensors(std::move(sensors)), |
| 12 | operationType(std::move(operationType)), id(std::move(id)), |
| 13 | metadata(std::move(metadata)) |
| 14 | {} |
| 15 | |
| 16 | void Metric::initialize() |
| 17 | { |
| 18 | readings = std::vector<MetricValue>(sensors.size(), |
| 19 | MetricValue{id, metadata, 0., 0u}); |
| 20 | |
| 21 | for (auto& sensor : sensors) |
| 22 | { |
| 23 | sensor->registerForUpdates(weak_from_this()); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | const std::vector<MetricValue>& Metric::getReadings() const |
| 28 | { |
| 29 | return readings; |
| 30 | } |
| 31 | |
| 32 | void Metric::sensorUpdated(interfaces::Sensor& sensor, uint64_t timestamp) |
| 33 | { |
| 34 | MetricValue& mv = findMetric(sensor); |
| 35 | mv.timestamp = timestamp; |
| 36 | } |
| 37 | |
| 38 | void Metric::sensorUpdated(interfaces::Sensor& sensor, uint64_t timestamp, |
| 39 | double value) |
| 40 | { |
| 41 | MetricValue& mv = findMetric(sensor); |
| 42 | mv.timestamp = timestamp; |
| 43 | mv.value = value; |
| 44 | } |
| 45 | |
| 46 | MetricValue& Metric::findMetric(interfaces::Sensor& sensor) |
| 47 | { |
| 48 | auto it = |
| 49 | std::find_if(sensors.begin(), sensors.end(), |
| 50 | [&sensor](const auto& s) { return s.get() == &sensor; }); |
| 51 | auto index = std::distance(sensors.begin(), it); |
| 52 | return readings.at(index); |
| 53 | } |
| 54 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 55 | LabeledMetricParameters Metric::dumpConfiguration() const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 56 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 57 | auto sensorPaths = utils::transform(sensors, [](const auto& sensor) { |
| 58 | return LabeledSensorParameters(sensor->id().service, sensor->id().path); |
| 59 | }); |
| 60 | return LabeledMetricParameters(std::move(sensorPaths), operationType, id, |
| 61 | metadata); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 62 | } |