blob: 6734d28aa76751ed059bdc882ac8b422a9755147 [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"
4#include "utils/json.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{
71 try
72 {
73 const nlohmann::json parsedMetadata = nlohmann::json::parse(metadata);
74 if (const auto metricProperties =
75 utils::readJson<std::vector<std::string>>(parsedMetadata,
76 "MetricProperties"))
77 {
78 if (readings.size() == metricProperties->size())
79 {
80 for (size_t i = 0; i < readings.size(); ++i)
81 {
82 readings[i].metadata = (*metricProperties)[i];
83 }
84 }
85 }
86 }
87 catch (const nlohmann::json::parse_error& e)
88 {}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010089}