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