xref: /openbmc/bmcweb/features/redfish/lib/metric_report.hpp (revision 1e1e598df6d1d9530dde6e92d8f74f8143f60e50)
1081ebf06SWludzik, Jozef #pragma once
2081ebf06SWludzik, Jozef 
34028ff77SEd Tanous #include "utils/collection.hpp"
4081ebf06SWludzik, Jozef #include "utils/telemetry_utils.hpp"
5081ebf06SWludzik, Jozef 
67e860f15SJohn Edward Broadbent #include <app.hpp>
7168e20c1SEd Tanous #include <dbus_utility.hpp>
8ed398213SEd Tanous #include <registries/privilege_registry.hpp>
9*1e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
107e860f15SJohn Edward Broadbent 
11081ebf06SWludzik, Jozef namespace redfish
12081ebf06SWludzik, Jozef {
13081ebf06SWludzik, Jozef 
14081ebf06SWludzik, Jozef namespace telemetry
15081ebf06SWludzik, Jozef {
16081ebf06SWludzik, Jozef 
17081ebf06SWludzik, Jozef using Readings =
18081ebf06SWludzik, Jozef     std::vector<std::tuple<std::string, std::string, double, uint64_t>>;
19081ebf06SWludzik, Jozef using TimestampReadings = std::tuple<uint64_t, Readings>;
20081ebf06SWludzik, Jozef 
21081ebf06SWludzik, Jozef inline nlohmann::json toMetricValues(const Readings& readings)
22081ebf06SWludzik, Jozef {
23081ebf06SWludzik, Jozef     nlohmann::json metricValues = nlohmann::json::array_t();
24081ebf06SWludzik, Jozef 
25081ebf06SWludzik, Jozef     for (auto& [id, metadata, sensorValue, timestamp] : readings)
26081ebf06SWludzik, Jozef     {
27081ebf06SWludzik, Jozef         metricValues.push_back({
28081ebf06SWludzik, Jozef             {"MetricId", id},
29081ebf06SWludzik, Jozef             {"MetricProperty", metadata},
30081ebf06SWludzik, Jozef             {"MetricValue", std::to_string(sensorValue)},
311d8782e7SNan Zhou             {"Timestamp", crow::utility::getDateTimeUint(timestamp)},
32081ebf06SWludzik, Jozef         });
33081ebf06SWludzik, Jozef     }
34081ebf06SWludzik, Jozef 
35081ebf06SWludzik, Jozef     return metricValues;
36081ebf06SWludzik, Jozef }
37081ebf06SWludzik, Jozef 
38c0353249SWludzik, Jozef inline bool fillReport(nlohmann::json& json, const std::string& id,
39*1e1e598dSJonathan Doman                        const TimestampReadings& timestampReadings)
40081ebf06SWludzik, Jozef {
41c0353249SWludzik, Jozef     json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport";
42c0353249SWludzik, Jozef     json["@odata.id"] = telemetry::metricReportUri + std::string("/") + id;
43c0353249SWludzik, Jozef     json["Id"] = id;
44c0353249SWludzik, Jozef     json["Name"] = id;
45c0353249SWludzik, Jozef     json["MetricReportDefinition"]["@odata.id"] =
464028ff77SEd Tanous         telemetry::metricReportDefinitionUri + std::string("/") + id;
47081ebf06SWludzik, Jozef 
48*1e1e598dSJonathan Doman     const auto& [timestamp, readings] = timestampReadings;
491d8782e7SNan Zhou     json["Timestamp"] = crow::utility::getDateTimeUint(timestamp);
50c0353249SWludzik, Jozef     json["MetricValues"] = toMetricValues(readings);
51c0353249SWludzik, Jozef     return true;
52081ebf06SWludzik, Jozef }
53081ebf06SWludzik, Jozef } // namespace telemetry
54081ebf06SWludzik, Jozef 
557e860f15SJohn Edward Broadbent inline void requestRoutesMetricReportCollection(App& app)
56081ebf06SWludzik, Jozef {
577e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/")
58ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReportCollection)
597e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
607e860f15SJohn Edward Broadbent             [](const crow::Request&,
617e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
628d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
63081ebf06SWludzik, Jozef                     "#MetricReportCollection.MetricReportCollection";
648d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
65081ebf06SWludzik, Jozef                     "/redfish/v1/TelemetryService/MetricReports";
668d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
674028ff77SEd Tanous                 const std::vector<const char*> interfaces{
684028ff77SEd Tanous                     telemetry::reportInterface};
694028ff77SEd Tanous                 collection_util::getCollectionMembers(
704028ff77SEd Tanous                     asyncResp, telemetry::metricReportUri, interfaces,
714028ff77SEd Tanous                     "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
727e860f15SJohn Edward Broadbent             });
73081ebf06SWludzik, Jozef }
74081ebf06SWludzik, Jozef 
757e860f15SJohn Edward Broadbent inline void requestRoutesMetricReport(App& app)
76081ebf06SWludzik, Jozef {
777e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/<str>/")
78ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReport)
797e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
807e860f15SJohn Edward Broadbent             [](const crow::Request&,
817e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
827e860f15SJohn Edward Broadbent                const std::string& id) {
83081ebf06SWludzik, Jozef                 const std::string reportPath = telemetry::getDbusReportPath(id);
84081ebf06SWludzik, Jozef                 crow::connections::systemBus->async_method_call(
857e860f15SJohn Edward Broadbent                     [asyncResp, id,
867e860f15SJohn Edward Broadbent                      reportPath](const boost::system::error_code& ec) {
87081ebf06SWludzik, Jozef                         if (ec.value() == EBADR ||
88081ebf06SWludzik, Jozef                             ec == boost::system::errc::host_unreachable)
89081ebf06SWludzik, Jozef                         {
907e860f15SJohn Edward Broadbent                             messages::resourceNotFound(asyncResp->res,
917e860f15SJohn Edward Broadbent                                                        "MetricReport", id);
92081ebf06SWludzik, Jozef                             return;
93081ebf06SWludzik, Jozef                         }
94081ebf06SWludzik, Jozef                         if (ec)
95081ebf06SWludzik, Jozef                         {
96081ebf06SWludzik, Jozef                             BMCWEB_LOG_ERROR << "respHandler DBus error " << ec;
97081ebf06SWludzik, Jozef                             messages::internalError(asyncResp->res);
98081ebf06SWludzik, Jozef                             return;
99081ebf06SWludzik, Jozef                         }
100081ebf06SWludzik, Jozef 
101*1e1e598dSJonathan Doman                         sdbusplus::asio::getProperty<
102*1e1e598dSJonathan Doman                             telemetry::TimestampReadings>(
103*1e1e598dSJonathan Doman                             *crow::connections::systemBus, telemetry::service,
104*1e1e598dSJonathan Doman                             reportPath, telemetry::reportInterface, "Readings",
1057e860f15SJohn Edward Broadbent                             [asyncResp,
1067e860f15SJohn Edward Broadbent                              id](const boost::system::error_code ec,
107*1e1e598dSJonathan Doman                                  const telemetry::TimestampReadings& ret) {
108081ebf06SWludzik, Jozef                                 if (ec)
109081ebf06SWludzik, Jozef                                 {
1107e860f15SJohn Edward Broadbent                                     BMCWEB_LOG_ERROR
1117e860f15SJohn Edward Broadbent                                         << "respHandler DBus error " << ec;
112081ebf06SWludzik, Jozef                                     messages::internalError(asyncResp->res);
113081ebf06SWludzik, Jozef                                     return;
114081ebf06SWludzik, Jozef                                 }
115081ebf06SWludzik, Jozef 
116*1e1e598dSJonathan Doman                                 telemetry::fillReport(asyncResp->res.jsonValue,
117*1e1e598dSJonathan Doman                                                       id, ret);
118*1e1e598dSJonathan Doman                             });
119081ebf06SWludzik, Jozef                     },
120081ebf06SWludzik, Jozef                     telemetry::service, reportPath, telemetry::reportInterface,
121081ebf06SWludzik, Jozef                     "Update");
1227e860f15SJohn Edward Broadbent             });
123081ebf06SWludzik, Jozef }
124081ebf06SWludzik, Jozef } // namespace redfish
125