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->res)) 72 { 73 return; 74 } 75 76 asyncResp->res.jsonValue["@odata.type"] = 77 "#MetricReportCollection.MetricReportCollection"; 78 asyncResp->res.jsonValue["@odata.id"] = 79 telemetry::metricReportUri; 80 asyncResp->res.jsonValue["Name"] = "Metric Report Collection"; 81 const std::vector<const char*> interfaces{ 82 telemetry::reportInterface}; 83 collection_util::getCollectionMembers( 84 asyncResp, telemetry::metricReportUri, 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->res)) 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 ec, 125 const telemetry::TimestampReadings& ret) { 126 if (ec) 127 { 128 BMCWEB_LOG_ERROR 129 << "respHandler DBus error " << ec; 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