xref: /openbmc/bmcweb/redfish-core/include/telemetry_readings.hpp (revision f86cdd7d287caa93d6508114d77e058937a2bf4a)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 
4 #pragma once
5 
6 #include "utils/time_utils.hpp"
7 
8 #include <boost/url/format.hpp>
9 #include <nlohmann/json.hpp>
10 
11 #include <cstdint>
12 #include <string>
13 #include <tuple>
14 #include <vector>
15 
16 namespace redfish
17 {
18 namespace telemetry
19 {
20 
21 using Readings = std::vector<std::tuple<std::string, double, uint64_t>>;
22 using TimestampReadings = std::tuple<uint64_t, Readings>;
23 
toMetricValues(const Readings & readings)24 inline nlohmann::json toMetricValues(const Readings& readings)
25 {
26     nlohmann::json metricValues = nlohmann::json::array_t();
27 
28     for (const auto& [metadata, sensorValue, timestamp] : readings)
29     {
30         nlohmann::json::object_t metricReport;
31         metricReport["MetricProperty"] = metadata;
32         metricReport["MetricValue"] = std::to_string(sensorValue);
33         metricReport["Timestamp"] =
34             redfish::time_utils::getDateTimeUintMs(timestamp);
35         metricValues.emplace_back(std::move(metricReport));
36     }
37 
38     return metricValues;
39 }
40 
fillReport(nlohmann::json & json,const std::string & id,const TimestampReadings & timestampReadings)41 inline bool fillReport(nlohmann::json& json, const std::string& id,
42                        const TimestampReadings& timestampReadings)
43 {
44     json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport";
45     json["@odata.id"] = boost::urls::format(
46         "/redfish/v1/TelemetryService/MetricReports/{}", id);
47     json["Id"] = id;
48     json["Name"] = id;
49     json["MetricReportDefinition"]["@odata.id"] = boost::urls::format(
50         "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", id);
51 
52     const auto& [timestamp, readings] = timestampReadings;
53     json["Timestamp"] = redfish::time_utils::getDateTimeUintMs(timestamp);
54     json["MetricValues"] = toMetricValues(readings);
55     return true;
56 }
57 
58 } // namespace telemetry
59 } // namespace redfish
60