-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI_data_collector_IBM.py
658 lines (441 loc) · 23.5 KB
/
API_data_collector_IBM.py
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
import os, shutil
import json
import time
import requests
import glob
import json
import xmltodict
import copy
from pprint import pprint
from tqdm import tqdm
from datetime import datetime
from datetime import timedelta
import sys
# Reload from disk.
## if True it will hit the API, if false it will relaod from the disk.
hit_api = {'accounts': False,
'applications': False,
'hits': True,
'stories_volumes': True,
'autocompletes_volumes': True,
'coverages_volumes': True,
'related_stories_volumes': True,
'time_series_volumes': True,
'trends_volumes': True,
'histograms_volumes': True,
'clusters_volumes': True,
'total_stories_volumes': True,
'features': True,
'limits': True}
# delta = 16
# search_period_type ="delta"
# # search_period_type ="date"
# date_since= '2022-11-05 00:00:00'
# date_until= '2022-11-20 23:59:59'
class collector:
def __init__(self, access_token, api_base_url='', api_url_path='', max_per_page=1, page=1):
self.access_token = access_token
self.api_base_url = api_base_url
self.api_url_path = api_url_path
self.max_per_page = max_per_page
self.page = page
def call_accounts_endpoint(self):
total_pages = 0
params = {'access_token': self.access_token,
'page': self.page,
'per_page': self.max_per_page}
url = self.api_base_url + self.api_url_path
listed_itens =[]
def interact_itens(itens_response):
for item in itens_response:
temp_item = copy.deepcopy(item)
listed_itens.append(temp_item)
while params['page'] <= total_pages or params['page'] == 1:
response_raw = requests.get(url=url, params=params)
response = response_raw.content
response = response.decode('utf8')
response = dict(xmltodict.parse(response))
response = json.loads(json.dumps(response))
if response_raw.status_code == 200:
# First if will capture the total of pages to loop through
if int(response['accounts']['@current_page']) == 1:
# Captures the the total of pages at the first interaction.
total_pages = int(response['accounts']['@total_pages'])
# Call the function that loop through each iten
interact_itens(response['accounts']['account'])
# From second page ownards the number of pages is already know and will be incremental
elif int(response['accounts']['@current_page']) > 1:
# Call the function that loop through each item
interact_itens(response['accounts']['account'])
pprint('Retrieving page %s from a total of %s page(s)' %(str(params['page']), str(total_pages)))
params['page'] += 1
return listed_itens
def call_applications_endpoint(self):
total_pages = 0
params = {'access_token': self.access_token,
'page': self.page,
'per_page': self.max_per_page}
url = self.api_base_url + self.api_url_path
listed_itens =[]
def interact_itens(itens_response):
for item in itens_response:
temp_item = copy.deepcopy(item)
listed_itens.append(temp_item)
while params['page'] <= total_pages or params['page'] == 1:
response_raw = requests.get(url=url, params=params)
response = response_raw.content
response = response.decode('utf8')
response = dict(xmltodict.parse(response))
response = json.loads(json.dumps(response))
if response_raw.status_code == 200:
# First if will capture the total of pages to loop through
if int(response['applications']['@current_page']) == 1:
# Captures the the total of pages at the first interaction.
total_pages = int(response['applications']['@total_pages'])
# Call the function that loop through each item
interact_itens(response['applications']['application'])
# From second page ownards the number of pages is already know and will be incremental
elif int(response['applications']['@current_page']) > 1:
# Call the function that loop through each item
interact_itens(response['applications']['application'])
pprint('Retrieving page %s from a total of %s page(s)' %(str(params['page']), str(total_pages)))
params['page'] += 1
return listed_itens
class analytics:
def __init__(self, formats="json", access_token='', api_base_url='', api_url_path='', application_id='', metric_name='', since='', until='', granuraty='', timezone='', skip_change="true"):
self.formats = formats
self.access_token = access_token
self.api_base_url = api_base_url
self.api_url_path = api_url_path
self.application_id = application_id
self.metric_name = metric_name
self.until = until
self.since = since
self.granuraty = granuraty
self.timezone = timezone
self.skip_change = skip_change
def call_analytics_endpoint(self):
path =self.api_url_path.replace('app_id', self.application_id)
url = self.api_base_url + path
params = {'access_token': self.access_token,
'metric_name': self.metric_name,
'since': self.since,
'until': self.until,
'granularity': self.granuraty,
'timezone': self.timezone,
'skip_change': self.skip_change}
response_raw = requests.get(url=url, params=params)
if response_raw.status_code == 200:
response = response_raw.json()
return response
class features:
def __init__(self, api_base_url="", api_url_path="", access_token='', application_plan_id=""):
self.api_base_url=api_base_url,
self.api_url_path= api_url_path,
self.access_token = access_token
self.application_plan_id = application_plan_id
def call_features_per_app(self):
path =self.api_url_path[0].replace('application_plan_id', self.application_plan_id)
url = self.api_base_url[0] + path
params = {'access_token': self.access_token}
response_raw = requests.get(url=url, params=params)
if response_raw.status_code == 200:
response = response_raw.content
response = response.decode('utf8')
response = dict(xmltodict.parse(response))
response = json.loads(json.dumps(response))
response['application_plan_id'] = self.application_plan_id
return response
def call_limits_per_app(self):
path =self.api_url_path[0].replace('application_plan_id', self.application_plan_id)
url = self.api_base_url[0] + path
params = {'access_token': self.access_token,
'page': 1,
'per_page': 50}
response_raw = requests.get(url=url, params=params)
if response_raw.status_code == 200:
response = response_raw.content
response = response.decode('utf8')
response = dict(xmltodict.parse(response))
response = json.loads(json.dumps(response))
response['application_plan_id'] = self.application_plan_id
return response
class file_operations:
def __init__(self, folder, file_name, date, extension, content):
self.folder = folder
self.file_name = file_name
self.date = str(datetime.now().strftime('%Y%m%d_%H%M%S'))
self.extension = extension
self.content = content
def save_to_json(self):
dir_path = os.getcwd()
pathanme = dir_path + '/' + self.folder + '/' + self.file_name + '_' + self.date + self.extension
with open(pathanme, 'w') as fp:
json.dump(self.content, fp)
fp.close()
def load_json(self):
dir_path = os.getcwd()
file = dir_path + '/' + self.folder + '/' + '*' + self.file_name + '*'
files = glob.glob(file)
f = open(files[0])
data = json.load(f)
f.close()
return data
def check_file_exists(self):
dir_path = os.getcwd()
file = dir_path + '/' + self.folder + '/' + '*' + self.file_name + '*'
files = glob.glob(file)
if len(files) > 0:
return True
else:
return False
def move_files(self):
dir_path = os.getcwd()
file = dir_path + '/' + self.folder + '/' + '*' + self.file_name + '*'
files = glob.glob(file)
for file in files:
file_name = file.split("/")[-1]
src = dir_path + '/' + self.folder + '/' + file_name
dst = dir_path + '/' + self.folder + '/' + 'archive/' + file_name
shutil.move(src,dst)
def delete_files(self):
dir_path = os.getcwd()
file = dir_path + '/' + self.folder + '/' + '*' + self.file_name + '*'
files = glob.glob(file)
for file in files:
file_name = file.split("/")[-1]
file_path_name = dir_path + '/' + self.folder + '/' + file_name
os.remove(file_path_name)
class date_operations:
def get_since_date(today, number_days):
since = today - timedelta(days=number_days)
since = since.replace(second=0, microsecond=0, minute=0, hour=0)
since = since.strftime('%Y-%m-%d %H:%M:%S')
return since
def get_until_date(today, number_days=1):
until = today - timedelta(days=number_days)
until = until.replace(second=59, microsecond=59, minute=59, hour=23)
until = until.strftime('%Y-%m-%d %H:%M:%S')
return until
def get_delta(date):
date = datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ')
today = datetime.today()
delta = today - date
return delta.days
def main(search_period_type_input="delta", delta_input=16, date_since_input='2099-11-01 00:00:00', date_until_input='2099-11-15 23:59:59'):
# Sample command line
# 'delta', 16
# 'date', '2022-11-05 00:00:00', '2022-11-20 23:59:59'
if search_period_type_input == "delta":
search_period_type ="delta"
delta = delta_input
elif search_period_type_input == "date":
search_period_type ="date"
date_since = date_since_input
date_until = date_until_input
delta = 0
if search_period_type == 'delta':
# Date pattern returned '2022-11-01 00:00:00'
date_since= date_operations.get_since_date(datetime.now(), delta)
# Date pattern returned '2022-11-15 23:59:59'
date_until= date_operations.get_until_date(datetime.now(), 1)
elif search_period_type == 'date':
# [] Build a function to validate the date foramt
date_since= date_since_input
date_until= date_until_input
pprint(date_since)
pprint(date_until)
# Pull data from accounts:
accounts_file = file_operations('data_retrieved', 'accounts', '', '', '')
# check = accounts_file.check_file_exists()
if hit_api.get('accounts') is True:
print("#################################")
print("#### Retrieving all accounts ####")
print("#################################")
accounts_file = file_operations('data_retrieved', 'accounts', '', '', '')
accounts_file.move_files()
accounts_endpoint = collector('217a5119fec38a58befee4681edaf321653088a4a3b8638b26cb3e429885bfda',
'https://aylien-news-api-admin.3scale.net',
'/admin/api/accounts.xml',
500)
accounts = accounts_endpoint.call_accounts_endpoint()
if accounts is not None:
accounts_file = file_operations('data_retrieved', 'accounts', '', '.json', accounts )
accounts_file.save_to_json()
# Pull data from applications:
applications_file = file_operations('data_retrieved', 'applications', '', '', '')
# check = applications_file.check_file_exists()
if hit_api.get('applications') is True:
print("#####################################")
print("#### Retrieving all applications ####")
print("#####################################")
applications_file = file_operations('data_retrieved', 'applications', '', '', '')
applications_file.move_files()
applications_endpoint = collector('217a5119fec38a58befee4681edaf321653088a4a3b8638b26cb3e429885bfda',
'https://aylien-news-api-admin.3scale.net',
'/admin/api/applications.xml',
500)
applications = applications_endpoint.call_applications_endpoint()
if applications is not None:
applications_file = file_operations('data_retrieved', 'applications', '', '.json', applications )
applications_file.save_to_json()
# Pull data from invoices:
# [ ] For future
# Pull analytics:
anlytic_endpoints=[]
for i in hit_api:
if i not in ['accounts', 'applications', 'features']:
anlytic_endpoints.append(hit_api.get(i))
if any(anlytic_endpoints):
print("##############################################")
print("#### Retrieving all anallytics per APP ID ####")
print("##############################################")
print()
print()
applications_file = file_operations('data_retrieved', 'applications', '', '.json', '' )
applications = applications_file.load_json()
### For IBM ww will retrieve a specific set of APP IDs, no need to load from file.
## List of IBM applications copied from 3Scale
IBM_applications = ['433a6774',
'930f4c51',
'60eaa1cb',
'1681e6c4',
'9fc0e0dd']
## Loop through apps and leave behind only the ones from IBM
apps_temp =[]
for app in applications:
if str(app['application_id']) in str(IBM_applications):
apps_temp.append(app)
applications = apps_temp
pprint("Total apps to be retrieved:")
pprint(len(applications))
endpoint_analytics_metrics = ['hits',
'stories_volumes',
'autocompletes_volumes',
'coverages_volumes',
'related_stories_volumes',
'time_series_volumes',
'trends_volumes',
'histograms_volumes',
'clusters_volumes',
'total_stories_volumes']
for metric in endpoint_analytics_metrics:
if hit_api.get(metric) is True:
message = "Retrive metrics for endpoint %s." %(metric)
print("#"*len(message))
print(message)
print("#"*len(message))
analytics_file = file_operations('data_retrieved', metric, '', '', '')
analytics_file.move_files()
total_anl = []
for app in tqdm(applications):
modified_delta = date_operations.get_delta(app['updated_at'])
if app['state'] != 'suspended' or (app['state'] == 'suspended' and modified_delta <= delta +1):
analytics_file = analytics(formats='json',
access_token= '217a5119fec38a58befee4681edaf321653088a4a3b8638b26cb3e429885bfda',
api_base_url='https://aylien-news-api-admin.3scale.net',
api_url_path= '/stats/applications/app_id/usage.json',
application_id = app['id'],
metric_name= metric,
since= date_since,
until= date_until,
granuraty='day',
timezone='london',
skip_change='true'
)
data_anl = analytics_file.call_analytics_endpoint()
if data_anl is not None:
total_anl.append(data_anl)
metrics_file = file_operations('data_retrieved', metric, '', '.json', total_anl )
metrics_file.save_to_json()
#####
# Retrieve Feature for each application
if hit_api.get('features') is True:
print("############################################")
print("#### Retrieving all features per APP ID ####")
print("############################################")
features_file = file_operations('data_retrieved', 'feature_flags', '', '', '')
features_file.move_files()
applications_file = file_operations('data_retrieved', 'applications', '', '.json', '' )
applications = applications_file.load_json()
### For IBM ww will retrieve a specific set of APP IDs, no need to load from file.
## List of IBM applications copied from 3Scale
IBM_applications = ['433a6774',
'930f4c51',
'60eaa1cb',
'1681e6c4',
'9fc0e0dd']
## Loop through apps and leave behind only the ones from IBM
apps_temp =[]
for app in applications:
if str(app['application_id']) in str(IBM_applications):
apps_temp.append(app)
applications = apps_temp
pprint("Total apps to be retrieved:")
pprint(len(applications))
application_plan_ids = []
for app in applications:
application_plan_ids.append( {'application_plan_id':app['plan']['id'], 'updated_at':app['updated_at'], 'state':app['state'] })
#Unique app pan ids
application_plan_ids= list({v['application_plan_id']:v for v in application_plan_ids}.values())
total_features = []
for app in tqdm(application_plan_ids):
modified_delta = date_operations.get_delta(app['updated_at'])
# if app['state'] != 'suspended' or (app['state'] == 'suspended' and modified_delta <= delta+1):
feature_file = features(access_token= '217a5119fec38a58befee4681edaf321653088a4a3b8638b26cb3e429885bfda',
api_base_url='https://aylien-news-api-admin.3scale.net',
api_url_path= "/admin/api/application_plans/application_plan_id/features.xml",
application_plan_id=app['application_plan_id'] )
data_features = feature_file.call_features_per_app()
if data_features is not None:
total_features.append(data_features)
features_file = file_operations('data_retrieved', 'feature_flags', '', '.json', total_features )
features_file.save_to_json()
###############################################################################################################
# Retrieve Limits for each application
if hit_api.get('limits') is True:
print("##########################################")
print("#### Retrieving all Limits per APP ID ####")
print("##########################################")
# Find and move to archive and previous file that mathc the name
features_file = file_operations('data_retrieved', 'limits', '', '', '')
features_file.move_files()
# Load the file with all the applications
applications_file = file_operations('data_retrieved', 'applications', '', '.json', '' )
applications = applications_file.load_json()
### For IBM ww will retrieve a specific set of APP IDs, no need to load from file.
## List of IBM applications copied from 3Scale
IBM_applications = ['433a6774',
'930f4c51',
'60eaa1cb',
'1681e6c4',
'9fc0e0dd']
## Loop through apps and leave behind only the ones from IBM
apps_temp =[]
for app in applications:
if str(app['application_id']) in str(IBM_applications):
apps_temp.append(app)
applications = apps_temp
pprint("Total apps to be retrieved:")
pprint(len(applications))
# List all applications and relevant metadata
application_plan_ids = []
for app in applications:
application_plan_ids.append( {'application_plan_id':app['plan']['id'], 'updated_at':app['updated_at'], 'state':app['state'] })
#Unique app pan ids
application_plan_ids= list({v['application_plan_id']:v for v in application_plan_ids}.values())
total_limits = []
for app in tqdm(application_plan_ids):
modified_delta = date_operations.get_delta(app['updated_at'])
# if app['state'] != 'suspended' or (app['state'] == 'suspended' and modified_delta <= delta+120):
feature_file = features(access_token= '217a5119fec38a58befee4681edaf321653088a4a3b8638b26cb3e429885bfda',
api_base_url='https://aylien-news-api-admin.3scale.net',
api_url_path= "/admin/api/application_plans/application_plan_id/limits.xml",
application_plan_id=app['application_plan_id'] )
data_limits = feature_file.call_features_per_app()
if data_limits is not None:
total_limits.append(data_limits)
limits_file = file_operations('data_retrieved', 'limits', '', '.json', total_limits )
limits_file.save_to_json()
if __name__ == "__main__":
main()