-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb_cache.py
214 lines (168 loc) · 7.89 KB
/
db_cache.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
import requests
import time
import json
import os
import pandas as pd
from sta_dashboard import db
from sta_dashboard.models import Thing, Datastream, ObservedProperty
from sta_dashboard.utils import *
with open('endpoints.json') as f:
ENDPOINTS = json.load(f)
class Endpoint:
def __init__(self, endpoint_name):
self.endpoint_name = endpoint_name
response = requests.get(ENDPOINTS[self.endpoint_name]['url'])
entities_url = {}
for entity in response.json()['value']:
entities_url[entity['name']] = entity['url']
self.entities_url = entities_url
def get_locations_from_locations(self):
url = self.entities_url['Locations']
locations_list = []
response = requests.get(url)
while True:
locations_list.extend(
response.json()['value']
)
if '@iot.nextLink' in response.json().keys():
response = requests.get(response.json()['@iot.nextLink'])
else:
break
return locations_list
def get_things_and_datastreams(self):
url = self.entities_url['Things']
url += '?$expand=Locations($select=location),Datastreams'
cached_things = []
response = requests.get(url)
while True:
response_json = response.json()
for thing in response_json['value']:
if not thing['Locations'] or not thing['@iot.id']:
continue # skip the records that don't have location info or an iot.id
for thing_key in ['name', 'description', 'Datastreams']:
if thing_key not in thing.keys():
thing[thing_key] = 'NA'
cached_things.append(
{
'id': '@'.join([str(thing['@iot.id']), endpoint]),
'endpoint': self.endpoint_name,
'name': thing['name'],
'description': thing['description'],
'location_geojson': thing['Locations'][0]['location'],
'datastreams': thing['Datastreams']
}
)
if '@iot.nextLink' in response.json().keys():
response = requests.get(response.json()['@iot.nextLink'])
else:
break
cached_things_df = pd.DataFrame.from_records(cached_things)
cached_things_df = cached_things_df.drop_duplicates(subset=['id'])
cached_things = cached_things_df.to_dict(orient='records')
return cached_things
def get_observed_properties(self):
url = self.entities_url['ObservedProperties']
url += '?$expand=Datastreams([email protected])'
cached_ops = []
response = requests.get(url)
while True:
response_json = response.json()
for op in response_json['value']:
if not op['Datastreams']:
continue
ds_list = ','.join(
['@'.join([str(list(v.values())[0]), endpoint]) for v in op['Datastreams']])
if '[email protected]' in op.keys():
next_resp = requests.get(op['[email protected]'])
next_ds = next_resp.json()['value']
ds_list = \
','.join([ds_list, ','.join(['@'.join([str(list(v.values())[0]), endpoint])
for v in next_ds])])
while '@iot.nextLink' in next_resp.json().keys():
next_resp = requests.get(
next_resp.json()['@iot.nextLink'])
next_ds = next_resp.json()['value']
ds_list = \
','.join([ds_list, ','.join(['@'.join([str(list(v.values())[0]), endpoint])
for v in next_ds])])
cached_ops.append(
{
'id': '@'.join([str(op['@iot.id']), endpoint]),
'endpoint': endpoint,
'name': op['name'],
'description': op['description'],
'definition': op['definition'],
'datastreams': ds_list
}
)
if '@iot.nextLink' in response.json().keys():
response = requests.get(response.json()['@iot.nextLink'])
else:
break
cached_ops_df = pd.DataFrame.from_records(cached_ops)
cached_ops_df = cached_ops_df.drop_duplicates(subset=['id'])
cached_ops = cached_ops_df.to_dict(orient='records')
return cached_ops
if __name__ == '__main__':
if os.environ['DROP_ALL'].title() == "True" or \
not os.path.exists(os.path.join('sta_dashboard', 'data', os.environ['SQLITE_DB_FILENAME'])):
db.drop_all()
db.create_all()
endpoints_to_cache = ENDPOINTS
else:
cached_endpoints = \
[t[0] for t in Thing.query.with_entities(Thing.endpoint).distinct().all()]
selected_endpoints = \
[k for k, v in ENDPOINTS.items() if v['include']]
endpoints_to_cache = \
{k: ENDPOINTS[k]
for k in list(set(selected_endpoints) - set(cached_endpoints))
if ENDPOINTS[k]['include']
}
for endpoint, endpoint_values in list(endpoints_to_cache.items()):
if not endpoint_values['include']:
continue
print('{}...'.format(endpoint))
start_timestamp = time.time()
edp = Endpoint(endpoint)
print('Caching things and datastreams...')
cached_things = edp.get_things_and_datastreams()
print('Caching observed properties...')
cached_ops = edp.get_observed_properties()
# Insert thing and datastream rows
for thing in cached_things:
new_thing_row = Thing(**thing)
new_thing_row.datastreams = \
','.join(['@'.join([str(ds['@iot.id']), endpoint]) for ds in thing['datastreams']])
db.session.add(new_thing_row)
ds_ids = []
# Add datastream rows
for ds in thing['datastreams']:
# 'phenomenonTime' and 'resultTime' are optional properties
if 'phenomenonTime' in ds.keys() and ds['phenomenonTime']:
phenomenonStartDate, phenomenonEndDate = \
[convert_date(d) for d in ds['phenomenonTime'].split('/')]
else:
phenomenonStartDate, phenomenonEndDate = None, None
if str(ds['@iot.id']) in ds_ids:
continue
ds_ids.append(str(ds['@iot.id']))
new_datastream_row = Datastream(
id='@'.join([str(ds['@iot.id']), endpoint]),
endpoint=endpoint,
name=ds['name'],
description=ds['description'],
observationType=ds['observationType'],
unitOfMeasurement=ds['unitOfMeasurement'],
phenomenonStartDate=phenomenonStartDate,
phenomenonEndDate=phenomenonEndDate,
selfLink=ds['@iot.selfLink'],
thingId=new_thing_row.id
)
db.session.add(new_datastream_row)
# Insert observed property rows
for op in cached_ops:
new_op_row = ObservedProperty(**op)
db.session.add(new_op_row)
print('{} finished within {:.2f} seconds'.format(endpoint, time.time() - start_timestamp))
db.session.commit()