Skip to content

Commit 963ecd1

Browse files
author
Xiaobin Zheng
committed
[cm_api] OPSAPS-29287 add new timeseries API for v11 to sync with java side changes
CM server side introduces queryTimeSeries(ApiTimeSeriesRequest request) in v11. This change adds query_timeseries_with_long_query_string to ApiResource to sync with server side changes. Testing done: End to end test: able to invoke new API in new python client against my cluster. Updated test_timeseries and following command passes: cm_api/python$ make clean test
1 parent cb4b88d commit 963ecd1

File tree

4 files changed

+71
-22
lines changed

4 files changed

+71
-22
lines changed

python/src/cm_api/api_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,15 @@ def get_metrics(self, path, from_time, to_time, metrics, view, params=None):
272272
resp = self.get(path, params=params)
273273
return types.ApiList.from_json_dict(resp, self, types.ApiMetric)
274274

275-
def query_timeseries(self, query, from_time=None, to_time=None):
275+
def query_timeseries(self, query, from_time=None, to_time=None, by_post=False):
276276
"""
277277
Query time series.
278278
@param query: Query string.
279279
@param from_time: Start of the period to query (optional).
280280
@param to_time: End of the period to query (default = now).
281281
@return: A list of ApiTimeSeriesResponse.
282282
"""
283-
return timeseries.query_timeseries(self, query, from_time, to_time)
283+
return timeseries.query_timeseries(self, query, from_time, to_time, by_post=by_post)
284284

285285
def get_metric_schema(self):
286286
"""

python/src/cm_api/endpoints/timeseries.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
METRIC_ENTITY_ATTR_PATH = "/timeseries/entityTypeAttributes"
2727

2828
def query_timeseries(resource_root, query, from_time=None, to_time=None,
29-
desired_rollup=None, must_use_desired_rollup=None):
29+
desired_rollup=None, must_use_desired_rollup=None, by_post=False):
3030
"""
3131
Query for time series data from the CM time series data store.
3232
@param query: Query string.
@@ -47,11 +47,22 @@ def query_timeseries(resource_root, query, from_time=None, to_time=None,
4747
must_use_desired_rollup is set to true.
4848
@param must_use_desired_rollup: Indicates that the monitoring server should
4949
return the data at the rollup desired.
50+
@param by_post: If true, an HTTP POST request will be made to server. This
51+
allows longer query string to be accepted compared to HTTP
52+
GET request.
5053
@return: List of ApiTimeSeriesResponse
5154
"""
55+
data = None
5256
params = {}
53-
if query:
57+
request_method = resource_root.get
58+
if by_post:
59+
request = ApiTimeSeriesRequest(resource_root,
60+
query=query)
61+
data = request
62+
request_method = resource_root.post
63+
elif query:
5464
params['query'] = query
65+
5566
if from_time:
5667
params['from'] = from_time.isoformat()
5768
if to_time:
@@ -60,8 +71,8 @@ def query_timeseries(resource_root, query, from_time=None, to_time=None,
6071
params['desiredRollup'] = desired_rollup
6172
if must_use_desired_rollup:
6273
params['mustUseDesiredRollup'] = must_use_desired_rollup
63-
return call(resource_root.get, TIME_SERIES_PATH,
64-
ApiTimeSeriesResponse, True, params=params)
74+
return call(request_method, TIME_SERIES_PATH,
75+
ApiTimeSeriesResponse, True, params=params, data=data)
6576

6677
def get_metric_schema(resource_root):
6778
"""

python/src/cm_api/endpoints/types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,19 @@ class ApiYarnApplicationAttribute(BaseApiObject):
11271127
def __str__(self):
11281128
return "<ApiYarnApplicationAttribute> %s" % name
11291129

1130+
class ApiTimeSeriesRequest(BaseApiObject):
1131+
_ATTRIBUTES = {
1132+
'query' : None,
1133+
'from' : None,
1134+
'to' : None,
1135+
'contentType' : None,
1136+
'desiredRollup' : None,
1137+
'mustUseDesiredRollup' : None
1138+
}
1139+
1140+
def __str__(self):
1141+
return "<ApiTimeSeriesRequest>: %s" % (self.query)
1142+
11301143
def config_to_api_list(dic):
11311144
"""
11321145
Converts a python dictionary into a list containing the proper

python/src/cm_api_tests/test_timeseries.py

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,53 @@ def test_query_timeseries(self):
106106
time = datetime.datetime.now()
107107
api_resource.expect("GET", "/timeseries",
108108
retdata=json.loads(TIME_SERIES),
109-
params={ 'from':time.isoformat(), 'to':time.isoformat(),
109+
params={ 'from':time.isoformat(),
110+
'to':time.isoformat(),
110111
'query':'select cpu_percent'})
111112
responses = query_timeseries(api_resource,
112113
"select cpu_percent",
113114
time,
114115
time)
116+
self._verify_timeseries_response(responses)
115117

118+
# Test the with-rollups call
119+
api_resource.expect("GET", "/timeseries",
120+
retdata=json.loads(TIME_SERIES),
121+
params={ 'from':time.isoformat(),
122+
'to':time.isoformat(),
123+
'query':'select cpu_percent',
124+
'desiredRollup':'RAW',
125+
'mustUseDesiredRollup': True})
126+
responses = query_timeseries(api_resource,
127+
"select cpu_percent",
128+
time,
129+
time,
130+
"RAW",
131+
True)
132+
self._verify_timeseries_response(responses)
133+
134+
# Test query_timeseries_with_long_query_string
135+
api_resource.expect("POST", "/timeseries",
136+
retdata=json.loads(TIME_SERIES),
137+
data={ 'query':'select cpu_percent'},
138+
params={ 'from':time.isoformat(),
139+
'to':time.isoformat()})
140+
responses = query_timeseries(
141+
api_resource, "select cpu_percent", time, time, by_post=True)
142+
self._verify_timeseries_response(responses)
143+
144+
api_resource.expect("POST", "/timeseries",
145+
retdata=json.loads(TIME_SERIES),
146+
data={ 'query':'select cpu_percent'},
147+
params={ 'from':time.isoformat(),
148+
'to':time.isoformat(),
149+
'desiredRollup':'RAW',
150+
'mustUseDesiredRollup': True})
151+
responses = query_timeseries(api_resource, "select cpu_percent",
152+
time, time, "RAW", True, by_post=True)
153+
self._verify_timeseries_response(responses)
154+
155+
def _verify_timeseries_response(self, responses):
116156
self.assertIsInstance(responses, ApiList)
117157
self.assertEqual(1, len(responses))
118158
response = responses[0]
@@ -154,21 +194,6 @@ def test_query_timeseries(self):
154194
xEntityMetadata = data.aggregateStatistics.crossEntityMetadata
155195
self.assertIsNone(xEntityMetadata)
156196

157-
# Test the with-rollups call
158-
api_resource.expect("GET", "/timeseries",
159-
retdata=json.loads(TIME_SERIES),
160-
params={ 'from':time.isoformat(), 'to':time.isoformat(),
161-
'query':'select cpu_percent',
162-
'desiredRollup':'RAW',
163-
'mustUseDesiredRollup': True})
164-
responses = query_timeseries(api_resource,
165-
"select cpu_percent",
166-
time,
167-
time,
168-
"RAW",
169-
True)
170-
171-
172197
def test_get_metric_schema(self):
173198
METRICS = '''{
174199
"items" : [ {

0 commit comments

Comments
 (0)