xref: /openbmc/telemetry/src/metric.cpp (revision d22381949a8705da399107fcff71f8fbfe910f46)
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 
86ccfcbf5SKrzysztof Grobelny Metric::Metric(std::vector<std::shared_ptr<interfaces::Sensor>> sensors,
96ccfcbf5SKrzysztof Grobelny                std::string operationType, std::string id,
106ccfcbf5SKrzysztof Grobelny                std::string metadata) :
116ccfcbf5SKrzysztof Grobelny     sensors(std::move(sensors)),
126ccfcbf5SKrzysztof Grobelny     operationType(std::move(operationType)), id(std::move(id)),
136ccfcbf5SKrzysztof Grobelny     metadata(std::move(metadata))
146ccfcbf5SKrzysztof Grobelny {}
156ccfcbf5SKrzysztof Grobelny 
166ccfcbf5SKrzysztof Grobelny void Metric::initialize()
176ccfcbf5SKrzysztof Grobelny {
186ccfcbf5SKrzysztof Grobelny     readings = std::vector<MetricValue>(sensors.size(),
196ccfcbf5SKrzysztof Grobelny                                         MetricValue{id, metadata, 0., 0u});
206ccfcbf5SKrzysztof Grobelny 
216ccfcbf5SKrzysztof Grobelny     for (auto& sensor : sensors)
226ccfcbf5SKrzysztof Grobelny     {
236ccfcbf5SKrzysztof Grobelny         sensor->registerForUpdates(weak_from_this());
246ccfcbf5SKrzysztof Grobelny     }
256ccfcbf5SKrzysztof Grobelny }
266ccfcbf5SKrzysztof Grobelny 
276ccfcbf5SKrzysztof Grobelny const std::vector<MetricValue>& Metric::getReadings() const
286ccfcbf5SKrzysztof Grobelny {
296ccfcbf5SKrzysztof Grobelny     return readings;
306ccfcbf5SKrzysztof Grobelny }
316ccfcbf5SKrzysztof Grobelny 
326ccfcbf5SKrzysztof Grobelny void Metric::sensorUpdated(interfaces::Sensor& sensor, uint64_t timestamp)
336ccfcbf5SKrzysztof Grobelny {
346ccfcbf5SKrzysztof Grobelny     MetricValue& mv = findMetric(sensor);
356ccfcbf5SKrzysztof Grobelny     mv.timestamp = timestamp;
366ccfcbf5SKrzysztof Grobelny }
376ccfcbf5SKrzysztof Grobelny 
386ccfcbf5SKrzysztof Grobelny void Metric::sensorUpdated(interfaces::Sensor& sensor, uint64_t timestamp,
396ccfcbf5SKrzysztof Grobelny                            double value)
406ccfcbf5SKrzysztof Grobelny {
416ccfcbf5SKrzysztof Grobelny     MetricValue& mv = findMetric(sensor);
426ccfcbf5SKrzysztof Grobelny     mv.timestamp = timestamp;
436ccfcbf5SKrzysztof Grobelny     mv.value = value;
446ccfcbf5SKrzysztof Grobelny }
456ccfcbf5SKrzysztof Grobelny 
466ccfcbf5SKrzysztof Grobelny MetricValue& Metric::findMetric(interfaces::Sensor& sensor)
476ccfcbf5SKrzysztof Grobelny {
486ccfcbf5SKrzysztof Grobelny     auto it =
496ccfcbf5SKrzysztof Grobelny         std::find_if(sensors.begin(), sensors.end(),
506ccfcbf5SKrzysztof Grobelny                      [&sensor](const auto& s) { return s.get() == &sensor; });
516ccfcbf5SKrzysztof Grobelny     auto index = std::distance(sensors.begin(), it);
526ccfcbf5SKrzysztof Grobelny     return readings.at(index);
536ccfcbf5SKrzysztof Grobelny }
546ccfcbf5SKrzysztof Grobelny 
55*d2238194SKrzysztof Grobelny LabeledMetricParameters Metric::dumpConfiguration() const
566ccfcbf5SKrzysztof Grobelny {
57*d2238194SKrzysztof Grobelny     auto sensorPaths = utils::transform(sensors, [](const auto& sensor) {
58*d2238194SKrzysztof Grobelny         return LabeledSensorParameters(sensor->id().service, sensor->id().path);
596ccfcbf5SKrzysztof Grobelny     });
60*d2238194SKrzysztof Grobelny     return LabeledMetricParameters(std::move(sensorPaths), operationType, id,
61*d2238194SKrzysztof Grobelny                                    metadata);
626ccfcbf5SKrzysztof Grobelny }
63