summaryrefslogtreecommitdiff
path: root/meta-ibs/meta-cp2-5422/recipes-phosphor/interfaces/bmcweb/0012-add-telemetry-hour-data.patch
blob: f835ac2ca5680e8e7529b2d0bb026d3c92f66061 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
From 8115dec92eb59b60e48295d3d94e739e138c22b3 Mon Sep 17 00:00:00 2001
From: eportnov <eportnov@ibs.ru>
Date: Thu, 8 Sep 2022 15:22:48 +0300
Subject: [PATCH] add telemetry hour data

---
 redfish-core/lib/metric_report.hpp | 148 +++++++++++++++++++++++------
 1 file changed, 119 insertions(+), 29 deletions(-)

diff --git a/redfish-core/lib/metric_report.hpp b/redfish-core/lib/metric_report.hpp
index b3f5721e..64808a9c 100644
--- a/redfish-core/lib/metric_report.hpp
+++ b/redfish-core/lib/metric_report.hpp
@@ -16,6 +16,20 @@ namespace redfish
 namespace telemetry
 {
 
+enum class Params
+{
+    ReportName,
+    IdGroup,
+    Period,
+};
+
+enum class Periods
+{
+    All,
+    LastHour,
+    None,
+};
+
 constexpr const char* metricReportUri =
     "/redfish/v1/TelemetryService/MetricReports";
 
@@ -87,6 +101,81 @@ inline bool fillReport(nlohmann::json& json, const std::string& id,
     json["MetricValues"] = toMetricValues(readings);
     return true;
 }
+
+inline telemetry::Periods ConvertPeriod(std::string const& period_as_string)
+{
+    if( period_as_string == "all" )
+    {
+        return telemetry::Periods::All;
+    }
+    else if( period_as_string == "last_hour" )
+    {
+        return telemetry::Periods::LastHour;
+    }
+    return telemetry::Periods::None;
+}
+
+inline std::pair<Params, std::string> BuildPairParams(std::vector<std::string> const& params_as_string)
+{
+    static constexpr int SIZE_OF_PAIR = 2;
+
+    std::pair<telemetry::Params, std::string> result;
+
+    if(params_as_string.empty() || params_as_string.size() > SIZE_OF_PAIR )
+    {
+        return result;
+    }
+    if(params_as_string[0] == "id")
+    {
+        result.first = telemetry::Params::IdGroup;
+        result.second = params_as_string[1];
+    }
+    else if(params_as_string[0] == "period")
+    {
+        result.first = telemetry::Params::Period;
+        result.second = params_as_string[1];
+    }
+    else if( params_as_string[0] == "report" )
+    {
+        result.first = telemetry::Params::ReportName;
+        result.second = params_as_string[1];
+    }
+    return result;
+}
+
+inline std::vector<std::string> ParseLine( std::string const& line, std::string const& delimiter )
+{
+    std::vector<std::string> result;
+
+    auto start = 0U;
+    auto end = line.find(delimiter);
+    while(end != std::string::npos)
+    {
+        auto parsed = line.substr(start, end - start);
+        result.push_back(parsed);
+        start = end + delimiter.length();
+        end = line.find(delimiter, start);
+    }
+    auto parsed = line.substr(start, end);
+    result.push_back(parsed);
+    return result;
+}
+
+inline std::unordered_map<telemetry::Params, std::string> BuildParams(std::vector<std::string> const& parsed_data)
+{
+    static const std::string DELIMITER_ON_PARAMETER = "=";
+
+    std::unordered_map<telemetry::Params, std::string> result;
+    for(const auto& not_parsed_param : parsed_data)
+    {
+        auto param_pair = ParseLine( not_parsed_param, DELIMITER_ON_PARAMETER );
+        auto pair_params = BuildPairParams( param_pair );
+        result.insert( pair_params );
+    }
+    return result;
+}
+
+
 } // namespace telemetry
 
 inline void requestRoutesMetricReportCollection(App& app)
@@ -119,44 +208,45 @@ inline void requestRoutesMetricReport(App& app)
         .methods(boost::beast::http::verb::get)(
             [&app](const crow::Request& req,
                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
-                   const std::string& id) {
+                   const std::string& params) {
         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
         {
             return;
         }
-        const std::string reportPath = telemetry::getDbusReportPath(id);
-        crow::connections::systemBus->async_method_call(
-            [asyncResp, id, reportPath](const boost::system::error_code& ec) {
-            if (ec.value() == EBADR ||
-                ec == boost::system::errc::host_unreachable)
-            {
-                messages::resourceNotFound(asyncResp->res, "MetricReport", id);
-                return;
-            }
-            if (ec)
-            {
-                BMCWEB_LOG_ERROR << "respHandler DBus error " << ec;
-                messages::internalError(asyncResp->res);
-                return;
-            }
-
-            sdbusplus::asio::getProperty<telemetry::TimestampReadings>(
-                *crow::connections::systemBus, telemetry::service, reportPath,
-                telemetry::reportInterface, "Readings",
-                [asyncResp, id](const boost::system::error_code ec2,
-                                const telemetry::TimestampReadings& ret) {
-                if (ec2)
+        static const std::string DELIMITER_ON_LINE = "&";
+
+        auto full_params = "report=" + params;
+        auto parsed_params = telemetry::BuildParams( telemetry::ParseLine( full_params, DELIMITER_ON_LINE ) );
+
+        auto report_name_it = parsed_params.find(telemetry::Params::ReportName);
+        auto report_name = report_name_it != parsed_params.end() ? report_name_it->second : "";
+
+        auto period_it = parsed_params.find(telemetry::Params::Period);
+        auto period = period_it != parsed_params.end() ? period_it->second : "";
+
+        const std::string reportPath = telemetry::getDbusReportPath(report_name);
+
+        auto is_need_last_hour = telemetry::ConvertPeriod(period) == telemetry::Periods::LastHour;
+
+        crow::connections::systemBus->async_method_call([asyncResp, report_name, period](const boost::system::error_code& error_code,
+                                                           const telemetry::TimestampReadings& ret){
+                if(error_code.value() == EBADR ||
+                    error_code == boost::system::errc::host_unreachable)
                 {
-                    BMCWEB_LOG_ERROR << "respHandler DBus error " << ec2;
-                    messages::internalError(asyncResp->res);
+                    std::cout << "first error " << std::endl;
+                    std::cout << "host_unreachable" << std::endl;
                     return;
                 }
+                if(error_code)
+                 {
+                    std::cout << "respHandler DBus error " << error_code << std::endl;
+                    return;
+                 }
 
-                telemetry::fillReport(asyncResp->res.jsonValue, id, ret);
-                });
+                telemetry::fillReport(asyncResp->res.jsonValue,report_name, ret);
             },
             telemetry::service, reportPath, telemetry::reportInterface,
-            "Update");
-        });
+            "GetReadings", is_need_last_hour);
+         });
 }
 } // namespace redfish