-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptoclient.py
536 lines (411 loc) · 14.9 KB
/
ptoclient.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
import codecs
import json
import datetime
import urllib.parse
from collections import deque
import requests
import pandas as pd
import numpy as np
import dateparser
def _as_time_query_string(v):
if isinstance(v, int):
raise ValueError("...no parsing of epoch times yet...")
elif isinstance(v, str):
dtv = dateparser.parse(v)
elif isinstance(v, datetime.datetime):
dtv = v
return dtv.strftime("%Y-%m-%dT%H:%M:%SZ")
def _parse_group_result(j):
if len(j) == 2:
return {
'group': j[0],
'count': j[1],
}
elif len(j) == 3:
return {
'group0': j[0],
'group1': j[1],
'count': j[2]
}
else:
return {'raw_row': j.dumps()}
def _parse_observation_result(j):
return {
'set_id': j[0],
'time_start': j[1],
'time_end': j[2],
'path': j[3],
'condition': j[4],
'value': j[5]
}
def _parse_set_result(j):
return {
'set': j
}
def _headers_for_token(token):
if token is None:
return dict()
else:
return {"Authorization": "APIKEY "+token}
# def _parse_group_result_fn(j, count_label, *groups):
# if len(groups) == 0:
# return lambda j: {}
# if len(groups) == 1:
# return lambda j: {groups[0]: j[0], count_label: j[1]}
# elif len(groups) == 2:
# return lambda j: {groups[0]: j[0], groups[1]: j[1], count_label: j[2]}
# else:
# raise RuntimeError("bad group count building aggregation result parse function")
class PTOError(Exception):
def __init__(self, status, text):
super().__init__(str(status) + ": " + text)
class PTOQuerySpec:
"""
Represents a query specification: all the parameters
necessary to run a PTO query. Pass to PTOClient to submit
this query on a PTO instance.
"""
def __init__(self):
super().__init__()
self._time_start = None
self._time_end = None
self._set_ids = []
self._on_path = []
self._sources = []
self._targets = []
self._conditions = []
self._group = []
self._options = []
def time(self, time_start, time_end):
self._time_start = _as_time_query_string(time_start)
self._time_end = _as_time_query_string(time_end)
return self
def set_id(self, *args):
for v in args:
if isinstance(v, int):
# integer, render as hex
self._set_ids.append("{:x}".format(v))
elif isinstance(v, str):
# string, strip possible URL parts
self._set_ids.append(v.split("/")[-1])
else:
raise TypeError("can't deal with {} as set_id".format(v))
return self
def on_path(self, *args):
for v in args:
self._on_path.append(str(v))
return self
def source(self, *args):
for v in args:
self._sources.append(str(v))
return self
def target(self, *args):
for v in args:
self._targets.append(str(v))
return self
def condition(self, *args):
for v in args:
self._conditions.append(str(v))
return self
def _append_group(self, gstr):
if len(self._group) >= 2:
raise ValueError("only one- and two-dimensional grouping is currently supported by the PTO")
self._group.append(gstr)
return self
def time_series_year(self):
return self._append_group("year")
def time_series_month(self):
return self._append_group("month")
def time_series_week(self):
return self._append_group("week")
def time_series_day(self):
return self._append_group("day")
def time_series_hour(self):
return self._append_group("hour")
def group_by_day_of_week(self):
return self._append_group("week_day")
def group_by_hour_of_day(self):
return self._append_group("day_hour")
def group_by_condition(self):
return self._append_group("condition")
def group_by_source(self):
return self._append_group("source")
def group_by_target(self):
return self._append_group("target")
def count_unique_targets(self):
self._options.append("count_targets")
def return_sets_only(self):
self._options.append("sets_only")
def _params(self):
params = {}
params['time_start'] = self._time_start
params['time_end'] = self._time_end
if len(self._on_path):
params['on_path'] = self._on_path
if len(self._sources):
params['source'] = self._sources
if len(self._targets):
params['target'] = self._targets
if len(self._conditions):
params['condition'] = self._conditions
if len(self._group):
params['group'] = self._group
if len(self._options):
params['option'] = self._options
return params
class PTOQuery:
"""
Represents a specific query in an instance of the PTO.
"""
def __init__(self, url, token, spec=None):
super().__init__()
self._url = url
self._token = token
self._metadata = None
self._results = None
# self._parse_group_result = _parse_group_result_fn(0, '')
if spec is not None:
self._submit(spec)
def _submit(self, spec):
# In the case of submit, URL starts as a base URL
r = requests.post(self._url+"query/submit",
headers = _headers_for_token(self._token),
params=spec._params())
if r.status_code == 200:
self._metadata = r.json()
self._url = self._metadata["__link"]
else:
raise PTOError(r.status_code, r.text)
# def _extract_group_labels(self, qs):
# qd = urllib.parse.parse_qs(qs)
# if "group" in qd:
# if "options" in qd and "count_targets" in qd["options"]:
# count_label = "targets"
# else:
# count_label = "observations"
# self._json_to_result_element = _parse_group_result_fn(count_label, *qd["group"])
def metadata(self, reload=False):
if (self._metadata is None) or reload:
r = requests.get(self._url, headers = _headers_for_token(self._token))
if r.status_code == 200:
self._metadata = r.json()
# self._extract_group_labels(self._metadata["__encoded"])
return self._metadata
def _reload_http_gen(self, j):
if 'obs' in j:
for o in j['obs']:
yield _parse_observation_result(o)
elif 'sets' in j:
for s in j['sets']:
yield _parse_set_result(s)
elif 'groups' in j:
for g in j['groups']:
yield _parse_group_result(g)
def results(self, reload=False):
"""
Return the results of this query as a Pandas dataframe.
If the query is still executing, returns None
"""
if self._results is None or reload:
m = self.metadata(reload)
if "__result" not in m:
return None
result_url = m["__result"]
while True:
r = requests.get(result_url,
headers = _headers_for_token(self._token))
j = r.json()
if r.status_code == 200:
if self._results is None:
self._results = pd.DataFrame(self._reload_http_gen(j))
else:
self._results = pd.concat([self._results, pd.DataFrame(self._reload_http_gen(j))])
else:
raise PTOError(r.status_code, r.text)
if 'next' in j:
result_url = j['next']
else:
break
self._results.index = np.arange(0,len(self._results))
return self._results
class PTOSet:
def __init__(self, url=None, token=None, obsfile=None):
super().__init__()
if url is None and obsfile is None:
raise ValueError("PTOSet requires either a url or an obsfile")
self._url = url
self._token = token
self._obsfile = obsfile
self._metadata = None
self._obsdata = None
def _reload_file_gen(self):
for line in self._obsfile:
try:
fc = line.strip()[0]
except IndexError:
pass
if fc == '{':
self._metadata = json.loads(line)
elif fc == '[':
ja = json.loads(line)
yield _parse_observation_result(ja)
def _reload_http_gen(self, res):
obsin = codecs.getreader("utf8")(res.content)
for line in obsin:
ja = json.loads(line)
yield {
'time_start': ja[1],
'time_end': ja[2],
'path': ja[3],
'condition': ja[4],
'value': ja[5]
}
def _reload_http_metadata(self):
r = requests.get(self._url,
headers = _headers_for_token(self._token))
if r.status_code == 200:
self._metadata = r.json()
else:
raise PTOError(r.status_code, r.text)
def metadata(self, reload=False):
"""
Retrieve and cache metadata associated with this observation set.
For files, this will also cache all data.
"""
if (self._metadata is None) or ((self._obsfile is None) and reload):
if self._obsfile is not None:
self._obsdata = pd.DataFrame(self._reload_file_gen())
else:
self._reload_http_metadata()
return self._metadata
def observations(self, reload=False):
"""
Return the observations in this set as a Pandas dataframe.
Caches data from the web unless reload is true.
"""
# cache metadata to get a data link
md = self.metadata(reload)
try:
data_url = md["__data"]
except KeyError:
data_url = None
if (self._obsdata is None) or ((self._obsfile is None) and reload):
r = requests.get(data_url, stream=True,
headers =_headers_for_token(self._token))
if r.status_code == 200:
self._obsdata = pd.DataFrame(self._reload_http_gen(r))
else:
raise PTOError(r.status_code, r.text)
return self._obsdata
class PTOClient:
def __init__(self, baseurl, token):
super().__init__()
if baseurl[-1] != "/":
baseurl += "/"
self._baseurl = baseurl
self._token = token
def sets_by_metadata(self, k=None, v=None, source=None, analyzer=None, condition=None):
params = {}
if k:
params["k"] = k
if v:
params["v"] = v
if source:
params["source"] = source
if analyzer:
params["analyzer"] = analyzer
if condition:
params["condition"] = condition
r = requests.get(self._baseurl+"obs/by_metadata", params=params,
headers = _headers_for_token(self._token))
if r.status_code == 200:
# FIXME pagination
return r.json()["sets"]
else:
raise PTOError(r.status_code, r.text)
def all_set_urls(self):
"""
Retrieve the list of all observation sets in the observatory, suitable for use with retrieve_set
"""
r = requests.get(self._baseurl+"obs",
headers = _headers_for_token(self._token))
if r.status_code == 200:
# FIXME pagination
return r.json()["sets"]
else:
raise PTOError(r.status_code, r.text)
def retrieve_set(self, url=None, setid=None):
"""
Retrieve a PTOSet with this client by URL or by set ID.
Prefetches metadata to force error on nonexistant set.
"""
if url is None:
if isinstance(setid, int):
setid = "{:x}".format(setid)
url = self._baseurl + "obs/{}".format(str(setid))
elif not url.startswith(self._baseurl):
raise ValueError("This client cannot connect to {}",format(url))
ptoset = PTOSet(url, self._token)
ptoset.metadata()
return ptoset
def retrieve_query(self, url=None, queryid=None):
"""
Retrieve an existing query with this client by URL or query ID.
Prefetches metadata to force error on nonexistant query.
Use submit_query instead if the query does not yet exist.
"""
if url is None:
url = self._baseurl + "query/{}".format(str(queryid))
elif not url.startswith(self._baseurl):
raise ValueError("This client cannot connect to {}",format(url))
q = PTOQuery(url, self._token)
q.metadata()
return q
def submit_query(self, spec):
q = PTOQuery(self._baseurl, self._token, spec=spec)
q.metadata()
return q
def _retrieve_provenance(url, token):
p = []
r = requests.get(url, headers = _headers_for_token(token))
if r.status_code != 200:
raise PTOError(r.status_code, r.text)
j = r.json()
if "_sources" in j:
p += j["_sources"]
if "_analyzer" in j:
p.append(j["_analyzer"])
return p
class Provenance():
"""
Provenance represents the full provenance of an object: its source and
analyzer antecedents, back to raw data and analyzers.
"""
def __init__(self, url, token):
super().__init__()
self._url = url
self._netloc = urllib.parse.urlparse(url).netloc
self._token = token
self._edges = {}
self._errors = []
self._iterate()
def _iterate(self):
url = self._url
urlq = deque()
while True:
try:
p = _retrieve_provenance(url, self._token)
print("retrieved {}".format(url))
except Exception as e:
self._errors.append(url)
print("error retrieving {}: {}".format(url, str(e)[:40]))
continue
if len(p) > 0:
self._edges[url] = p
for u in p:
if u not in self._edges:
if urllib.parse.urlparse(u).netloc == self._netloc:
urlq.append(u)
if len(urlq) == 0:
break
url = urlq.popleft()