xref: /openbmc/bmcweb/features/redfish/lib/metric_report.hpp (revision ef4c65b741724d724452a3a0efe8dff0d450514a)
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 
11*ef4c65b7SEd 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 
23081ebf06SWludzik, Jozef using Readings =
24081ebf06SWludzik, Jozef     std::vector<std::tuple<std::string, std::string, double, uint64_t>>;
25081ebf06SWludzik, Jozef using TimestampReadings = std::tuple<uint64_t, Readings>;
26081ebf06SWludzik, Jozef 
27081ebf06SWludzik, Jozef inline nlohmann::json toMetricValues(const Readings& readings)
28081ebf06SWludzik, Jozef {
29081ebf06SWludzik, Jozef     nlohmann::json metricValues = nlohmann::json::array_t();
30081ebf06SWludzik, Jozef 
319eb808c1SEd Tanous     for (const auto& [id, metadata, sensorValue, timestamp] : readings)
32081ebf06SWludzik, Jozef     {
33613dabeaSEd Tanous         nlohmann::json::object_t metricReport;
34613dabeaSEd Tanous         metricReport["MetricId"] = id;
35613dabeaSEd Tanous         metricReport["MetricProperty"] = metadata;
36613dabeaSEd Tanous         metricReport["MetricValue"] = std::to_string(sensorValue);
37613dabeaSEd Tanous         metricReport["Timestamp"] =
38613dabeaSEd Tanous             redfish::time_utils::getDateTimeUintMs(timestamp);
39b2ba3072SPatrick Williams         metricValues.emplace_back(std::move(metricReport));
40081ebf06SWludzik, Jozef     }
41081ebf06SWludzik, Jozef 
42081ebf06SWludzik, Jozef     return metricValues;
43081ebf06SWludzik, Jozef }
44081ebf06SWludzik, Jozef 
45c0353249SWludzik, Jozef inline bool fillReport(nlohmann::json& json, const std::string& id,
461e1e598dSJonathan Doman                        const TimestampReadings& timestampReadings)
47081ebf06SWludzik, Jozef {
48c0353249SWludzik, Jozef     json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport";
49*ef4c65b7SEd Tanous     json["@odata.id"] = boost::urls::format(
50*ef4c65b7SEd Tanous         "/redfish/v1/TelemetryService/MetricReports/{}", id);
51c0353249SWludzik, Jozef     json["Id"] = id;
52c0353249SWludzik, Jozef     json["Name"] = id;
53*ef4c65b7SEd Tanous     json["MetricReportDefinition"]["@odata.id"] = boost::urls::format(
54*ef4c65b7SEd Tanous         "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", id);
55081ebf06SWludzik, Jozef 
561e1e598dSJonathan Doman     const auto& [timestamp, readings] = timestampReadings;
572b82937eSEd Tanous     json["Timestamp"] = redfish::time_utils::getDateTimeUintMs(timestamp);
58c0353249SWludzik, Jozef     json["MetricValues"] = toMetricValues(readings);
59c0353249SWludzik, Jozef     return true;
60081ebf06SWludzik, Jozef }
61081ebf06SWludzik, Jozef } // namespace telemetry
62081ebf06SWludzik, Jozef 
637e860f15SJohn Edward Broadbent inline void requestRoutesMetricReportCollection(App& app)
64081ebf06SWludzik, Jozef {
657e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/")
66ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReportCollection)
677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
6845ca1b86SEd Tanous             [&app](const crow::Request& req,
697e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
703ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
7145ca1b86SEd Tanous         {
7245ca1b86SEd Tanous             return;
7345ca1b86SEd Tanous         }
7445ca1b86SEd Tanous 
758d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
76081ebf06SWludzik, Jozef             "#MetricReportCollection.MetricReportCollection";
77ae9031f0SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
78ae9031f0SWilly Tu             "/redfish/v1/TelemetryService/MetricReports";
798d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
807a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces{
817a1dbc48SGeorge Liu             telemetry::reportInterface};
824028ff77SEd Tanous         collection_util::getCollectionMembers(
83ae9031f0SWilly Tu             asyncResp,
84ae9031f0SWilly Tu             boost::urls::url("/redfish/v1/TelemetryService/MetricReports"),
85ae9031f0SWilly Tu             interfaces,
864028ff77SEd Tanous             "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
877e860f15SJohn Edward Broadbent         });
88081ebf06SWludzik, Jozef }
89081ebf06SWludzik, Jozef 
907e860f15SJohn Edward Broadbent inline void requestRoutesMetricReport(App& app)
91081ebf06SWludzik, Jozef {
927e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/<str>/")
93ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReport)
947e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
9545ca1b86SEd Tanous             [&app](const crow::Request& req,
967e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
977e860f15SJohn Edward Broadbent                    const std::string& id) {
983ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
9945ca1b86SEd Tanous         {
10045ca1b86SEd Tanous             return;
10145ca1b86SEd Tanous         }
102081ebf06SWludzik, Jozef         const std::string reportPath = telemetry::getDbusReportPath(id);
103081ebf06SWludzik, Jozef         crow::connections::systemBus->async_method_call(
104002d39b4SEd Tanous             [asyncResp, id, reportPath](const boost::system::error_code& ec) {
105081ebf06SWludzik, Jozef             if (ec.value() == EBADR ||
106081ebf06SWludzik, Jozef                 ec == boost::system::errc::host_unreachable)
107081ebf06SWludzik, Jozef             {
108002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "MetricReport", id);
109081ebf06SWludzik, Jozef                 return;
110081ebf06SWludzik, Jozef             }
111081ebf06SWludzik, Jozef             if (ec)
112081ebf06SWludzik, Jozef             {
113081ebf06SWludzik, Jozef                 BMCWEB_LOG_ERROR << "respHandler DBus error " << ec;
114081ebf06SWludzik, Jozef                 messages::internalError(asyncResp->res);
115081ebf06SWludzik, Jozef                 return;
116081ebf06SWludzik, Jozef             }
117081ebf06SWludzik, Jozef 
118002d39b4SEd Tanous             sdbusplus::asio::getProperty<telemetry::TimestampReadings>(
119002d39b4SEd Tanous                 *crow::connections::systemBus, telemetry::service, reportPath,
120002d39b4SEd Tanous                 telemetry::reportInterface, "Readings",
1215e7e2dc5SEd Tanous                 [asyncResp, id](const boost::system::error_code& ec2,
1221e1e598dSJonathan Doman                                 const telemetry::TimestampReadings& ret) {
1238a592810SEd Tanous                 if (ec2)
124081ebf06SWludzik, Jozef                 {
1258a592810SEd Tanous                     BMCWEB_LOG_ERROR << "respHandler DBus error " << ec2;
126081ebf06SWludzik, Jozef                     messages::internalError(asyncResp->res);
127081ebf06SWludzik, Jozef                     return;
128081ebf06SWludzik, Jozef                 }
129081ebf06SWludzik, Jozef 
130002d39b4SEd Tanous                 telemetry::fillReport(asyncResp->res.jsonValue, id, ret);
1311e1e598dSJonathan Doman                 });
132081ebf06SWludzik, Jozef             },
133081ebf06SWludzik, Jozef             telemetry::service, reportPath, telemetry::reportInterface,
134081ebf06SWludzik, Jozef             "Update");
1357e860f15SJohn Edward Broadbent         });
136081ebf06SWludzik, Jozef }
137081ebf06SWludzik, Jozef } // namespace redfish
138