xref: /openbmc/bmcweb/redfish-core/lib/metric_report.hpp (revision 5a19396d081c5cb68d3e880529ecd552d1c4f5a0)
1 #pragma once
2 
3 #include "utils/collection.hpp"
4 #include "utils/telemetry_utils.hpp"
5 #include "utils/time_utils.hpp"
6 
7 #include <app.hpp>
8 #include <dbus_utility.hpp>
9 #include <query.hpp>
10 #include <registries/privilege_registry.hpp>
11 #include <sdbusplus/asio/property.hpp>
12 
13 namespace redfish
14 {
15 
16 namespace telemetry
17 {
18 
19 using Readings =
20     std::vector<std::tuple<std::string, std::string, double, uint64_t>>;
21 using TimestampReadings = std::tuple<uint64_t, Readings>;
22 
23 inline nlohmann::json toMetricValues(const Readings& readings)
24 {
25     nlohmann::json metricValues = nlohmann::json::array_t();
26 
27     for (const auto& [id, metadata, sensorValue, timestamp] : readings)
28     {
29         nlohmann::json::object_t metricReport;
30         metricReport["MetricId"] = id;
31         metricReport["MetricProperty"] = metadata;
32         metricReport["MetricValue"] = std::to_string(sensorValue);
33         metricReport["Timestamp"] =
34             redfish::time_utils::getDateTimeUintMs(timestamp);
35         metricValues.push_back(std::move(metricReport));
36     }
37 
38     return metricValues;
39 }
40 
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"] = crow::utility::urlFromPieces(
46         "redfish", "v1", "TelemetryService", "MetricReports", id);
47     json["Id"] = id;
48     json["Name"] = id;
49     json["MetricReportDefinition"]["@odata.id"] = crow::utility::urlFromPieces(
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 } // namespace telemetry
58 
59 inline void requestRoutesMetricReportCollection(App& app)
60 {
61     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/")
62         .privileges(redfish::privileges::getMetricReportCollection)
63         .methods(boost::beast::http::verb::get)(
64             [&app](const crow::Request& req,
65                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
66         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
67         {
68             return;
69         }
70 
71         asyncResp->res.jsonValue["@odata.type"] =
72             "#MetricReportCollection.MetricReportCollection";
73         asyncResp->res.jsonValue["@odata.id"] =
74             "/redfish/v1/TelemetryService/MetricReports";
75         asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
76         const std::vector<const char*> interfaces{telemetry::reportInterface};
77         collection_util::getCollectionMembers(
78             asyncResp,
79             boost::urls::url("/redfish/v1/TelemetryService/MetricReports"),
80             interfaces,
81             "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
82         });
83 }
84 
85 inline void requestRoutesMetricReport(App& app)
86 {
87     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/<str>/")
88         .privileges(redfish::privileges::getMetricReport)
89         .methods(boost::beast::http::verb::get)(
90             [&app](const crow::Request& req,
91                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
92                    const std::string& id) {
93         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
94         {
95             return;
96         }
97         const std::string reportPath = telemetry::getDbusReportPath(id);
98         crow::connections::systemBus->async_method_call(
99             [asyncResp, id, reportPath](const boost::system::error_code& ec) {
100             if (ec.value() == EBADR ||
101                 ec == boost::system::errc::host_unreachable)
102             {
103                 messages::resourceNotFound(asyncResp->res, "MetricReport", id);
104                 return;
105             }
106             if (ec)
107             {
108                 BMCWEB_LOG_ERROR << "respHandler DBus error " << ec;
109                 messages::internalError(asyncResp->res);
110                 return;
111             }
112 
113             sdbusplus::asio::getProperty<telemetry::TimestampReadings>(
114                 *crow::connections::systemBus, telemetry::service, reportPath,
115                 telemetry::reportInterface, "Readings",
116                 [asyncResp, id](const boost::system::error_code ec2,
117                                 const telemetry::TimestampReadings& ret) {
118                 if (ec2)
119                 {
120                     BMCWEB_LOG_ERROR << "respHandler DBus error " << ec2;
121                     messages::internalError(asyncResp->res);
122                     return;
123                 }
124 
125                 telemetry::fillReport(asyncResp->res.jsonValue, id, ret);
126                 });
127             },
128             telemetry::service, reportPath, telemetry::reportInterface,
129             "Update");
130         });
131 }
132 } // namespace redfish
133