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