From 1920d507c28baa6ca9ec3de3694a2df218a4f265 Mon Sep 17 00:00:00 2001 From: eportnov Date: Thu, 8 Sep 2022 14:25:36 +0300 Subject: [PATCH] configure telemetry --- include/dbus_utility.hpp | 1 + redfish-core/lib/metric_report.hpp | 39 +++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/include/dbus_utility.hpp b/include/dbus_utility.hpp index bd0d64bd..05adb1e6 100644 --- a/include/dbus_utility.hpp +++ b/include/dbus_utility.hpp @@ -62,6 +62,7 @@ using DbusVariantType = std::variant< std::vector, sdbusplus::message::object_path, std::tuple>>, + std::tuple>>, std::vector>, std::vector>>, std::vector>, diff --git a/redfish-core/lib/metric_report.hpp b/redfish-core/lib/metric_report.hpp index 18830f13..b3f5721e 100644 --- a/redfish-core/lib/metric_report.hpp +++ b/redfish-core/lib/metric_report.hpp @@ -20,20 +20,47 @@ constexpr const char* metricReportUri = "/redfish/v1/TelemetryService/MetricReports"; using Readings = - std::vector>; + std::vector>; using TimestampReadings = std::tuple; +inline std::string GetCroppedTimeStamp(uint64_t timestamp) +{ + static constexpr char DOT_CHAR = '.'; + + auto timestamp_as_string = redfish::time_utils::getDateTimeUintMs(timestamp); + auto last_dot_position = timestamp_as_string.rfind(DOT_CHAR); + if(last_dot_position != std::string::npos) + { + timestamp_as_string.erase(last_dot_position); + } + return timestamp_as_string; +} + +inline std::string GetCroppedValue(double value) +{ + static constexpr char DOT_CHAR = '.'; + static constexpr size_t COUNT_NUMBER_AFTER_DOT = 1; + + auto value_as_string = std::to_string(value); + auto last_dot_position = value_as_string.rfind(DOT_CHAR); + auto general_size = last_dot_position + COUNT_NUMBER_AFTER_DOT + sizeof(DOT_CHAR); + if(last_dot_position != std::string::npos && general_size <= value_as_string.length()) + { + value_as_string.erase(general_size); + } + return value_as_string; +} + inline nlohmann::json toMetricValues(const Readings& readings) { nlohmann::json metricValues = nlohmann::json::array_t(); - for (const auto& [id, metadata, sensorValue, timestamp] : readings) + for (const auto& [metadata, sensorValue, timestamp] : readings) { metricValues.push_back({ - {"MetricId", id}, - {"MetricProperty", metadata}, - {"MetricValue", std::to_string(sensorValue)}, - {"Timestamp", redfish::time_utils::getDateTimeUintMs(timestamp)}, + {"Sensor", metadata}, + {"Value", GetCroppedValue(sensorValue)}, + {"Timestamp", GetCroppedTimeStamp(timestamp)}, }); } @@ -56,7 +83,7 @@ inline bool fillReport(nlohmann::json& json, const std::string& id, .string(); const auto& [timestamp, readings] = timestampReadings; - json["Timestamp"] = redfish::time_utils::getDateTimeUintMs(timestamp); + json["Timestamp"] = GetCroppedTimeStamp(timestamp); json["MetricValues"] = toMetricValues(readings); return true; }