1c8e3a64aSKrzysztof Grobelny #include "metric.hpp" 2*6ccfcbf5SKrzysztof Grobelny 3*6ccfcbf5SKrzysztof Grobelny #include "interfaces/types.hpp" 4*6ccfcbf5SKrzysztof Grobelny #include "utils/transform.hpp" 5*6ccfcbf5SKrzysztof Grobelny 6*6ccfcbf5SKrzysztof Grobelny #include <algorithm> 7*6ccfcbf5SKrzysztof Grobelny 8*6ccfcbf5SKrzysztof Grobelny Metric::Metric(std::vector<std::shared_ptr<interfaces::Sensor>> sensors, 9*6ccfcbf5SKrzysztof Grobelny std::string operationType, std::string id, 10*6ccfcbf5SKrzysztof Grobelny std::string metadata) : 11*6ccfcbf5SKrzysztof Grobelny sensors(std::move(sensors)), 12*6ccfcbf5SKrzysztof Grobelny operationType(std::move(operationType)), id(std::move(id)), 13*6ccfcbf5SKrzysztof Grobelny metadata(std::move(metadata)) 14*6ccfcbf5SKrzysztof Grobelny {} 15*6ccfcbf5SKrzysztof Grobelny 16*6ccfcbf5SKrzysztof Grobelny void Metric::initialize() 17*6ccfcbf5SKrzysztof Grobelny { 18*6ccfcbf5SKrzysztof Grobelny readings = std::vector<MetricValue>(sensors.size(), 19*6ccfcbf5SKrzysztof Grobelny MetricValue{id, metadata, 0., 0u}); 20*6ccfcbf5SKrzysztof Grobelny 21*6ccfcbf5SKrzysztof Grobelny for (auto& sensor : sensors) 22*6ccfcbf5SKrzysztof Grobelny { 23*6ccfcbf5SKrzysztof Grobelny sensor->registerForUpdates(weak_from_this()); 24*6ccfcbf5SKrzysztof Grobelny } 25*6ccfcbf5SKrzysztof Grobelny } 26*6ccfcbf5SKrzysztof Grobelny 27*6ccfcbf5SKrzysztof Grobelny const std::vector<MetricValue>& Metric::getReadings() const 28*6ccfcbf5SKrzysztof Grobelny { 29*6ccfcbf5SKrzysztof Grobelny return readings; 30*6ccfcbf5SKrzysztof Grobelny } 31*6ccfcbf5SKrzysztof Grobelny 32*6ccfcbf5SKrzysztof Grobelny void Metric::sensorUpdated(interfaces::Sensor& sensor, uint64_t timestamp) 33*6ccfcbf5SKrzysztof Grobelny { 34*6ccfcbf5SKrzysztof Grobelny MetricValue& mv = findMetric(sensor); 35*6ccfcbf5SKrzysztof Grobelny mv.timestamp = timestamp; 36*6ccfcbf5SKrzysztof Grobelny } 37*6ccfcbf5SKrzysztof Grobelny 38*6ccfcbf5SKrzysztof Grobelny void Metric::sensorUpdated(interfaces::Sensor& sensor, uint64_t timestamp, 39*6ccfcbf5SKrzysztof Grobelny double value) 40*6ccfcbf5SKrzysztof Grobelny { 41*6ccfcbf5SKrzysztof Grobelny MetricValue& mv = findMetric(sensor); 42*6ccfcbf5SKrzysztof Grobelny mv.timestamp = timestamp; 43*6ccfcbf5SKrzysztof Grobelny mv.value = value; 44*6ccfcbf5SKrzysztof Grobelny } 45*6ccfcbf5SKrzysztof Grobelny 46*6ccfcbf5SKrzysztof Grobelny MetricValue& Metric::findMetric(interfaces::Sensor& sensor) 47*6ccfcbf5SKrzysztof Grobelny { 48*6ccfcbf5SKrzysztof Grobelny auto it = 49*6ccfcbf5SKrzysztof Grobelny std::find_if(sensors.begin(), sensors.end(), 50*6ccfcbf5SKrzysztof Grobelny [&sensor](const auto& s) { return s.get() == &sensor; }); 51*6ccfcbf5SKrzysztof Grobelny auto index = std::distance(sensors.begin(), it); 52*6ccfcbf5SKrzysztof Grobelny return readings.at(index); 53*6ccfcbf5SKrzysztof Grobelny } 54*6ccfcbf5SKrzysztof Grobelny 55*6ccfcbf5SKrzysztof Grobelny nlohmann::json Metric::to_json() const 56*6ccfcbf5SKrzysztof Grobelny { 57*6ccfcbf5SKrzysztof Grobelny auto sensorPaths = utils::transform( 58*6ccfcbf5SKrzysztof Grobelny sensors, [](const auto& sensor) -> sdbusplus::message::object_path { 59*6ccfcbf5SKrzysztof Grobelny return sdbusplus::message::object_path(sensor->id().service + ":" + 60*6ccfcbf5SKrzysztof Grobelny sensor->id().path); 61*6ccfcbf5SKrzysztof Grobelny }); 62*6ccfcbf5SKrzysztof Grobelny return LabeledReadingParameter::to_json(ReadingParameters::value_type( 63*6ccfcbf5SKrzysztof Grobelny std::move(sensorPaths), operationType, id, metadata)); 64*6ccfcbf5SKrzysztof Grobelny } 65