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