1 #pragma once
2 
3 #include "app.hpp"
4 #include "dbus_utility.hpp"
5 #include "query.hpp"
6 #include "registries/privilege_registry.hpp"
7 #include "utils/collection.hpp"
8 #include "utils/telemetry_utils.hpp"
9 #include "utils/time_utils.hpp"
10 
11 #include <sdbusplus/asio/property.hpp>
12 
13 #include <array>
14 #include <string_view>
15 
16 namespace redfish
17 {
18 
19 namespace telemetry
20 {
21 
22 using Readings =
23     std::vector<std::tuple<std::string, std::string, double, uint64_t>>;
24 using TimestampReadings = std::tuple<uint64_t, Readings>;
25 
26 inline nlohmann::json toMetricValues(const Readings& readings)
27 {
28     nlohmann::json metricValues = nlohmann::json::array_t();
29 
30     for (const auto& [id, metadata, sensorValue, timestamp] : readings)
31     {
32         nlohmann::json::object_t metricReport;
33         metricReport["MetricId"] = id;
34         metricReport["MetricProperty"] = metadata;
35         metricReport["MetricValue"] = std::to_string(sensorValue);
36         metricReport["Timestamp"] =
37             redfish::time_utils::getDateTimeUintMs(timestamp);
38         metricValues.push_back(std::move(metricReport));
39     }
40 
41     return metricValues;
42 }
43 
44 inline bool fillReport(nlohmann::json& json, const std::string& id,
45                        const TimestampReadings& timestampReadings)
46 {
47     json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport";
48     json["@odata.id"] = crow::utility::urlFromPieces(
49         "redfish", "v1", "TelemetryService", "MetricReports", id);
50     json["Id"] = id;
51     json["Name"] = id;
52     json["MetricReportDefinition"]["@odata.id"] = crow::utility::urlFromPieces(
53         "redfish", "v1", "TelemetryService", "MetricReportDefinitions", id);
54 
55     const auto& [timestamp, readings] = timestampReadings;
56     json["Timestamp"] = redfish::time_utils::getDateTimeUintMs(timestamp);
57     json["MetricValues"] = toMetricValues(readings);
58     return true;
59 }
60 } // namespace telemetry
61 
62 inline void requestRoutesMetricReportCollection(App& app)
63 {
64     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/")
65         .privileges(redfish::privileges::getMetricReportCollection)
66         .methods(boost::beast::http::verb::get)(
67             [&app](const crow::Request& req,
68                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
69         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
70         {
71             return;
72         }
73 
74         asyncResp->res.jsonValue["@odata.type"] =
75             "#MetricReportCollection.MetricReportCollection";
76         asyncResp->res.jsonValue["@odata.id"] =
77             "/redfish/v1/TelemetryService/MetricReports";
78         asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
79         constexpr std::array<std::string_view, 1> interfaces{
80             telemetry::reportInterface};
81         collection_util::getCollectionMembers(
82             asyncResp,
83             boost::urls::url("/redfish/v1/TelemetryService/MetricReports"),
84             interfaces,
85             "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
86         });
87 }
88 
89 inline void requestRoutesMetricReport(App& app)
90 {
91     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/<str>/")
92         .privileges(redfish::privileges::getMetricReport)
93         .methods(boost::beast::http::verb::get)(
94             [&app](const crow::Request& req,
95                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
96                    const std::string& id) {
97         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
98         {
99             return;
100         }
101         const std::string reportPath = telemetry::getDbusReportPath(id);
102         crow::connections::systemBus->async_method_call(
103             [asyncResp, id, reportPath](const boost::system::error_code& ec) {
104             if (ec.value() == EBADR ||
105                 ec == boost::system::errc::host_unreachable)
106             {
107                 messages::resourceNotFound(asyncResp->res, "MetricReport", id);
108                 return;
109             }
110             if (ec)
111             {
112                 BMCWEB_LOG_ERROR << "respHandler DBus error " << ec;
113                 messages::internalError(asyncResp->res);
114                 return;
115             }
116 
117             sdbusplus::asio::getProperty<telemetry::TimestampReadings>(
118                 *crow::connections::systemBus, telemetry::service, reportPath,
119                 telemetry::reportInterface, "Readings",
120                 [asyncResp, id](const boost::system::error_code& ec2,
121                                 const telemetry::TimestampReadings& ret) {
122                 if (ec2)
123                 {
124                     BMCWEB_LOG_ERROR << "respHandler DBus error " << ec2;
125                     messages::internalError(asyncResp->res);
126                     return;
127                 }
128 
129                 telemetry::fillReport(asyncResp->res.jsonValue, id, ret);
130                 });
131             },
132             telemetry::service, reportPath, telemetry::reportInterface,
133             "Update");
134         });
135 }
136 } // namespace redfish
137