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