xref: /openbmc/bmcweb/features/redfish/lib/metric_report.hpp (revision 079360ae6e04d3f2245e00d70f83d15c5cad3630)
1081ebf06SWludzik, Jozef #pragma once
2081ebf06SWludzik, Jozef 
34028ff77SEd Tanous #include "utils/collection.hpp"
4081ebf06SWludzik, Jozef #include "utils/telemetry_utils.hpp"
52b82937eSEd Tanous #include "utils/time_utils.hpp"
6081ebf06SWludzik, Jozef 
77e860f15SJohn Edward Broadbent #include <app.hpp>
8168e20c1SEd Tanous #include <dbus_utility.hpp>
945ca1b86SEd Tanous #include <query.hpp>
10ed398213SEd Tanous #include <registries/privilege_registry.hpp>
111e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
127e860f15SJohn Edward Broadbent 
13081ebf06SWludzik, Jozef namespace redfish
14081ebf06SWludzik, Jozef {
15081ebf06SWludzik, Jozef 
16081ebf06SWludzik, Jozef namespace telemetry
17081ebf06SWludzik, Jozef {
18081ebf06SWludzik, Jozef 
19081ebf06SWludzik, Jozef using Readings =
20081ebf06SWludzik, Jozef     std::vector<std::tuple<std::string, std::string, double, uint64_t>>;
21081ebf06SWludzik, Jozef using TimestampReadings = std::tuple<uint64_t, Readings>;
22081ebf06SWludzik, Jozef 
23081ebf06SWludzik, Jozef inline nlohmann::json toMetricValues(const Readings& readings)
24081ebf06SWludzik, Jozef {
25081ebf06SWludzik, Jozef     nlohmann::json metricValues = nlohmann::json::array_t();
26081ebf06SWludzik, Jozef 
279eb808c1SEd Tanous     for (const auto& [id, metadata, sensorValue, timestamp] : readings)
28081ebf06SWludzik, Jozef     {
29613dabeaSEd Tanous         nlohmann::json::object_t metricReport;
30613dabeaSEd Tanous         metricReport["MetricId"] = id;
31613dabeaSEd Tanous         metricReport["MetricProperty"] = metadata;
32613dabeaSEd Tanous         metricReport["MetricValue"] = std::to_string(sensorValue);
33613dabeaSEd Tanous         metricReport["Timestamp"] =
34613dabeaSEd Tanous             redfish::time_utils::getDateTimeUintMs(timestamp);
35613dabeaSEd Tanous         metricValues.push_back(std::move(metricReport));
36081ebf06SWludzik, Jozef     }
37081ebf06SWludzik, Jozef 
38081ebf06SWludzik, Jozef     return metricValues;
39081ebf06SWludzik, Jozef }
40081ebf06SWludzik, Jozef 
41c0353249SWludzik, Jozef inline bool fillReport(nlohmann::json& json, const std::string& id,
421e1e598dSJonathan Doman                        const TimestampReadings& timestampReadings)
43081ebf06SWludzik, Jozef {
44c0353249SWludzik, Jozef     json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport";
45*079360aeSEd Tanous     json["@odata.id"] = crow::utility::urlFromPieces(
46*079360aeSEd Tanous         "redfish", "v1", "TelemetryService", "MetricReports", id);
47c0353249SWludzik, Jozef     json["Id"] = id;
48c0353249SWludzik, Jozef     json["Name"] = id;
49*079360aeSEd Tanous     json["MetricReportDefinition"]["@odata.id"] = crow::utility::urlFromPieces(
50*079360aeSEd Tanous         "redfish", "v1", "TelemetryService", "MetricReportDefinitions", id);
51081ebf06SWludzik, Jozef 
521e1e598dSJonathan Doman     const auto& [timestamp, readings] = timestampReadings;
532b82937eSEd Tanous     json["Timestamp"] = redfish::time_utils::getDateTimeUintMs(timestamp);
54c0353249SWludzik, Jozef     json["MetricValues"] = toMetricValues(readings);
55c0353249SWludzik, Jozef     return true;
56081ebf06SWludzik, Jozef }
57081ebf06SWludzik, Jozef } // namespace telemetry
58081ebf06SWludzik, Jozef 
597e860f15SJohn Edward Broadbent inline void requestRoutesMetricReportCollection(App& app)
60081ebf06SWludzik, Jozef {
617e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/")
62ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReportCollection)
637e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
6445ca1b86SEd Tanous             [&app](const crow::Request& req,
657e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
663ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
6745ca1b86SEd Tanous         {
6845ca1b86SEd Tanous             return;
6945ca1b86SEd Tanous         }
7045ca1b86SEd Tanous 
718d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
72081ebf06SWludzik, Jozef             "#MetricReportCollection.MetricReportCollection";
73ae9031f0SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
74ae9031f0SWilly Tu             "/redfish/v1/TelemetryService/MetricReports";
758d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
76002d39b4SEd Tanous         const std::vector<const char*> interfaces{telemetry::reportInterface};
774028ff77SEd Tanous         collection_util::getCollectionMembers(
78ae9031f0SWilly Tu             asyncResp,
79ae9031f0SWilly Tu             boost::urls::url("/redfish/v1/TelemetryService/MetricReports"),
80ae9031f0SWilly Tu             interfaces,
814028ff77SEd Tanous             "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
827e860f15SJohn Edward Broadbent         });
83081ebf06SWludzik, Jozef }
84081ebf06SWludzik, Jozef 
857e860f15SJohn Edward Broadbent inline void requestRoutesMetricReport(App& app)
86081ebf06SWludzik, Jozef {
877e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/<str>/")
88ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReport)
897e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
9045ca1b86SEd Tanous             [&app](const crow::Request& req,
917e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
927e860f15SJohn Edward Broadbent                    const std::string& id) {
933ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
9445ca1b86SEd Tanous         {
9545ca1b86SEd Tanous             return;
9645ca1b86SEd Tanous         }
97081ebf06SWludzik, Jozef         const std::string reportPath = telemetry::getDbusReportPath(id);
98081ebf06SWludzik, Jozef         crow::connections::systemBus->async_method_call(
99002d39b4SEd Tanous             [asyncResp, id, reportPath](const boost::system::error_code& ec) {
100081ebf06SWludzik, Jozef             if (ec.value() == EBADR ||
101081ebf06SWludzik, Jozef                 ec == boost::system::errc::host_unreachable)
102081ebf06SWludzik, Jozef             {
103002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "MetricReport", id);
104081ebf06SWludzik, Jozef                 return;
105081ebf06SWludzik, Jozef             }
106081ebf06SWludzik, Jozef             if (ec)
107081ebf06SWludzik, Jozef             {
108081ebf06SWludzik, Jozef                 BMCWEB_LOG_ERROR << "respHandler DBus error " << ec;
109081ebf06SWludzik, Jozef                 messages::internalError(asyncResp->res);
110081ebf06SWludzik, Jozef                 return;
111081ebf06SWludzik, Jozef             }
112081ebf06SWludzik, Jozef 
113002d39b4SEd Tanous             sdbusplus::asio::getProperty<telemetry::TimestampReadings>(
114002d39b4SEd Tanous                 *crow::connections::systemBus, telemetry::service, reportPath,
115002d39b4SEd Tanous                 telemetry::reportInterface, "Readings",
1168a592810SEd Tanous                 [asyncResp, id](const boost::system::error_code ec2,
1171e1e598dSJonathan Doman                                 const telemetry::TimestampReadings& ret) {
1188a592810SEd Tanous                 if (ec2)
119081ebf06SWludzik, Jozef                 {
1208a592810SEd Tanous                     BMCWEB_LOG_ERROR << "respHandler DBus error " << ec2;
121081ebf06SWludzik, Jozef                     messages::internalError(asyncResp->res);
122081ebf06SWludzik, Jozef                     return;
123081ebf06SWludzik, Jozef                 }
124081ebf06SWludzik, Jozef 
125002d39b4SEd Tanous                 telemetry::fillReport(asyncResp->res.jsonValue, id, ret);
1261e1e598dSJonathan Doman                 });
127081ebf06SWludzik, Jozef             },
128081ebf06SWludzik, Jozef             telemetry::service, reportPath, telemetry::reportInterface,
129081ebf06SWludzik, Jozef             "Update");
1307e860f15SJohn Edward Broadbent         });
131081ebf06SWludzik, Jozef }
132081ebf06SWludzik, Jozef } // namespace redfish
133