blob: 9dd4e6724a63daa93818a54507d04c3cc6db1663 [file] [log] [blame]
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02001#include "metric.hpp"
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01002
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00003#include "types/report_types.hpp"
Szymon Dompke3a617022021-07-19 18:23:02 +02004#include "utils/labeled_tuple.hpp"
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01005#include "utils/transform.hpp"
6
7#include <algorithm>
8
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00009Metric::Metric(Sensors sensorsIn, OperationType operationTypeIn,
10 std::string idIn, std::string metadataIn,
11 CollectionTimeScope timeScopeIn,
12 CollectionDuration collectionDurationIn) :
13 id(idIn),
14 metadata(metadataIn),
15 readings(sensorsIn.size(),
16 MetricValue{std::move(idIn), std::move(metadataIn), 0., 0u}),
17 sensors(std::move(sensorsIn)), operationType(operationTypeIn),
18 timeScope(timeScopeIn), collectionDuration(collectionDurationIn)
19{
20 tryUnpackJsonMetadata();
21}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010022
23void Metric::initialize()
24{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000025 for (const auto& sensor : sensors)
26 {
27 sensor->registerForUpdates(weak_from_this());
28 }
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010029}
30
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000031const std::vector<MetricValue>& Metric::getReadings() const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010032{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000033 return readings;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010034}
35
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000036void Metric::sensorUpdated(interfaces::Sensor& notifier, uint64_t timestamp)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010037{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000038 MetricValue& mv = findMetric(notifier);
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010039 mv.timestamp = timestamp;
40}
41
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000042void Metric::sensorUpdated(interfaces::Sensor& notifier, uint64_t timestamp,
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010043 double value)
44{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000045 MetricValue& mv = findMetric(notifier);
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010046 mv.timestamp = timestamp;
47 mv.value = value;
48}
49
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000050MetricValue& Metric::findMetric(interfaces::Sensor& notifier)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010051{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000052 auto it = std::find_if(
53 sensors.begin(), sensors.end(),
54 [&notifier](const auto& sensor) { return sensor.get() == &notifier; });
55 auto index = std::distance(sensors.begin(), it);
56 return readings.at(index);
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010057}
58
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000059LabeledMetricParameters Metric::dumpConfiguration() const
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010060{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000061 auto sensorPath = utils::transform(sensors, [this](const auto& sensor) {
62 return LabeledSensorParameters(sensor->id().service, sensor->id().path);
63 });
64
65 return LabeledMetricParameters(std::move(sensorPath), operationType, id,
66 metadata, timeScope, collectionDuration);
67}
68
69void Metric::tryUnpackJsonMetadata()
70{
Szymon Dompke3a617022021-07-19 18:23:02 +020071 using MetricMetadata =
72 utils::LabeledTuple<std::tuple<std::vector<std::string>>,
73 utils::tstring::MetricProperties>;
74
75 using ReadingMetadata =
76 utils::LabeledTuple<std::tuple<std::string, std::string>,
77 utils::tstring::SensorDbusPath,
78 utils::tstring::SensorRedfishUri>;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000079 try
80 {
Szymon Dompke3a617022021-07-19 18:23:02 +020081 const MetricMetadata parsedMetadata =
82 nlohmann::json::parse(metadata).get<MetricMetadata>();
83
84 if (readings.size() == parsedMetadata.at_index<0>().size())
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000085 {
Szymon Dompke3a617022021-07-19 18:23:02 +020086 for (size_t i = 0; i < readings.size(); ++i)
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000087 {
Szymon Dompke3a617022021-07-19 18:23:02 +020088 ReadingMetadata readingMetadata{
89 sensors[i]->id().path, parsedMetadata.at_index<0>()[i]};
90 readings[i].metadata = readingMetadata.dump();
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000091 }
92 }
93 }
Szymon Dompke3a617022021-07-19 18:23:02 +020094 catch (const nlohmann::json::parse_error&)
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000095 {}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010096}