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