-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathma_session.py
More file actions
355 lines (305 loc) · 11.7 KB
/
ma_session.py
File metadata and controls
355 lines (305 loc) · 11.7 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from json.decoder import JSONDecodeError
from typing import List
import json
import requests
import numpy as np
import pandas as pd
from pandas import json_normalize
from requests.structures import CaseInsensitiveDict
from requests.sessions import Session
try:
import importlib.resources as pkg_resources
except ImportError:
# Try backported to PY<37 `importlib_resources`.
import importlib_resources as pkg_resources
from smsdk import config
RESOURCE_CONFIG = json.loads(pkg_resources.read_text(config, "message_config.json"))
SM_AUTH_HEADER_SECRET_ID = RESOURCE_CONFIG["auth_header-api-secret"]
SM_AUTH_HEADER_SECRET_ID_OLD = RESOURCE_CONFIG["auth_header-api-secret_old"]
SM_AUTH_HEADER_KEY_ID = RESOURCE_CONFIG["auth_header-api-key"]
def dict_to_df(data, normalize=True):
if normalize:
# special case to handle the 'stats' block
if data and 'stats' in data[0]:
if isinstance(data[0]['stats'], dict):
# part stats are dict
df = json_normalize(data)
else:
# machine type stats are list
cols = [*data[0]]
cols.remove('stats')
df = json_normalize(data, 'stats', cols, record_prefix='stats.', errors='ignore')
else:
try:
df = json_normalize(data)
except:
# From cases like _distinct which don't have a "normal" return format
return pd.DataFrame({'values': data})
else:
df = pd.DataFrame(data)
if len(df) > 0:
if '_id' in df.columns:
df.set_index('_id', inplace=True)
if 'id' in df.columns:
df.set_index('id', inplace=True)
return df
import logging
log = logging.getLogger(__name__)
class MaSession:
def __init__(self):
self.requests = requests
self.session = Session()
def _get_records(
self,
endpoint,
method="get",
_limit=np.Inf,
_offset=0,
**url_params
):
"""
Function to get api call and fetch data from MA APIs
:param endpoint: complete url endpoint
:param method: Reqested method. Default = get
:param enable_pagination: if pagination is enabled then
the records are fetched with limit offset pagination
:param limit: Limit the number of records for pagination
:param offset: pagination offset
:param url_params: dict of params for API ex filtering, columns etc
:return: List of records
"""
if 'machine_type' in url_params:
url_params.pop('machine_type')
max_page_size = 2000
records: List = []
while True:
try:
remaining_limit = _limit - len(records)
this_loop_limit = min(remaining_limit, max_page_size)
# If we exactly hit our desired number of records -- limit is 0 -- then can stop
if this_loop_limit <= 0:
return records
url_params["_offset"] = _offset
url_params["_limit"] = this_loop_limit
#print(f'Pulling up to {this_loop_limit} records ({remaining_limit} remain)')
response = getattr(self.session, method.lower())(
endpoint, params=url_params
)
# print(f"response text -- {response.text}")
if response.text:
if response.status_code not in [200, 201]:
raise ValueError("Error - {}".format(response.text))
try:
data = response.json()
if 'results' in data:
data = data['results']
except JSONDecodeError as e:
print(f'No valid JSON returned {e}')
return []
else:
return []
records.extend(data)
# print(f'sizes {len(data)} vs {this_loop_limit}')
if len(data) < this_loop_limit:
# Cursor exhausted, so just return
return records
_offset += this_loop_limit
except:
import traceback
log.error(traceback.print_exc())
return records
def _get_schema(
self,
endpoint,
method="get",
**url_params
):
"""
This function can be used to fetch HLO schemas like AIDP
Function to get api call and fetch data from MA APIs
:param endpoint: complete url endpoint
:param method: Reqested method. Default = get
:param url_params: dict of params for API ex filtering, columns etc
:return: List of records
"""
if 'machine_type' in url_params:
url_params.pop('machine_type')
response = getattr(self.session, method.lower())(
endpoint, params=url_params
)
if response.text:
if response.status_code not in [200, 201]:
raise ValueError("Error - {}".format(response.text))
try:
data = response.json()
if 'objects' in data:
data = data['objects']
return data
except JSONDecodeError as e:
print(f'No valid JSON returned {e}')
return []
else:
return []
def _get_records_v1(
self,
endpoint,
method="post",
limit=np.Inf,
offset=0,
db_mode='sql',
**url_params
):
"""
Function to get api call and fetch data from MA APIs
:param endpoint: complete url endpoint
:param method: Reqested method. Default = get
:param enable_pagination: if pagination is enabled then
the records are fetched with limit offset pagination
:param limit: Limit the number of records for pagination
:param offset: pagination offset
:param url_params: dict of params for API ex filtering, columns etc
:return: List of records
"""
max_page_size = 50000
records: List = []
while True:
try:
remaining_limit = limit - len(records)
this_loop_limit = min(remaining_limit, max_page_size)
# If we exactly hit our desired number of records -- limit is 0 -- then can stop
if this_loop_limit == 0:
return records
url_params["offset"] = offset
url_params["limit"] = this_loop_limit
url_params["db_mode"] = db_mode
# print(f'Pulling up to {this_loop_limit} records ({remaining_limit} remain)')
response = getattr(self.session, method.lower())(
endpoint, json=url_params
)
if response.text:
if response.status_code not in [200, 201]:
raise ValueError("Error - {}".format(response.text))
try:
data = response.json()
data = data['results']
if isinstance(data, dict):
data = [data]
except JSONDecodeError as e:
print(f'No valid JSON returned {e}')
return []
else:
return []
records.extend(data)
if len(data) < this_loop_limit:
# Cursor exhausted, so just return
return records
offset += this_loop_limit
except:
import traceback
log.error(traceback.print_exc())
return records
def get_json_headers(self):
"""
Headers for json requests
"""
return CaseInsensitiveDict(
{
"Accept-Encoding": "*/*",
"Content-Type": "application/json",
"accept": "application/json",
}
)
def get_starttime_endtime_keys(self, **kwargs):
"""
This function takes kwargs as input and tried to identify starttime and endtime key provided by user and returns
:param kwargs:
:return:
"""
starttime_key = ""
endtime_key = ""
times = {i: kwargs[i] for i in kwargs if 'time' in i.lower()}
if times:
starttime = min(times.values())
endtime = max(times.values())
for key in times:
if times[key] == starttime:
starttime_key = key
elif times[key] == endtime:
endtime_key = key
else:
continue
return starttime_key, endtime_key
def _get_records_mongo_v1(
self,
endpoint,
normalize=True,
method="get",
limit=np.Inf,
offset=1,
**url_params
):
"""
Function to get api call and fetch data from MA APIs
:param endpoint: complete url endpoint
:param method: Reqested method. Default = get
:param enable_pagination: if pagination is enabled then
the records are fetched with limit offset pagination
:param limit: Limit the number of records for pagination
:param offset: pagination offset
:param url_params: dict of params for API ex filtering, columns etc
:return: List of records
"""
next_page = ""
offset = int(offset)
try:
limit = int(limit)
except:
limit = float(limit)
if 'machine_type' in url_params:
url_params.pop('machine_type')
max_page_size = 2000
limit = min(max_page_size, limit)
if not url_params.get("per_page"):
url_params["per_page"] = 5
def _fetch_data(endpoint, url_params):
response = getattr(self.session, method.lower())(
endpoint, params=url_params
)
if response.text:
if response.status_code not in [200, 201]:
raise ValueError("Error - {}".format(response.text))
try:
data = response.json()
try:
next_page = data["next_page"]
except:
next_page = ""
if data["success"]:
data = data['objects']
except JSONDecodeError as e:
print(f'No valid JSON returned {e}')
data = []
else:
data = []
return data, next_page
while limit > 0:
if next_page:
data, next_page = _fetch_data(endpoint=next_page, url_params={})
if not next_page:
limit = 0
else:
limit -= len(data)
else:
data, next_page = _fetch_data(endpoint=endpoint, url_params=url_params)
if not next_page:
limit = 0
else:
limit -= len(data)
data = dict_to_df(data, normalize=normalize)
# To keep consistent, rename columns back from '.' to '__'
data.columns = [name.replace('.', '__') for name in data.columns]
if 'endtime' in data.columns:
data['endtime'] = pd.to_datetime(data['endtime'])
if 'starttime' in data.columns:
data['starttime'] = pd.to_datetime(data['starttime'])
yield data