-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathdb_utils.py
More file actions
70 lines (52 loc) · 1.98 KB
/
db_utils.py
File metadata and controls
70 lines (52 loc) · 1.98 KB
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
import psycopg2
import collections
import time
import json
DBResponse = collections.namedtuple("DBResponse", "response_code body")
DBPagination = collections.namedtuple(
"DBPagination", "limit offset count page")
def aiopg_exception_handling(exception):
err_msg = str(exception)
body = {"err_msg": err_msg}
if isinstance(exception, psycopg2.IntegrityError):
if "duplicate key" in err_msg:
return DBResponse(response_code=409, body=body)
elif "foreign key" in err_msg:
return DBResponse(response_code=404, body=body)
else:
return DBResponse(response_code=500, body=body)
elif isinstance(exception, psycopg2.errors.UniqueViolation):
return DBResponse(response_code=409, body=body)
elif isinstance(exception, IndexError):
return DBResponse(response_code=404, body={})
else:
return DBResponse(response_code=500, body=body)
def get_db_ts_epoch_str():
return str(int(round(time.time() * 1000)))
def translate_run_key(v: str):
value = str(v)
return "run_number" if value.isnumeric() else "run_id", value
def translate_task_key(v: str):
value = str(v)
return "task_id" if value.isnumeric() else "task_name", value
def get_exposed_run_id(run_number, run_id):
if run_id is not None:
return run_id
return run_number
def get_exposed_task_id(task_id, task_name):
if task_name is not None:
return task_name
return task_id
def get_latest_attempt_id_for_tasks(artifacts):
attempt_ids = {}
for artifact in artifacts:
attempt_ids[artifact['task_id']] = max(
artifact['attempt_id'], attempt_ids.get(artifact['task_id'], 0))
return attempt_ids
def filter_artifacts_by_attempt_id_for_tasks(artifacts):
attempt_ids = get_latest_attempt_id_for_tasks(artifacts)
result = []
for artifact in artifacts:
if artifact['attempt_id'] == attempt_ids[artifact['task_id']]:
result.append(artifact)
return result