1081ebf06SWludzik, Jozef #pragma once 2081ebf06SWludzik, Jozef 34028ff77SEd Tanous #include "utils/collection.hpp" 4081ebf06SWludzik, Jozef #include "utils/telemetry_utils.hpp" 52b82937eSEd Tanous #include "utils/time_utils.hpp" 6081ebf06SWludzik, Jozef 77e860f15SJohn Edward Broadbent #include <app.hpp> 8168e20c1SEd Tanous #include <dbus_utility.hpp> 945ca1b86SEd Tanous #include <query.hpp> 10ed398213SEd Tanous #include <registries/privilege_registry.hpp> 111e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 127e860f15SJohn Edward Broadbent 13081ebf06SWludzik, Jozef namespace redfish 14081ebf06SWludzik, Jozef { 15081ebf06SWludzik, Jozef 16081ebf06SWludzik, Jozef namespace telemetry 17081ebf06SWludzik, Jozef { 18081ebf06SWludzik, Jozef 19081ebf06SWludzik, Jozef using Readings = 20081ebf06SWludzik, Jozef std::vector<std::tuple<std::string, std::string, double, uint64_t>>; 21081ebf06SWludzik, Jozef using TimestampReadings = std::tuple<uint64_t, Readings>; 22081ebf06SWludzik, Jozef 23081ebf06SWludzik, Jozef inline nlohmann::json toMetricValues(const Readings& readings) 24081ebf06SWludzik, Jozef { 25081ebf06SWludzik, Jozef nlohmann::json metricValues = nlohmann::json::array_t(); 26081ebf06SWludzik, Jozef 279eb808c1SEd Tanous for (const auto& [id, metadata, sensorValue, timestamp] : readings) 28081ebf06SWludzik, Jozef { 29613dabeaSEd Tanous nlohmann::json::object_t metricReport; 30613dabeaSEd Tanous metricReport["MetricId"] = id; 31613dabeaSEd Tanous metricReport["MetricProperty"] = metadata; 32613dabeaSEd Tanous metricReport["MetricValue"] = std::to_string(sensorValue); 33613dabeaSEd Tanous metricReport["Timestamp"] = 34613dabeaSEd Tanous redfish::time_utils::getDateTimeUintMs(timestamp); 35613dabeaSEd Tanous metricValues.push_back(std::move(metricReport)); 36081ebf06SWludzik, Jozef } 37081ebf06SWludzik, Jozef 38081ebf06SWludzik, Jozef return metricValues; 39081ebf06SWludzik, Jozef } 40081ebf06SWludzik, Jozef 41c0353249SWludzik, Jozef inline bool fillReport(nlohmann::json& json, const std::string& id, 421e1e598dSJonathan Doman const TimestampReadings& timestampReadings) 43081ebf06SWludzik, Jozef { 44c0353249SWludzik, Jozef json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport"; 45456cd875SSzymon Dompke json["@odata.id"] = 46456cd875SSzymon Dompke crow::utility::urlFromPieces("redfish", "v1", "TelemetryService", 47456cd875SSzymon Dompke "MetricReports", id) 48456cd875SSzymon Dompke .string(); 49c0353249SWludzik, Jozef json["Id"] = id; 50c0353249SWludzik, Jozef json["Name"] = id; 51c0353249SWludzik, Jozef json["MetricReportDefinition"]["@odata.id"] = 52456cd875SSzymon Dompke crow::utility::urlFromPieces("redfish", "v1", "TelemetryService", 53456cd875SSzymon Dompke "MetricReportDefinitions", id) 54456cd875SSzymon Dompke .string(); 55081ebf06SWludzik, Jozef 561e1e598dSJonathan Doman const auto& [timestamp, readings] = timestampReadings; 572b82937eSEd Tanous json["Timestamp"] = redfish::time_utils::getDateTimeUintMs(timestamp); 58c0353249SWludzik, Jozef json["MetricValues"] = toMetricValues(readings); 59c0353249SWludzik, Jozef return true; 60081ebf06SWludzik, Jozef } 61081ebf06SWludzik, Jozef } // namespace telemetry 62081ebf06SWludzik, Jozef 637e860f15SJohn Edward Broadbent inline void requestRoutesMetricReportCollection(App& app) 64081ebf06SWludzik, Jozef { 657e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/") 66ed398213SEd Tanous .privileges(redfish::privileges::getMetricReportCollection) 677e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 6845ca1b86SEd Tanous [&app](const crow::Request& req, 697e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 703ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 7145ca1b86SEd Tanous { 7245ca1b86SEd Tanous return; 7345ca1b86SEd Tanous } 7445ca1b86SEd Tanous 758d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 76081ebf06SWludzik, Jozef "#MetricReportCollection.MetricReportCollection"; 77*ae9031f0SWilly Tu asyncResp->res.jsonValue["@odata.id"] = 78*ae9031f0SWilly Tu "/redfish/v1/TelemetryService/MetricReports"; 798d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Metric Report Collection"; 80002d39b4SEd Tanous const std::vector<const char*> interfaces{telemetry::reportInterface}; 814028ff77SEd Tanous collection_util::getCollectionMembers( 82*ae9031f0SWilly Tu asyncResp, 83*ae9031f0SWilly Tu boost::urls::url("/redfish/v1/TelemetryService/MetricReports"), 84*ae9031f0SWilly Tu interfaces, 854028ff77SEd Tanous "/xyz/openbmc_project/Telemetry/Reports/TelemetryService"); 867e860f15SJohn Edward Broadbent }); 87081ebf06SWludzik, Jozef } 88081ebf06SWludzik, Jozef 897e860f15SJohn Edward Broadbent inline void requestRoutesMetricReport(App& app) 90081ebf06SWludzik, Jozef { 917e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/<str>/") 92ed398213SEd Tanous .privileges(redfish::privileges::getMetricReport) 937e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 9445ca1b86SEd Tanous [&app](const crow::Request& req, 957e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 967e860f15SJohn Edward Broadbent const std::string& id) { 973ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 9845ca1b86SEd Tanous { 9945ca1b86SEd Tanous return; 10045ca1b86SEd Tanous } 101081ebf06SWludzik, Jozef const std::string reportPath = telemetry::getDbusReportPath(id); 102081ebf06SWludzik, Jozef crow::connections::systemBus->async_method_call( 103002d39b4SEd Tanous [asyncResp, id, reportPath](const boost::system::error_code& ec) { 104081ebf06SWludzik, Jozef if (ec.value() == EBADR || 105081ebf06SWludzik, Jozef ec == boost::system::errc::host_unreachable) 106081ebf06SWludzik, Jozef { 107002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "MetricReport", id); 108081ebf06SWludzik, Jozef return; 109081ebf06SWludzik, Jozef } 110081ebf06SWludzik, Jozef if (ec) 111081ebf06SWludzik, Jozef { 112081ebf06SWludzik, Jozef BMCWEB_LOG_ERROR << "respHandler DBus error " << ec; 113081ebf06SWludzik, Jozef messages::internalError(asyncResp->res); 114081ebf06SWludzik, Jozef return; 115081ebf06SWludzik, Jozef } 116081ebf06SWludzik, Jozef 117002d39b4SEd Tanous sdbusplus::asio::getProperty<telemetry::TimestampReadings>( 118002d39b4SEd Tanous *crow::connections::systemBus, telemetry::service, reportPath, 119002d39b4SEd Tanous telemetry::reportInterface, "Readings", 1208a592810SEd Tanous [asyncResp, id](const boost::system::error_code ec2, 1211e1e598dSJonathan Doman const telemetry::TimestampReadings& ret) { 1228a592810SEd Tanous if (ec2) 123081ebf06SWludzik, Jozef { 1248a592810SEd Tanous BMCWEB_LOG_ERROR << "respHandler DBus error " << ec2; 125081ebf06SWludzik, Jozef messages::internalError(asyncResp->res); 126081ebf06SWludzik, Jozef return; 127081ebf06SWludzik, Jozef } 128081ebf06SWludzik, Jozef 129002d39b4SEd Tanous telemetry::fillReport(asyncResp->res.jsonValue, id, ret); 1301e1e598dSJonathan Doman }); 131081ebf06SWludzik, Jozef }, 132081ebf06SWludzik, Jozef telemetry::service, reportPath, telemetry::reportInterface, 133081ebf06SWludzik, Jozef "Update"); 1347e860f15SJohn Edward Broadbent }); 135081ebf06SWludzik, Jozef } 136081ebf06SWludzik, Jozef } // namespace redfish 137