xref: /openbmc/bmcweb/features/redfish/lib/metric_report.hpp (revision 479e899d5f57a67647f83b7f615d2c8565290bcf)
1081ebf06SWludzik, Jozef #pragma once
2081ebf06SWludzik, Jozef 
33ccb3adbSEd Tanous #include "app.hpp"
43ccb3adbSEd Tanous #include "dbus_utility.hpp"
53ccb3adbSEd Tanous #include "query.hpp"
63ccb3adbSEd 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 
11ef4c65b7SEd Tanous #include <boost/url/format.hpp>
121e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
137e860f15SJohn Edward Broadbent 
147a1dbc48SGeorge Liu #include <array>
157a1dbc48SGeorge Liu #include <string_view>
167a1dbc48SGeorge Liu 
17081ebf06SWludzik, Jozef namespace redfish
18081ebf06SWludzik, Jozef {
19081ebf06SWludzik, Jozef 
20081ebf06SWludzik, Jozef namespace telemetry
21081ebf06SWludzik, Jozef {
22081ebf06SWludzik, Jozef 
23*479e899dSKrzysztof Grobelny using Readings = std::vector<std::tuple<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 
30*479e899dSKrzysztof Grobelny     for (const auto& [metadata, sensorValue, timestamp] : readings)
31081ebf06SWludzik, Jozef     {
32613dabeaSEd Tanous         nlohmann::json::object_t metricReport;
33613dabeaSEd Tanous         metricReport["MetricProperty"] = metadata;
34613dabeaSEd Tanous         metricReport["MetricValue"] = std::to_string(sensorValue);
35613dabeaSEd Tanous         metricReport["Timestamp"] =
36613dabeaSEd Tanous             redfish::time_utils::getDateTimeUintMs(timestamp);
37b2ba3072SPatrick Williams         metricValues.emplace_back(std::move(metricReport));
38081ebf06SWludzik, Jozef     }
39081ebf06SWludzik, Jozef 
40081ebf06SWludzik, Jozef     return metricValues;
41081ebf06SWludzik, Jozef }
42081ebf06SWludzik, Jozef 
43c0353249SWludzik, Jozef inline bool fillReport(nlohmann::json& json, const std::string& id,
441e1e598dSJonathan Doman                        const TimestampReadings& timestampReadings)
45081ebf06SWludzik, Jozef {
46c0353249SWludzik, Jozef     json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport";
47ef4c65b7SEd Tanous     json["@odata.id"] = boost::urls::format(
48ef4c65b7SEd Tanous         "/redfish/v1/TelemetryService/MetricReports/{}", id);
49c0353249SWludzik, Jozef     json["Id"] = id;
50c0353249SWludzik, Jozef     json["Name"] = id;
51ef4c65b7SEd Tanous     json["MetricReportDefinition"]["@odata.id"] = boost::urls::format(
52ef4c65b7SEd Tanous         "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", id);
53081ebf06SWludzik, Jozef 
541e1e598dSJonathan Doman     const auto& [timestamp, readings] = timestampReadings;
552b82937eSEd Tanous     json["Timestamp"] = redfish::time_utils::getDateTimeUintMs(timestamp);
56c0353249SWludzik, Jozef     json["MetricValues"] = toMetricValues(readings);
57c0353249SWludzik, Jozef     return true;
58081ebf06SWludzik, Jozef }
59081ebf06SWludzik, Jozef } // namespace telemetry
60081ebf06SWludzik, Jozef 
617e860f15SJohn Edward Broadbent inline void requestRoutesMetricReportCollection(App& app)
62081ebf06SWludzik, Jozef {
637e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/")
64ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReportCollection)
657e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
6645ca1b86SEd Tanous             [&app](const crow::Request& req,
677e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
683ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
6945ca1b86SEd Tanous         {
7045ca1b86SEd Tanous             return;
7145ca1b86SEd Tanous         }
7245ca1b86SEd Tanous 
738d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
74081ebf06SWludzik, Jozef             "#MetricReportCollection.MetricReportCollection";
75ae9031f0SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
76ae9031f0SWilly Tu             "/redfish/v1/TelemetryService/MetricReports";
778d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
787a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces{
797a1dbc48SGeorge Liu             telemetry::reportInterface};
804028ff77SEd Tanous         collection_util::getCollectionMembers(
81ae9031f0SWilly Tu             asyncResp,
82ae9031f0SWilly Tu             boost::urls::url("/redfish/v1/TelemetryService/MetricReports"),
83ae9031f0SWilly Tu             interfaces,
844028ff77SEd Tanous             "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
857e860f15SJohn Edward Broadbent         });
86081ebf06SWludzik, Jozef }
87081ebf06SWludzik, Jozef 
887e860f15SJohn Edward Broadbent inline void requestRoutesMetricReport(App& app)
89081ebf06SWludzik, Jozef {
907e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/<str>/")
91ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReport)
927e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
9345ca1b86SEd Tanous             [&app](const crow::Request& req,
947e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
957e860f15SJohn Edward Broadbent                    const std::string& id) {
963ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
9745ca1b86SEd Tanous         {
9845ca1b86SEd Tanous             return;
9945ca1b86SEd Tanous         }
100081ebf06SWludzik, Jozef         const std::string reportPath = telemetry::getDbusReportPath(id);
101081ebf06SWludzik, Jozef         crow::connections::systemBus->async_method_call(
102002d39b4SEd Tanous             [asyncResp, id, reportPath](const boost::system::error_code& ec) {
103081ebf06SWludzik, Jozef             if (ec.value() == EBADR ||
104081ebf06SWludzik, Jozef                 ec == boost::system::errc::host_unreachable)
105081ebf06SWludzik, Jozef             {
106002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "MetricReport", id);
107081ebf06SWludzik, Jozef                 return;
108081ebf06SWludzik, Jozef             }
109081ebf06SWludzik, Jozef             if (ec)
110081ebf06SWludzik, Jozef             {
111081ebf06SWludzik, Jozef                 BMCWEB_LOG_ERROR << "respHandler DBus error " << ec;
112081ebf06SWludzik, Jozef                 messages::internalError(asyncResp->res);
113081ebf06SWludzik, Jozef                 return;
114081ebf06SWludzik, Jozef             }
115081ebf06SWludzik, Jozef 
116002d39b4SEd Tanous             sdbusplus::asio::getProperty<telemetry::TimestampReadings>(
117002d39b4SEd Tanous                 *crow::connections::systemBus, telemetry::service, reportPath,
118002d39b4SEd Tanous                 telemetry::reportInterface, "Readings",
1195e7e2dc5SEd Tanous                 [asyncResp, id](const boost::system::error_code& ec2,
1201e1e598dSJonathan Doman                                 const telemetry::TimestampReadings& ret) {
1218a592810SEd Tanous                 if (ec2)
122081ebf06SWludzik, Jozef                 {
1238a592810SEd Tanous                     BMCWEB_LOG_ERROR << "respHandler DBus error " << ec2;
124081ebf06SWludzik, Jozef                     messages::internalError(asyncResp->res);
125081ebf06SWludzik, Jozef                     return;
126081ebf06SWludzik, Jozef                 }
127081ebf06SWludzik, Jozef 
128002d39b4SEd Tanous                 telemetry::fillReport(asyncResp->res.jsonValue, id, ret);
1291e1e598dSJonathan Doman                 });
130081ebf06SWludzik, Jozef             },
131081ebf06SWludzik, Jozef             telemetry::service, reportPath, telemetry::reportInterface,
132081ebf06SWludzik, Jozef             "Update");
1337e860f15SJohn Edward Broadbent         });
134081ebf06SWludzik, Jozef }
135081ebf06SWludzik, Jozef } // namespace redfish
136