xref: /openbmc/bmcweb/redfish-core/lib/metric_report.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3081ebf06SWludzik, Jozef #pragma once
4081ebf06SWludzik, Jozef 
53ccb3adbSEd Tanous #include "app.hpp"
6*d7857201SEd Tanous #include "async_resp.hpp"
7*d7857201SEd Tanous #include "dbus_singleton.hpp"
8*d7857201SEd Tanous #include "error_messages.hpp"
9*d7857201SEd Tanous #include "http_request.hpp"
10*d7857201SEd Tanous #include "logging.hpp"
113ccb3adbSEd Tanous #include "query.hpp"
123ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
134028ff77SEd Tanous #include "utils/collection.hpp"
14081ebf06SWludzik, Jozef #include "utils/telemetry_utils.hpp"
152b82937eSEd Tanous #include "utils/time_utils.hpp"
16081ebf06SWludzik, Jozef 
17*d7857201SEd Tanous #include <asm-generic/errno.h>
18*d7857201SEd Tanous 
19*d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
20ef4c65b7SEd Tanous #include <boost/url/format.hpp>
21*d7857201SEd Tanous #include <boost/url/url.hpp>
22*d7857201SEd Tanous #include <nlohmann/json.hpp>
231e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
247e860f15SJohn Edward Broadbent 
257a1dbc48SGeorge Liu #include <array>
26*d7857201SEd Tanous #include <cstdint>
27*d7857201SEd Tanous #include <memory>
28*d7857201SEd Tanous #include <string>
297a1dbc48SGeorge Liu #include <string_view>
30*d7857201SEd Tanous #include <tuple>
31*d7857201SEd Tanous #include <utility>
32*d7857201SEd Tanous #include <vector>
337a1dbc48SGeorge Liu 
34081ebf06SWludzik, Jozef namespace redfish
35081ebf06SWludzik, Jozef {
36081ebf06SWludzik, Jozef 
37081ebf06SWludzik, Jozef namespace telemetry
38081ebf06SWludzik, Jozef {
39081ebf06SWludzik, Jozef 
40479e899dSKrzysztof Grobelny using Readings = std::vector<std::tuple<std::string, double, uint64_t>>;
41081ebf06SWludzik, Jozef using TimestampReadings = std::tuple<uint64_t, Readings>;
42081ebf06SWludzik, Jozef 
toMetricValues(const Readings & readings)43081ebf06SWludzik, Jozef inline nlohmann::json toMetricValues(const Readings& readings)
44081ebf06SWludzik, Jozef {
45081ebf06SWludzik, Jozef     nlohmann::json metricValues = nlohmann::json::array_t();
46081ebf06SWludzik, Jozef 
47479e899dSKrzysztof Grobelny     for (const auto& [metadata, sensorValue, timestamp] : readings)
48081ebf06SWludzik, Jozef     {
49613dabeaSEd Tanous         nlohmann::json::object_t metricReport;
50613dabeaSEd Tanous         metricReport["MetricProperty"] = metadata;
51613dabeaSEd Tanous         metricReport["MetricValue"] = std::to_string(sensorValue);
52613dabeaSEd Tanous         metricReport["Timestamp"] =
53613dabeaSEd Tanous             redfish::time_utils::getDateTimeUintMs(timestamp);
54b2ba3072SPatrick Williams         metricValues.emplace_back(std::move(metricReport));
55081ebf06SWludzik, Jozef     }
56081ebf06SWludzik, Jozef 
57081ebf06SWludzik, Jozef     return metricValues;
58081ebf06SWludzik, Jozef }
59081ebf06SWludzik, Jozef 
fillReport(nlohmann::json & json,const std::string & id,const TimestampReadings & timestampReadings)60c0353249SWludzik, Jozef inline bool fillReport(nlohmann::json& json, const std::string& id,
611e1e598dSJonathan Doman                        const TimestampReadings& timestampReadings)
62081ebf06SWludzik, Jozef {
63c0353249SWludzik, Jozef     json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport";
64ef4c65b7SEd Tanous     json["@odata.id"] = boost::urls::format(
65ef4c65b7SEd Tanous         "/redfish/v1/TelemetryService/MetricReports/{}", id);
66c0353249SWludzik, Jozef     json["Id"] = id;
67c0353249SWludzik, Jozef     json["Name"] = id;
68ef4c65b7SEd Tanous     json["MetricReportDefinition"]["@odata.id"] = boost::urls::format(
69ef4c65b7SEd Tanous         "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", id);
70081ebf06SWludzik, Jozef 
711e1e598dSJonathan Doman     const auto& [timestamp, readings] = timestampReadings;
722b82937eSEd Tanous     json["Timestamp"] = redfish::time_utils::getDateTimeUintMs(timestamp);
73c0353249SWludzik, Jozef     json["MetricValues"] = toMetricValues(readings);
74c0353249SWludzik, Jozef     return true;
75081ebf06SWludzik, Jozef }
76081ebf06SWludzik, Jozef } // namespace telemetry
77081ebf06SWludzik, Jozef 
requestRoutesMetricReportCollection(App & app)787e860f15SJohn Edward Broadbent inline void requestRoutesMetricReportCollection(App& app)
79081ebf06SWludzik, Jozef {
807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/")
81ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReportCollection)
827e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
8345ca1b86SEd Tanous             [&app](const crow::Request& req,
847e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
853ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
8645ca1b86SEd Tanous                 {
8745ca1b86SEd Tanous                     return;
8845ca1b86SEd Tanous                 }
8945ca1b86SEd Tanous 
908d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
91081ebf06SWludzik, Jozef                     "#MetricReportCollection.MetricReportCollection";
92ae9031f0SWilly Tu                 asyncResp->res.jsonValue["@odata.id"] =
93ae9031f0SWilly Tu                     "/redfish/v1/TelemetryService/MetricReports";
948d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
957a1dbc48SGeorge Liu                 constexpr std::array<std::string_view, 1> interfaces{
967a1dbc48SGeorge Liu                     telemetry::reportInterface};
974028ff77SEd Tanous                 collection_util::getCollectionMembers(
98ae9031f0SWilly Tu                     asyncResp,
99bd79bce8SPatrick Williams                     boost::urls::url(
100bd79bce8SPatrick Williams                         "/redfish/v1/TelemetryService/MetricReports"),
101ae9031f0SWilly Tu                     interfaces,
1024028ff77SEd Tanous                     "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
1037e860f15SJohn Edward Broadbent             });
104081ebf06SWludzik, Jozef }
105081ebf06SWludzik, Jozef 
requestRoutesMetricReport(App & app)1067e860f15SJohn Edward Broadbent inline void requestRoutesMetricReport(App& app)
107081ebf06SWludzik, Jozef {
1087e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/<str>/")
109ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReport)
1107e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
11145ca1b86SEd Tanous             [&app](const crow::Request& req,
1127e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1137e860f15SJohn Edward Broadbent                    const std::string& id) {
1143ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
11545ca1b86SEd Tanous                 {
11645ca1b86SEd Tanous                     return;
11745ca1b86SEd Tanous                 }
118081ebf06SWludzik, Jozef                 const std::string reportPath = telemetry::getDbusReportPath(id);
119081ebf06SWludzik, Jozef                 crow::connections::systemBus->async_method_call(
120bd79bce8SPatrick Williams                     [asyncResp, id,
121bd79bce8SPatrick Williams                      reportPath](const boost::system::error_code& ec) {
122081ebf06SWludzik, Jozef                         if (ec.value() == EBADR ||
123081ebf06SWludzik, Jozef                             ec == boost::system::errc::host_unreachable)
124081ebf06SWludzik, Jozef                         {
125bd79bce8SPatrick Williams                             messages::resourceNotFound(asyncResp->res,
126bd79bce8SPatrick Williams                                                        "MetricReport", id);
127081ebf06SWludzik, Jozef                             return;
128081ebf06SWludzik, Jozef                         }
129081ebf06SWludzik, Jozef                         if (ec)
130081ebf06SWludzik, Jozef                         {
13162598e31SEd Tanous                             BMCWEB_LOG_ERROR("respHandler DBus error {}", ec);
132081ebf06SWludzik, Jozef                             messages::internalError(asyncResp->res);
133081ebf06SWludzik, Jozef                             return;
134081ebf06SWludzik, Jozef                         }
135081ebf06SWludzik, Jozef 
136bd79bce8SPatrick Williams                         sdbusplus::asio::getProperty<
137bd79bce8SPatrick Williams                             telemetry::TimestampReadings>(
138bd79bce8SPatrick Williams                             *crow::connections::systemBus, telemetry::service,
139bd79bce8SPatrick Williams                             reportPath, telemetry::reportInterface, "Readings",
140bd79bce8SPatrick Williams                             [asyncResp,
141bd79bce8SPatrick Williams                              id](const boost::system::error_code& ec2,
1421e1e598dSJonathan Doman                                  const telemetry::TimestampReadings& ret) {
1438a592810SEd Tanous                                 if (ec2)
144081ebf06SWludzik, Jozef                                 {
145bd79bce8SPatrick Williams                                     BMCWEB_LOG_ERROR(
146bd79bce8SPatrick Williams                                         "respHandler DBus error {}", ec2);
147081ebf06SWludzik, Jozef                                     messages::internalError(asyncResp->res);
148081ebf06SWludzik, Jozef                                     return;
149081ebf06SWludzik, Jozef                                 }
150081ebf06SWludzik, Jozef 
151bd79bce8SPatrick Williams                                 telemetry::fillReport(asyncResp->res.jsonValue,
152bd79bce8SPatrick Williams                                                       id, ret);
1531e1e598dSJonathan Doman                             });
154081ebf06SWludzik, Jozef                     },
155081ebf06SWludzik, Jozef                     telemetry::service, reportPath, telemetry::reportInterface,
156081ebf06SWludzik, Jozef                     "Update");
1577e860f15SJohn Edward Broadbent             });
158081ebf06SWludzik, Jozef }
159081ebf06SWludzik, Jozef } // namespace redfish
160