-
Notifications
You must be signed in to change notification settings - Fork 0
/
netx_create_collection.py
71 lines (53 loc) · 2.17 KB
/
netx_create_collection.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
'''Given a CSV list of assetIds, create a NetX Collection (group) via NetX API'''
import logging
import math
import time
import re
from datetime import datetime
from utils import netx_api as un
from utils import csv_tools as uc
from utils import setup
def main():
'''main function'''
setup.start_log_dams_netx(config=None)
live_or_test = setup.get_sys_argv(1)
config = setup.get_config_dams_netx(live_or_test)
# Point the .env 'GROUP_ADD_CSV' variable at the CSV file with the list of assets
input_csv = config['GROUP_ADD_CSV']
group_add_rows = uc.rows(input_csv)
asset_id_list = [int(row['assetId']) for row in group_add_rows if len(re.findall(r'\D', row['assetId'])) == 0]
# Break down list into chunks if > 3000 assets
# if len(asset_id_list) > 3000:
asset_list_list = []
chunk_size = 15000
start_chunk = 1
chunks = math.ceil(len(asset_id_list)/chunk_size)
for i in list(range(start_chunk,chunks)):
chunk_min = chunk_size*(i-1)
chunk_max = chunk_size*i
asset_list_list.append(asset_id_list[chunk_min:chunk_max])
i += 1
collection_time = re.sub(r'\s|\:', '-', str(datetime.now())[:16])
collection_title = f'API Collection {collection_time}'
# Create new groups
chunk_number = start_chunk
for asset_list_chunk in asset_list_list:
collection_data = un.netx_create_collection(
collection_title=f'{collection_title}_{chunk_number}',
asset_id_list=asset_list_chunk, # asset_id_list,
netx_env=live_or_test)
chunk_number += 1
if 'result' in collection_data:
col_id = collection_data['result']['id']
col_title = collection_data['result']['title']
col_item_count = collection_data['result']['itemCount']
log_message = f'{col_title} - id {col_id} - created collection of {col_item_count} assets'
print(log_message)
logging.info(log_message)
else:
print(f'ERROR - {collection_data}')
logging.error(collection_data)
time.sleep(10)
setup.stop_log_dams_netx()
if __name__ == '__main__':
main()