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