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