-
Notifications
You must be signed in to change notification settings - Fork 3
/
import.py
executable file
·95 lines (65 loc) · 2.33 KB
/
import.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
#!/usr/bin/env python3
from coverage_map import CoverageMap
import rest_api
import features
import logging
import coloredlogs
import argparse
import time
MAX_API_CALLS = 50
logger = logging.getLogger(__name__)
def import_new_features(node_eui, exclude_list):
coverage = CoverageMap(node_eui)
for feature in get_new_features(node_eui, exclude_list):
if coverage.exists(feature):
logger.info('Found last imported data point, stopping.')
break
gateway_eui = feature['properties']['gateway_eui']
if not coverage.gateway_seen(gateway_eui):
gateway_details = rest_api.fetch_gateway(gateway_eui)
gateway = features.build_gateway(gateway_details)
coverage.add_gateway(gateway)
coverage.add(feature)
coverage.save_all()
def get_new_features(node_eui, exclude_list):
offset = 0
api_calls = 0
while True:
api_calls += 1
api_result = rest_api.fetch_data_points(node_eui, offset)
count = 0
for data_point in api_result:
try:
count += 1
if data_point['gateway_eui'] in exclude_list:
continue
feature = features.build(data_point)
print('.', end='')
yield feature
except:
print('x', end='')
if count == 0:
logger.info('Reached end of data stream.')
return
print('')
if api_calls >= MAX_API_CALLS:
logger.error('Exceeded the max numbers of API calls, aborting.')
return
logger.info('Giving a break to the API server...')
time.sleep(10)
offset += 100
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='count')
parser.add_argument('node_eui', help='Identifier of the node to import')
parser.add_argument('-x', '--exclude', action='append', default=[],
help='Exclude gateways, multiple gateway EUIs allowed')
return parser.parse_args()
def main():
args = parse_args()
loglevel = logging.INFO if args.verbose is None else logging.DEBUG
logging.basicConfig(level=loglevel)
coloredlogs.install()
import_new_features(args.node_eui, args.exclude)
if __name__ == "__main__":
main()