xref: /openbmc/telemetry/src/metric.cpp (revision e8fc57514575f07565035db07cb522fe21988224)
1c8e3a64aSKrzysztof Grobelny #include "metric.hpp"
26ccfcbf5SKrzysztof Grobelny 
36ccfcbf5SKrzysztof Grobelny #include "interfaces/types.hpp"
46ccfcbf5SKrzysztof Grobelny #include "utils/transform.hpp"
56ccfcbf5SKrzysztof Grobelny 
66ccfcbf5SKrzysztof Grobelny #include <algorithm>
76ccfcbf5SKrzysztof Grobelny 
8*e8fc5751SKrzysztof Grobelny Metric::Metric(std::shared_ptr<interfaces::Sensor> sensor,
9*e8fc5751SKrzysztof Grobelny                OperationType operationType, std::string id,
106ccfcbf5SKrzysztof Grobelny                std::string metadata) :
11*e8fc5751SKrzysztof Grobelny     sensor(std::move(sensor)),
12*e8fc5751SKrzysztof Grobelny     operationType(std::move(operationType)), reading{std::move(id),
13*e8fc5751SKrzysztof Grobelny                                                      std::move(metadata), 0.,
14*e8fc5751SKrzysztof Grobelny                                                      0u}
156ccfcbf5SKrzysztof Grobelny {}
166ccfcbf5SKrzysztof Grobelny 
176ccfcbf5SKrzysztof Grobelny void Metric::initialize()
186ccfcbf5SKrzysztof Grobelny {
196ccfcbf5SKrzysztof Grobelny     sensor->registerForUpdates(weak_from_this());
206ccfcbf5SKrzysztof Grobelny }
21*e8fc5751SKrzysztof Grobelny 
22*e8fc5751SKrzysztof Grobelny const MetricValue& Metric::getReading() const
23*e8fc5751SKrzysztof Grobelny {
24*e8fc5751SKrzysztof Grobelny     return reading;
256ccfcbf5SKrzysztof Grobelny }
266ccfcbf5SKrzysztof Grobelny 
27*e8fc5751SKrzysztof Grobelny void Metric::sensorUpdated(interfaces::Sensor& notifier, uint64_t timestamp)
286ccfcbf5SKrzysztof Grobelny {
29*e8fc5751SKrzysztof Grobelny     MetricValue& mv = findMetric(notifier);
306ccfcbf5SKrzysztof Grobelny     mv.timestamp = timestamp;
316ccfcbf5SKrzysztof Grobelny }
326ccfcbf5SKrzysztof Grobelny 
33*e8fc5751SKrzysztof Grobelny void Metric::sensorUpdated(interfaces::Sensor& notifier, uint64_t timestamp,
346ccfcbf5SKrzysztof Grobelny                            double value)
356ccfcbf5SKrzysztof Grobelny {
36*e8fc5751SKrzysztof Grobelny     MetricValue& mv = findMetric(notifier);
376ccfcbf5SKrzysztof Grobelny     mv.timestamp = timestamp;
386ccfcbf5SKrzysztof Grobelny     mv.value = value;
396ccfcbf5SKrzysztof Grobelny }
406ccfcbf5SKrzysztof Grobelny 
41*e8fc5751SKrzysztof Grobelny MetricValue& Metric::findMetric(interfaces::Sensor& notifier)
426ccfcbf5SKrzysztof Grobelny {
43*e8fc5751SKrzysztof Grobelny     if (sensor.get() != &notifier)
44*e8fc5751SKrzysztof Grobelny     {
45*e8fc5751SKrzysztof Grobelny         throw std::out_of_range("unknown sensor");
46*e8fc5751SKrzysztof Grobelny     }
47*e8fc5751SKrzysztof Grobelny     return reading;
486ccfcbf5SKrzysztof Grobelny }
496ccfcbf5SKrzysztof Grobelny 
50d2238194SKrzysztof Grobelny LabeledMetricParameters Metric::dumpConfiguration() const
516ccfcbf5SKrzysztof Grobelny {
52*e8fc5751SKrzysztof Grobelny     auto sensorPath =
53*e8fc5751SKrzysztof Grobelny         LabeledSensorParameters(sensor->id().service, sensor->id().path);
54*e8fc5751SKrzysztof Grobelny     return LabeledMetricParameters(std::move(sensorPath), operationType,
55*e8fc5751SKrzysztof Grobelny                                    reading.id, reading.metadata);
566ccfcbf5SKrzysztof Grobelny }
57