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