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