blob: 2a735369f2daa6bb35e6c4bcd9f9e2327aa293ff [file] [log] [blame]
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02001#include "metric.hpp"
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01002
3#include "interfaces/types.hpp"
4#include "utils/transform.hpp"
5
6#include <algorithm>
7
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +00008Metric::Metric(std::shared_ptr<interfaces::Sensor> sensor,
9 OperationType operationType, std::string id,
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010010 std::string metadata) :
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000011 sensor(std::move(sensor)),
12 operationType(std::move(operationType)), reading{std::move(id),
13 std::move(metadata), 0.,
14 0u}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010015{}
16
17void Metric::initialize()
18{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000019 sensor->registerForUpdates(weak_from_this());
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010020}
21
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000022const MetricValue& Metric::getReading() const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010023{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000024 return reading;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010025}
26
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000027void Metric::sensorUpdated(interfaces::Sensor& notifier, uint64_t timestamp)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010028{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000029 MetricValue& mv = findMetric(notifier);
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010030 mv.timestamp = timestamp;
31}
32
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000033void Metric::sensorUpdated(interfaces::Sensor& notifier, uint64_t timestamp,
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010034 double value)
35{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000036 MetricValue& mv = findMetric(notifier);
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010037 mv.timestamp = timestamp;
38 mv.value = value;
39}
40
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000041MetricValue& Metric::findMetric(interfaces::Sensor& notifier)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010042{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000043 if (sensor.get() != &notifier)
44 {
45 throw std::out_of_range("unknown sensor");
46 }
47 return reading;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010048}
49
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000050LabeledMetricParameters Metric::dumpConfiguration() const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010051{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000052 auto sensorPath =
53 LabeledSensorParameters(sensor->id().service, sensor->id().path);
54 return LabeledMetricParameters(std::move(sensorPath), operationType,
55 reading.id, reading.metadata);
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010056}