1 #pragma once
2 
3 #include "utils/collection.hpp"
4 #include "utils/telemetry_utils.hpp"
5 #include "utils/time_utils.hpp"
6 
7 #include <app.hpp>
8 #include <dbus_utility.hpp>
9 #include <query.hpp>
10 #include <registries/privilege_registry.hpp>
11 #include <sdbusplus/asio/property.hpp>
12 
13 namespace redfish
14 {
15 
16 namespace telemetry
17 {
18 
19 using Readings =
20     std::vector<std::tuple<std::string, std::string, double, uint64_t>>;
21 using TimestampReadings = std::tuple<uint64_t, Readings>;
22 
23 inline nlohmann::json toMetricValues(const Readings& readings)
24 {
25     nlohmann::json metricValues = nlohmann::json::array_t();
26 
27     for (const auto& [id, metadata, sensorValue, timestamp] : readings)
28     {
29         nlohmann::json::object_t metricReport;
30         metricReport["MetricId"] = id;
31         metricReport["MetricProperty"] = metadata;
32         metricReport["MetricValue"] = std::to_string(sensorValue);
33         metricReport["Timestamp"] =
34             redfish::time_utils::getDateTimeUintMs(timestamp);
35         metricValues.push_back(std::move(metricReport));
36     }
37 
38     return metricValues;
39 }
40 
41 inline bool fillReport(nlohmann::json& json, const std::string& id,
42                        const TimestampReadings& timestampReadings)
43 {
44     json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport";
45     json["@odata.id"] =
46         crow::utility::urlFromPieces("redfish", "v1", "TelemetryService",
47                                      "MetricReports", id)
48             .string();
49     json["Id"] = id;
50     json["Name"] = id;
51     json["MetricReportDefinition"]["@odata.id"] =
52         crow::utility::urlFromPieces("redfish", "v1", "TelemetryService",
53                                      "MetricReportDefinitions", id)
54             .string();
55 
56     const auto& [timestamp, readings] = timestampReadings;
57     json["Timestamp"] = redfish::time_utils::getDateTimeUintMs(timestamp);
58     json["MetricValues"] = toMetricValues(readings);
59     return true;
60 }
61 } // namespace telemetry
62 
63 inline void requestRoutesMetricReportCollection(App& app)
64 {
65     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/")
66         .privileges(redfish::privileges::getMetricReportCollection)
67         .methods(boost::beast::http::verb::get)(
68             [&app](const crow::Request& req,
69                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
70         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
71         {
72             return;
73         }
74 
75         asyncResp->res.jsonValue["@odata.type"] =
76             "#MetricReportCollection.MetricReportCollection";
77         asyncResp->res.jsonValue["@odata.id"] =
78             "/redfish/v1/TelemetryService/MetricReports";
79         asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
80         const std::vector<const char*> interfaces{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