-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsd_tools.py
78 lines (65 loc) · 2.43 KB
/
tsd_tools.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
import pipeline
import json
import subprocess
import logging
logger = logging.getLogger(__name__)
STORAGE_FORMAT = "./data/tsd.{trip_pk}.jsonl"
class JWTWrapper(object):
def __init__(self):
type(self)._instance = self
self._token = None
@property
def token(self):
if self._token is None:
print("Refreshing token ...")
response_body = subprocess.check_output("ssh -Cqt rideserver@rideserver-backend-1 ./manage.py get_admin_jwt", shell=True)
data = json.loads(response_body.split('\n')[0])
self._token = data['token']
print("Got new token")
return self._token
def reset(self):
self._token = None
def __unicode__(self):
return self.token
def __str__(self):
return unicode(self)
@classmethod
def getInstance(cls):
if not hasattr(cls, '_instance'):
cls._instance = JWTWrapper()
return cls._instance
def get_tsd_stubs(sa=None):
params = []
if sa is not None:
params.append('--sa {}'.format(sa))
cmd_base = "ssh -Cqt rideserver@rideserver-backend-1 ./manage.py list_misclassifications --out=json"
cmd = '{} {}'.format(cmd_base, ' '.join(params))
items = json.loads(subprocess.check_output(cmd, shell=True))
return items
def tsd_url(pk):
return 'https://ride.report/__tools/inspect/tripsensordata_raw/{}'.format(pk)
def download_single_tsd(session, token, pk):
url = tsd_url(pk)
headers = {
'Authorization': 'JWT {}'.format(token),
}
response = session.get(url, headers=headers)
if hasattr(response, 'result'):
response = response.result()
response.raise_for_status()
with open(STORAGE_FORMAT.format(trip_pk=pk), 'wb') as f:
f.write(response.text.encode('utf-8'))
def load_tsd_by_pk(pk, force_update=False):
filename = STORAGE_FORMAT.format(trip_pk=pk)
try:
return pipeline.loadTSD(filename, force_update=force_update)
except (IOError, OSError):
import requests
download_single_tsd(requests, JWTWrapper.getInstance(), pk)
return pipeline.loadTSD(filename, force_update=force_update)
def load_many_tsds(stubs, force_update=False):
for stub in stubs:
try:
yield load_tsd_by_pk(stub['trip_pk'], force_update=force_update)
except Exception as e:
logger.error("Failed to load TSD trip_pk={}: {}".format(stub.get('trip_pk', None), repr(e)))