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
toMetricValues(const Readings & readings)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
fillReport(nlohmann::json & json,const std::string & id,const TimestampReadings & timestampReadings)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
requestRoutesMetricReportCollection(App & app)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(
83 "/redfish/v1/TelemetryService/MetricReports"),
84 interfaces,
85 "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
86 });
87 }
88
requestRoutesMetricReport(App & app)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,
104 reportPath](const boost::system::error_code& ec) {
105 if (ec.value() == EBADR ||
106 ec == boost::system::errc::host_unreachable)
107 {
108 messages::resourceNotFound(asyncResp->res,
109 "MetricReport", id);
110 return;
111 }
112 if (ec)
113 {
114 BMCWEB_LOG_ERROR("respHandler DBus error {}", ec);
115 messages::internalError(asyncResp->res);
116 return;
117 }
118
119 sdbusplus::asio::getProperty<
120 telemetry::TimestampReadings>(
121 *crow::connections::systemBus, telemetry::service,
122 reportPath, telemetry::reportInterface, "Readings",
123 [asyncResp,
124 id](const boost::system::error_code& ec2,
125 const telemetry::TimestampReadings& ret) {
126 if (ec2)
127 {
128 BMCWEB_LOG_ERROR(
129 "respHandler DBus error {}", ec2);
130 messages::internalError(asyncResp->res);
131 return;
132 }
133
134 telemetry::fillReport(asyncResp->res.jsonValue,
135 id, ret);
136 });
137 },
138 telemetry::service, reportPath, telemetry::reportInterface,
139 "Update");
140 });
141 }
142 } // namespace redfish
143