xref: /openbmc/bmcweb/features/redfish/lib/metric_report.hpp (revision 613dabea03cd2f5cb999be37aaf539280c63ea7a)
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 
19456cd875SSzymon Dompke constexpr const char* metricReportUri =
20456cd875SSzymon Dompke     "/redfish/v1/TelemetryService/MetricReports";
21456cd875SSzymon Dompke 
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     {
32*613dabeaSEd Tanous         nlohmann::json::object_t metricReport;
33*613dabeaSEd Tanous         metricReport["MetricId"] = id;
34*613dabeaSEd Tanous         metricReport["MetricProperty"] = metadata;
35*613dabeaSEd Tanous         metricReport["MetricValue"] = std::to_string(sensorValue);
36*613dabeaSEd Tanous         metricReport["Timestamp"] =
37*613dabeaSEd Tanous             redfish::time_utils::getDateTimeUintMs(timestamp);
38*613dabeaSEd 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";
48456cd875SSzymon Dompke     json["@odata.id"] =
49456cd875SSzymon Dompke         crow::utility::urlFromPieces("redfish", "v1", "TelemetryService",
50456cd875SSzymon Dompke                                      "MetricReports", id)
51456cd875SSzymon Dompke             .string();
52c0353249SWludzik, Jozef     json["Id"] = id;
53c0353249SWludzik, Jozef     json["Name"] = id;
54c0353249SWludzik, Jozef     json["MetricReportDefinition"]["@odata.id"] =
55456cd875SSzymon Dompke         crow::utility::urlFromPieces("redfish", "v1", "TelemetryService",
56456cd875SSzymon Dompke                                      "MetricReportDefinitions", id)
57456cd875SSzymon Dompke             .string();
58081ebf06SWludzik, Jozef 
591e1e598dSJonathan Doman     const auto& [timestamp, readings] = timestampReadings;
602b82937eSEd Tanous     json["Timestamp"] = redfish::time_utils::getDateTimeUintMs(timestamp);
61c0353249SWludzik, Jozef     json["MetricValues"] = toMetricValues(readings);
62c0353249SWludzik, Jozef     return true;
63081ebf06SWludzik, Jozef }
64081ebf06SWludzik, Jozef } // namespace telemetry
65081ebf06SWludzik, Jozef 
667e860f15SJohn Edward Broadbent inline void requestRoutesMetricReportCollection(App& app)
67081ebf06SWludzik, Jozef {
687e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/")
69ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReportCollection)
707e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
7145ca1b86SEd Tanous             [&app](const crow::Request& req,
727e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
733ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
7445ca1b86SEd Tanous         {
7545ca1b86SEd Tanous             return;
7645ca1b86SEd Tanous         }
7745ca1b86SEd Tanous 
788d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
79081ebf06SWludzik, Jozef             "#MetricReportCollection.MetricReportCollection";
80002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = telemetry::metricReportUri;
818d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
82002d39b4SEd Tanous         const std::vector<const char*> interfaces{telemetry::reportInterface};
834028ff77SEd Tanous         collection_util::getCollectionMembers(
844028ff77SEd Tanous             asyncResp, telemetry::metricReportUri, 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