xref: /openbmc/bmcweb/features/redfish/lib/metric_report.hpp (revision 3ccb3adb9a14783f6bef601506de9f8bcae22d51)
1081ebf06SWludzik, Jozef #pragma once
2081ebf06SWludzik, Jozef 
3*3ccb3adbSEd Tanous #include "app.hpp"
4*3ccb3adbSEd Tanous #include "dbus_utility.hpp"
5*3ccb3adbSEd Tanous #include "query.hpp"
6*3ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
74028ff77SEd Tanous #include "utils/collection.hpp"
8081ebf06SWludzik, Jozef #include "utils/telemetry_utils.hpp"
92b82937eSEd Tanous #include "utils/time_utils.hpp"
10081ebf06SWludzik, Jozef 
111e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
127e860f15SJohn Edward Broadbent 
137a1dbc48SGeorge Liu #include <array>
147a1dbc48SGeorge Liu #include <string_view>
157a1dbc48SGeorge Liu 
16081ebf06SWludzik, Jozef namespace redfish
17081ebf06SWludzik, Jozef {
18081ebf06SWludzik, Jozef 
19081ebf06SWludzik, Jozef namespace telemetry
20081ebf06SWludzik, Jozef {
21081ebf06SWludzik, Jozef 
22081ebf06SWludzik, Jozef using Readings =
23081ebf06SWludzik, Jozef     std::vector<std::tuple<std::string, std::string, double, uint64_t>>;
24081ebf06SWludzik, Jozef using TimestampReadings = std::tuple<uint64_t, Readings>;
25081ebf06SWludzik, Jozef 
26081ebf06SWludzik, Jozef inline nlohmann::json toMetricValues(const Readings& readings)
27081ebf06SWludzik, Jozef {
28081ebf06SWludzik, Jozef     nlohmann::json metricValues = nlohmann::json::array_t();
29081ebf06SWludzik, Jozef 
309eb808c1SEd Tanous     for (const auto& [id, metadata, sensorValue, timestamp] : readings)
31081ebf06SWludzik, Jozef     {
32613dabeaSEd Tanous         nlohmann::json::object_t metricReport;
33613dabeaSEd Tanous         metricReport["MetricId"] = id;
34613dabeaSEd Tanous         metricReport["MetricProperty"] = metadata;
35613dabeaSEd Tanous         metricReport["MetricValue"] = std::to_string(sensorValue);
36613dabeaSEd Tanous         metricReport["Timestamp"] =
37613dabeaSEd Tanous             redfish::time_utils::getDateTimeUintMs(timestamp);
38613dabeaSEd Tanous         metricValues.push_back(std::move(metricReport));
39081ebf06SWludzik, Jozef     }
40081ebf06SWludzik, Jozef 
41081ebf06SWludzik, Jozef     return metricValues;
42081ebf06SWludzik, Jozef }
43081ebf06SWludzik, Jozef 
44c0353249SWludzik, Jozef inline bool fillReport(nlohmann::json& json, const std::string& id,
451e1e598dSJonathan Doman                        const TimestampReadings& timestampReadings)
46081ebf06SWludzik, Jozef {
47c0353249SWludzik, Jozef     json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport";
48079360aeSEd Tanous     json["@odata.id"] = crow::utility::urlFromPieces(
49079360aeSEd Tanous         "redfish", "v1", "TelemetryService", "MetricReports", id);
50c0353249SWludzik, Jozef     json["Id"] = id;
51c0353249SWludzik, Jozef     json["Name"] = id;
52079360aeSEd Tanous     json["MetricReportDefinition"]["@odata.id"] = crow::utility::urlFromPieces(
53079360aeSEd Tanous         "redfish", "v1", "TelemetryService", "MetricReportDefinitions", id);
54081ebf06SWludzik, Jozef 
551e1e598dSJonathan Doman     const auto& [timestamp, readings] = timestampReadings;
562b82937eSEd Tanous     json["Timestamp"] = redfish::time_utils::getDateTimeUintMs(timestamp);
57c0353249SWludzik, Jozef     json["MetricValues"] = toMetricValues(readings);
58c0353249SWludzik, Jozef     return true;
59081ebf06SWludzik, Jozef }
60081ebf06SWludzik, Jozef } // namespace telemetry
61081ebf06SWludzik, Jozef 
627e860f15SJohn Edward Broadbent inline void requestRoutesMetricReportCollection(App& app)
63081ebf06SWludzik, Jozef {
647e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/")
65ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReportCollection)
667e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
6745ca1b86SEd Tanous             [&app](const crow::Request& req,
687e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
693ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
7045ca1b86SEd Tanous         {
7145ca1b86SEd Tanous             return;
7245ca1b86SEd Tanous         }
7345ca1b86SEd Tanous 
748d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
75081ebf06SWludzik, Jozef             "#MetricReportCollection.MetricReportCollection";
76ae9031f0SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
77ae9031f0SWilly Tu             "/redfish/v1/TelemetryService/MetricReports";
788d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
797a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces{
807a1dbc48SGeorge Liu             telemetry::reportInterface};
814028ff77SEd Tanous         collection_util::getCollectionMembers(
82ae9031f0SWilly Tu             asyncResp,
83ae9031f0SWilly Tu             boost::urls::url("/redfish/v1/TelemetryService/MetricReports"),
84ae9031f0SWilly Tu             interfaces,
854028ff77SEd Tanous             "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
867e860f15SJohn Edward Broadbent         });
87081ebf06SWludzik, Jozef }
88081ebf06SWludzik, Jozef 
897e860f15SJohn Edward Broadbent inline void requestRoutesMetricReport(App& app)
90081ebf06SWludzik, Jozef {
917e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/<str>/")
92ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReport)
937e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
9445ca1b86SEd Tanous             [&app](const crow::Request& req,
957e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
967e860f15SJohn Edward Broadbent                    const std::string& id) {
973ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
9845ca1b86SEd Tanous         {
9945ca1b86SEd Tanous             return;
10045ca1b86SEd Tanous         }
101081ebf06SWludzik, Jozef         const std::string reportPath = telemetry::getDbusReportPath(id);
102081ebf06SWludzik, Jozef         crow::connections::systemBus->async_method_call(
103002d39b4SEd Tanous             [asyncResp, id, reportPath](const boost::system::error_code& ec) {
104081ebf06SWludzik, Jozef             if (ec.value() == EBADR ||
105081ebf06SWludzik, Jozef                 ec == boost::system::errc::host_unreachable)
106081ebf06SWludzik, Jozef             {
107002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "MetricReport", id);
108081ebf06SWludzik, Jozef                 return;
109081ebf06SWludzik, Jozef             }
110081ebf06SWludzik, Jozef             if (ec)
111081ebf06SWludzik, Jozef             {
112081ebf06SWludzik, Jozef                 BMCWEB_LOG_ERROR << "respHandler DBus error " << ec;
113081ebf06SWludzik, Jozef                 messages::internalError(asyncResp->res);
114081ebf06SWludzik, Jozef                 return;
115081ebf06SWludzik, Jozef             }
116081ebf06SWludzik, Jozef 
117002d39b4SEd Tanous             sdbusplus::asio::getProperty<telemetry::TimestampReadings>(
118002d39b4SEd Tanous                 *crow::connections::systemBus, telemetry::service, reportPath,
119002d39b4SEd Tanous                 telemetry::reportInterface, "Readings",
1208a592810SEd Tanous                 [asyncResp, id](const boost::system::error_code ec2,
1211e1e598dSJonathan Doman                                 const telemetry::TimestampReadings& ret) {
1228a592810SEd Tanous                 if (ec2)
123081ebf06SWludzik, Jozef                 {
1248a592810SEd Tanous                     BMCWEB_LOG_ERROR << "respHandler DBus error " << ec2;
125081ebf06SWludzik, Jozef                     messages::internalError(asyncResp->res);
126081ebf06SWludzik, Jozef                     return;
127081ebf06SWludzik, Jozef                 }
128081ebf06SWludzik, Jozef 
129002d39b4SEd Tanous                 telemetry::fillReport(asyncResp->res.jsonValue, id, ret);
1301e1e598dSJonathan Doman                 });
131081ebf06SWludzik, Jozef             },
132081ebf06SWludzik, Jozef             telemetry::service, reportPath, telemetry::reportInterface,
133081ebf06SWludzik, Jozef             "Update");
1347e860f15SJohn Edward Broadbent         });
135081ebf06SWludzik, Jozef }
136081ebf06SWludzik, Jozef } // namespace redfish
137