-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathpostgres_async_db.py
More file actions
903 lines (806 loc) · 32.2 KB
/
postgres_async_db.py
File metadata and controls
903 lines (806 loc) · 32.2 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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
import psycopg2
import psycopg2.extras
import os
import aiopg
import json
import math
import time
import datetime
from services.utils import logging
from typing import List, Tuple
from .db_utils import DBResponse, DBPagination, aiopg_exception_handling, \
get_db_ts_epoch_str, translate_run_key, translate_task_key
from .models import FlowRow, RunRow, StepRow, TaskRow, MetadataRow, ArtifactRow
from services.utils import DBConfiguration
AIOPG_ECHO = os.environ.get("AIOPG_ECHO", 0) == "1"
from services.data.service_configs import max_connection_retires, \
connection_retry_wait_time_seconds
WAIT_TIME = 10
# Create database triggers automatically, disabled by default
# Enable with env variable `DB_TRIGGER_CREATE=1`
DB_TRIGGER_CREATE = os.environ.get("DB_TRIGGER_CREATE", 0) == "1"
# Configure DB Table names. Custom names can be supplied through environment variables,
# in case the deployment differs from the default naming scheme from the supplied migrations.
FLOW_TABLE_NAME = os.environ.get("DB_TABLE_NAME_FLOWS", "flows_v3")
RUN_TABLE_NAME = os.environ.get("DB_TABLE_NAME_RUNS", "runs_v3")
STEP_TABLE_NAME = os.environ.get("DB_TABLE_NAME_STEPS", "steps_v3")
TASK_TABLE_NAME = os.environ.get("DB_TABLE_NAME_TASKS", "tasks_v3")
METADATA_TABLE_NAME = os.environ.get("DB_TABLE_NAME_METADATA", "metadata_v3")
ARTIFACT_TABLE_NAME = os.environ.get("DB_TABLE_NAME_ARTIFACT", "artifact_v3")
class _AsyncPostgresDB(object):
connection = None
flow_table_postgres = None
run_table_postgres = None
step_table_postgres = None
task_table_postgres = None
artifact_table_postgres = None
metadata_table_postgres = None
pool = None
db_conf: DBConfiguration = None
def __init__(self, name='global'):
self.name = name
self.logger = logging.getLogger("AsyncPostgresDB:{name}".format(name=self.name))
tables = []
self.flow_table_postgres = AsyncFlowTablePostgres(self)
self.run_table_postgres = AsyncRunTablePostgres(self)
self.step_table_postgres = AsyncStepTablePostgres(self)
self.task_table_postgres = AsyncTaskTablePostgres(self)
self.artifact_table_postgres = AsyncArtifactTablePostgres(self)
self.metadata_table_postgres = AsyncMetadataTablePostgres(self)
tables.append(self.flow_table_postgres)
tables.append(self.run_table_postgres)
tables.append(self.step_table_postgres)
tables.append(self.task_table_postgres)
tables.append(self.artifact_table_postgres)
tables.append(self.metadata_table_postgres)
self.tables = tables
async def _init(self, db_conf: DBConfiguration, create_triggers=DB_TRIGGER_CREATE, create_tables=True):
# todo make poolsize min and max configurable as well as timeout
# todo add retry and better error message
retries = max_connection_retires
for i in range(retries):
try:
self.pool = await aiopg.create_pool(
db_conf.dsn,
timeout=db_conf.pool_timeout,
minsize=db_conf.pool_min,
maxsize=db_conf.pool_max,
pool_recycle=db_conf.pool_recycle,
echo=AIOPG_ECHO)
# Clean existing trigger functions before creating new ones
if create_triggers:
self.logger.info("Cleanup existing notify triggers")
await PostgresUtils.function_cleanup(self)
for table in self.tables:
await table._init(create_tables=create_tables, create_triggers=create_triggers)
self.logger.info(
"Connection established.\n"
" Pool min: {pool_min} max: {pool_max} timeout: {pool_timeout} recycle: {pool_recycle}\n".format(
pool_min=self.pool.minsize,
pool_max=self.pool.maxsize,
pool_timeout=self.pool.timeout,
pool_recycle=db_conf.pool_recycle))
break # Break the retry loop
except Exception as e:
self.logger.exception("Exception occured")
if retries - i <= 1:
raise e
time.sleep(connection_retry_wait_time_seconds)
def get_table_by_name(self, table_name: str):
for table in self.tables:
if table.table_name == table_name:
return table
return None
async def get_run_ids(self, flow_id: str, run_id: str):
return await self.run_table_postgres.get_run(flow_id, run_id,
expanded=True)
async def get_task_ids(self, flow_id: str, run_id: str,
step_name: str, task_name: str):
return await self.task_table_postgres.get_task(flow_id, run_id,
step_name, task_name,
expanded=True)
class AsyncPostgresDB(object):
__instance = None
@staticmethod
def get_instance():
return AsyncPostgresDB()
def __init__(self):
if not AsyncPostgresDB.__instance:
AsyncPostgresDB.__instance = _AsyncPostgresDB()
def __getattribute__(self, name):
return getattr(AsyncPostgresDB.__instance, name)
class AsyncPostgresTable(object):
db = None
table_name = None
schema_version = 1
keys: List[str] = []
primary_keys: List[str] = None
trigger_keys: List[str] = None
ordering: List[str] = None
joins: List[str] = None
select_columns: List[str] = keys
join_columns: List[str] = None
_command = None
_insert_command = None
_filters = None
_base_query = "SELECT {0} from"
_row_type = None
def __init__(self, db: _AsyncPostgresDB = None):
self.db = db
if self.table_name is None or self._command is None:
raise NotImplementedError(
"need to specify table name and create command")
async def _init(self, create_tables: bool, create_triggers: bool):
if create_tables:
await PostgresUtils.create_if_missing(self.db, self.table_name, self._command)
if create_triggers:
self.db.logger.info(
"Create notify trigger for {table_name}\n Keys: {keys}".format(
table_name=self.table_name, keys=self.trigger_keys))
await PostgresUtils.trigger_notify(db=self.db, table_name=self.table_name, keys=self.trigger_keys)
async def get_records(self, filter_dict={}, fetch_single=False,
ordering: List[str] = None, limit: int = 0, expanded=False) -> DBResponse:
conditions = []
values = []
for col_name, col_val in filter_dict.items():
conditions.append("{} = %s".format(col_name))
values.append(col_val)
response, _ = await self.find_records(
conditions=conditions, values=values, fetch_single=fetch_single,
order=ordering, limit=limit, expanded=expanded
)
return response
async def find_records(self, conditions: List[str] = None, values=[], fetch_single=False,
limit: int = 0, offset: int = 0, order: List[str] = None, expanded=False,
enable_joins=False) -> Tuple[DBResponse, DBPagination]:
sql_template = """
SELECT * FROM (
SELECT
{keys}
FROM {table_name}
{joins}
) T
{where}
{order_by}
{limit}
{offset}
"""
select_sql = sql_template.format(
keys=",".join(
self.select_columns + (self.join_columns if enable_joins and self.join_columns else [])),
table_name=self.table_name,
joins=" ".join(self.joins) if enable_joins and self.joins is not None else "",
where="WHERE {}".format(" AND ".join(conditions)) if conditions else "",
order_by="ORDER BY {}".format(", ".join(order)) if order else "",
limit="LIMIT {}".format(limit) if limit else "",
offset="OFFSET {}".format(offset) if offset else ""
).strip()
return await self.execute_sql(select_sql=select_sql, values=values, fetch_single=fetch_single,
expanded=expanded, limit=limit, offset=offset)
async def execute_sql(self, select_sql: str, values=[], fetch_single=False,
expanded=False, limit: int = 0, offset: int = 0) -> Tuple[DBResponse, DBPagination]:
try:
with (
await self.db.pool.cursor(
cursor_factory=psycopg2.extras.DictCursor
)
) as cur:
await cur.execute(select_sql, values)
rows = []
records = await cur.fetchall()
for record in records:
row = self._row_type(**record) # pylint: disable=not-callable
rows.append(row.serialize(expanded))
count = len(rows)
# Will raise IndexError in case fetch_single=True and there's no results
body = rows[0] if fetch_single else rows
pagination = DBPagination(
limit=limit,
offset=offset,
count=count,
page=math.floor(int(offset) / max(int(limit), 1)) + 1,
)
cur.close()
return DBResponse(response_code=200, body=body), pagination
except IndexError as error:
return aiopg_exception_handling(error), None
except (Exception, psycopg2.DatabaseError) as error:
self.db.logger.exception("Exception occured")
return aiopg_exception_handling(error), None
async def create_record(self, record_dict):
# note: need to maintain order
cols = []
values = []
for col_name, col_val in record_dict.items():
cols.append(col_name)
values.append(col_val)
# add create ts
cols.append("ts_epoch")
values.append(get_db_ts_epoch_str())
str_format = []
for _ in cols:
str_format.append("%s")
seperator = ", "
insert_sql = """
INSERT INTO {0}({1}) VALUES({2})
RETURNING *
""".format(
self.table_name, seperator.join(cols), seperator.join(str_format)
)
try:
response_body = {}
with (
await self.db.pool.cursor(
cursor_factory=psycopg2.extras.DictCursor
)
) as cur:
await cur.execute(insert_sql, tuple(values))
records = await cur.fetchall()
record = records[0]
filtered_record = {}
for key, value in record.items():
if key in self.keys:
filtered_record[key] = value
response_body = self._row_type(**filtered_record).serialize() # pylint: disable=not-callable
# todo make sure connection is closed even with error
cur.close()
return DBResponse(response_code=200, body=response_body)
except (Exception, psycopg2.DatabaseError) as error:
self.db.logger.exception("Exception occured")
return aiopg_exception_handling(error)
async def update_row(self, filter_dict={}, update_dict={}):
# generate where clause
filters = []
for col_name, col_val in filter_dict.items():
v = str(col_val).strip("'")
if not v.isnumeric():
v = "'" + v + "'"
filters.append(col_name + "=" + str(v))
seperator = " and "
where_clause = ""
if bool(filter_dict):
where_clause = seperator.join(filters)
sets = []
for col_name, col_val in update_dict.items():
sets.append(col_name + " = " + str(col_val))
set_seperator = ", "
set_clause = ""
if bool(filter_dict):
set_clause = set_seperator.join(sets)
update_sql = """
UPDATE {0} SET {1} WHERE {2};
""".format(self.table_name, set_clause, where_clause)
try:
with (
await self.db.pool.cursor(
cursor_factory=psycopg2.extras.DictCursor
)
) as cur:
await cur.execute(update_sql)
if cur.rowcount < 1:
return DBResponse(response_code=404,
body={"msg": "could not find row"})
if cur.rowcount > 1:
return DBResponse(response_code=500,
body={"msg": "duplicate rows"})
body = {"rowcount": cur.rowcount}
# todo make sure connection is closed even with error
cur.close()
return DBResponse(response_code=200, body=body)
except (Exception, psycopg2.DatabaseError) as error:
self.db.logger.exception("Exception occured")
return aiopg_exception_handling(error)
class PostgresUtils(object):
@staticmethod
async def create_if_missing(db: _AsyncPostgresDB, table_name, command):
with (await db.pool.cursor()) as cur:
try:
await cur.execute(
"select * from information_schema.tables where table_name=%s",
(table_name,),
)
table_exist = bool(cur.rowcount)
if not table_exist:
await cur.execute(command)
finally:
cur.close()
# todo add method to check schema version
@staticmethod
async def function_cleanup(db: _AsyncPostgresDB):
name_prefix = "notify_ui"
_command = """
DO $$DECLARE r RECORD;
BEGIN
FOR r IN SELECT routine_schema, routine_name FROM information_schema.routines
WHERE routine_name LIKE '{prefix}%'
LOOP
EXECUTE 'DROP FUNCTION ' || quote_ident(r.routine_schema) || '.' || quote_ident(r.routine_name) || '() CASCADE';
END LOOP;
END$$;
""".format(
prefix=name_prefix
)
with (await db.pool.cursor()) as cur:
await cur.execute(_command)
cur.close()
@staticmethod
async def trigger_notify(db: _AsyncPostgresDB, table_name, keys: List[str] = None, schema="public"):
if not keys:
pass
name_prefix = "notify_ui"
operations = ["INSERT", "UPDATE", "DELETE"]
_commands = ["""
CREATE OR REPLACE FUNCTION {schema}.{prefix}_{table}() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
rec RECORD;
BEGIN
CASE TG_OP
WHEN 'INSERT', 'UPDATE' THEN
rec := NEW;
WHEN 'DELETE' THEN
rec := OLD;
ELSE
RAISE EXCEPTION 'Unknown TG_OP: "%"', TG_OP;
END CASE;
PERFORM pg_notify('notify', json_build_object(
'table', TG_TABLE_NAME,
'schema', TG_TABLE_SCHEMA,
'operation', TG_OP,
'data', json_build_object({keys})
)::text);
RETURN rec;
END;
$$;
""".format(
schema=schema,
prefix=name_prefix,
table=table_name,
keys=", ".join(map(lambda k: "'{0}', rec.{0}".format(k), keys)),
events=" OR ".join(operations)
)]
_commands += ["DROP TRIGGER IF EXISTS {prefix}_{table} ON {schema}.{table};".format(
schema=schema,
prefix=name_prefix,
table=table_name
)]
_commands += ["""
CREATE TRIGGER {prefix}_{table} AFTER {events} ON {schema}.{table}
FOR EACH ROW EXECUTE PROCEDURE {schema}.{prefix}_{table}();
""".format(
schema=schema,
prefix=name_prefix,
table=table_name,
events=" OR ".join(operations)
)]
# This enables trigger on both replica and non-replica mode
_commands += ["ALTER TABLE {schema}.{table} ENABLE ALWAYS TRIGGER {prefix}_{table};".format(
schema=schema,
prefix=name_prefix,
table=table_name
)]
with (await db.pool.cursor()) as cur:
for _command in _commands:
await cur.execute(_command)
cur.close()
class AsyncFlowTablePostgres(AsyncPostgresTable):
flow_dict = {}
table_name = FLOW_TABLE_NAME
keys = ["flow_id", "user_name", "ts_epoch", "tags", "system_tags"]
primary_keys = ["flow_id"]
trigger_keys = primary_keys
select_columns = keys
_command = """
CREATE TABLE {0} (
flow_id VARCHAR(255) PRIMARY KEY,
user_name VARCHAR(255),
ts_epoch BIGINT NOT NULL,
tags JSONB,
system_tags JSONB
)
""".format(
table_name
)
_row_type = FlowRow
async def add_flow(self, flow: FlowRow):
dict = {
"flow_id": flow.flow_id,
"user_name": flow.user_name,
"tags": json.dumps(flow.tags),
"system_tags": json.dumps(flow.system_tags),
}
return await self.create_record(dict)
async def get_flow(self, flow_id: str):
filter_dict = {"flow_id": flow_id}
return await self.get_records(filter_dict=filter_dict, fetch_single=True)
async def get_all_flows(self):
return await self.get_records()
class AsyncRunTablePostgres(AsyncPostgresTable):
run_dict = {}
run_by_flow_dict = {}
_current_count = 0
_row_type = RunRow
table_name = RUN_TABLE_NAME
keys = ["flow_id", "run_number", "run_id",
"user_name", "ts_epoch", "last_heartbeat_ts", "tags", "system_tags"]
primary_keys = ["flow_id", "run_number"]
trigger_keys = primary_keys + ["last_heartbeat_ts"]
select_columns = keys
flow_table_name = AsyncFlowTablePostgres.table_name
_command = """
CREATE TABLE {0} (
flow_id VARCHAR(255) NOT NULL,
run_number SERIAL NOT NULL,
run_id VARCHAR(255),
user_name VARCHAR(255),
ts_epoch BIGINT NOT NULL,
tags JSONB,
system_tags JSONB,
last_heartbeat_ts BIGINT,
PRIMARY KEY(flow_id, run_number),
FOREIGN KEY(flow_id) REFERENCES {1} (flow_id),
UNIQUE (flow_id, run_id)
)
""".format(
table_name, flow_table_name
)
async def add_run(self, run: RunRow):
dict = {
"flow_id": run.flow_id,
"user_name": run.user_name,
"tags": json.dumps(run.tags),
"system_tags": json.dumps(run.system_tags),
"run_id": run.run_id,
}
return await self.create_record(dict)
async def get_run(self, flow_id: str, run_id: str, expanded: bool = False):
key, value = translate_run_key(run_id)
filter_dict = {"flow_id": flow_id, key: str(value)}
return await self.get_records(filter_dict=filter_dict,
fetch_single=True, expanded=expanded)
async def get_all_runs(self, flow_id: str):
filter_dict = {"flow_id": flow_id}
return await self.get_records(filter_dict=filter_dict)
async def update_heartbeat(self, flow_id: str, run_id: str):
run_key, run_value = translate_run_key(run_id)
filter_dict = {"flow_id": flow_id,
run_key: str(run_value)}
set_dict = {
"last_heartbeat_ts": int(datetime.datetime.utcnow().timestamp())
}
result = await self.update_row(filter_dict=filter_dict,
update_dict=set_dict)
body = {"wait_time_in_seconds": WAIT_TIME}
return DBResponse(response_code=result.response_code, body=body)
class AsyncStepTablePostgres(AsyncPostgresTable):
step_dict = {}
run_to_step_dict = {}
_row_type = StepRow
table_name = STEP_TABLE_NAME
keys = ["flow_id", "run_number", "run_id", "step_name",
"user_name", "ts_epoch", "tags", "system_tags"]
primary_keys = ["flow_id", "run_number", "step_name"]
trigger_keys = primary_keys
select_columns = keys
run_table_name = AsyncRunTablePostgres.table_name
_command = """
CREATE TABLE {0} (
flow_id VARCHAR(255) NOT NULL,
run_number BIGINT NOT NULL,
run_id VARCHAR(255),
step_name VARCHAR(255) NOT NULL,
user_name VARCHAR(255),
ts_epoch BIGINT NOT NULL,
tags JSONB,
system_tags JSONB,
PRIMARY KEY(flow_id, run_number, step_name),
FOREIGN KEY(flow_id, run_number) REFERENCES {1} (flow_id, run_number),
UNIQUE(flow_id, run_id, step_name)
)
""".format(
table_name, run_table_name
)
async def add_step(self, step_object: StepRow):
dict = {
"flow_id": step_object.flow_id,
"run_number": str(step_object.run_number),
"run_id": step_object.run_id,
"step_name": step_object.step_name,
"user_name": step_object.user_name,
"tags": json.dumps(step_object.tags),
"system_tags": json.dumps(step_object.system_tags),
}
return await self.create_record(dict)
async def get_steps(self, flow_id: str, run_id: str):
run_id_key, run_id_value = translate_run_key(run_id)
filter_dict = {"flow_id": flow_id,
run_id_key: run_id_value}
return await self.get_records(filter_dict=filter_dict)
async def get_step(self, flow_id: str, run_id: str, step_name: str):
run_id_key, run_id_value = translate_run_key(run_id)
filter_dict = {
"flow_id": flow_id,
run_id_key: run_id_value,
"step_name": step_name,
}
return await self.get_records(filter_dict=filter_dict, fetch_single=True)
class AsyncTaskTablePostgres(AsyncPostgresTable):
task_dict = {}
step_to_task_dict = {}
_current_count = 0
_row_type = TaskRow
table_name = TASK_TABLE_NAME
keys = ["flow_id", "run_number", "run_id", "step_name", "task_id",
"task_name", "user_name", "ts_epoch", "last_heartbeat_ts", "tags", "system_tags"]
primary_keys = ["flow_id", "run_number", "step_name", "task_id"]
trigger_keys = primary_keys
select_columns = keys
step_table_name = AsyncStepTablePostgres.table_name
_command = """
CREATE TABLE {0} (
flow_id VARCHAR(255) NOT NULL,
run_number BIGINT NOT NULL,
run_id VARCHAR(255),
step_name VARCHAR(255) NOT NULL,
task_id BIGSERIAL PRIMARY KEY,
task_name VARCHAR(255),
user_name VARCHAR(255),
ts_epoch BIGINT NOT NULL,
tags JSONB,
system_tags JSONB,
last_heartbeat_ts BIGINT,
FOREIGN KEY(flow_id, run_number, step_name) REFERENCES {1} (flow_id, run_number, step_name),
UNIQUE (flow_id, run_number, step_name, task_name)
)
""".format(
table_name, step_table_name
)
async def add_task(self, task: TaskRow):
# todo backfill run_number if missing?
dict = {
"flow_id": task.flow_id,
"run_number": str(task.run_number),
"run_id": task.run_id,
"step_name": task.step_name,
"task_name": task.task_name,
"user_name": task.user_name,
"tags": json.dumps(task.tags),
"system_tags": json.dumps(task.system_tags),
}
return await self.create_record(dict)
async def get_tasks(self, flow_id: str, run_id: str, step_name: str):
run_id_key, run_id_value = translate_run_key(run_id)
filter_dict = {
"flow_id": flow_id,
run_id_key: run_id_value,
"step_name": step_name,
}
return await self.get_records(filter_dict=filter_dict)
async def get_task(self, flow_id: str, run_id: str, step_name: str,
task_id: str, expanded: bool = False):
run_id_key, run_id_value = translate_run_key(run_id)
task_id_key, task_id_value = translate_task_key(task_id)
filter_dict = {
"flow_id": flow_id,
run_id_key: run_id_value,
"step_name": step_name,
task_id_key: task_id_value,
}
return await self.get_records(filter_dict=filter_dict,
fetch_single=True, expanded=expanded)
async def update_heartbeat(self, flow_id: str, run_id: str, step_name: str,
task_id: str):
run_key, run_value = translate_run_key(run_id)
task_key, task_value = translate_task_key(task_id)
filter_dict = {"flow_id": flow_id,
run_key: str(run_value),
"step_name": step_name,
task_key: str(task_value)}
set_dict = {
"last_heartbeat_ts": int(datetime.datetime.utcnow().timestamp())
}
result = await self.update_row(filter_dict=filter_dict,
update_dict=set_dict)
body = {"wait_time_in_seconds": WAIT_TIME}
return DBResponse(response_code=result.response_code, body=body)
class AsyncMetadataTablePostgres(AsyncPostgresTable):
metadata_dict = {}
run_to_metadata_dict = {}
_current_count = 0
_row_type = MetadataRow
table_name = METADATA_TABLE_NAME
keys = ["flow_id", "run_number", "run_id", "step_name", "task_id", "task_name", "id",
"field_name", "value", "type", "user_name", "ts_epoch", "tags", "system_tags"]
primary_keys = ["flow_id", "run_number",
"step_name", "task_id", "field_name"]
trigger_keys = ["flow_id", "run_number",
"step_name", "task_id", "field_name", "value"]
select_columns = keys
_command = """
CREATE TABLE {0} (
flow_id VARCHAR(255),
run_number BIGINT NOT NULL,
run_id VARCHAR(255),
step_name VARCHAR(255) NOT NULL,
task_name VARCHAR(255),
task_id BIGINT NOT NULL,
id BIGSERIAL NOT NULL,
field_name VARCHAR(255) NOT NULL,
value TEXT NOT NULL,
type VARCHAR(255) NOT NULL,
user_name VARCHAR(255),
ts_epoch BIGINT NOT NULL,
tags JSONB,
system_tags JSONB,
PRIMARY KEY(id, flow_id, run_number, step_name, task_id, field_name)
)
""".format(table_name)
async def add_metadata(
self,
flow_id,
run_number,
run_id,
step_name,
task_id,
task_name,
field_name,
value,
type,
user_name,
tags,
system_tags,
):
dict = {
"flow_id": flow_id,
"run_number": str(run_number),
"run_id": run_id,
"step_name": step_name,
"task_id": str(task_id),
"task_name": task_name,
"field_name": field_name,
"value": value,
"type": type,
"user_name": user_name,
"tags": json.dumps(tags),
"system_tags": json.dumps(system_tags),
}
return await self.create_record(dict)
async def get_metadata_in_runs(self, flow_id: str, run_id: str):
run_id_key, run_id_value = translate_run_key(run_id)
filter_dict = {"flow_id": flow_id,
run_id_key: run_id_value}
return await self.get_records(filter_dict=filter_dict)
async def get_metadata(
self, flow_id: str, run_id: int, step_name: str, task_id: str
):
run_id_key, run_id_value = translate_run_key(run_id)
task_id_key, task_id_value = translate_task_key(task_id)
filter_dict = {
"flow_id": flow_id,
run_id_key: run_id_value,
"step_name": step_name,
task_id_key: task_id_value,
}
return await self.get_records(filter_dict=filter_dict)
class AsyncArtifactTablePostgres(AsyncPostgresTable):
artifact_dict = {}
run_to_artifact_dict = {}
step_to_artifact_dict = {}
task_to_artifact_dict = {}
current_count = 0
_row_type = ArtifactRow
table_name = ARTIFACT_TABLE_NAME
ordering = ["attempt_id DESC"]
keys = ["flow_id", "run_number", "run_id", "step_name", "task_id", "task_name", "name", "location",
"ds_type", "sha", "type", "content_type", "user_name", "attempt_id", "ts_epoch", "tags", "system_tags"]
primary_keys = ["flow_id", "run_number",
"step_name", "task_id", "attempt_id", "name"]
trigger_keys = primary_keys
select_columns = keys
_command = """
CREATE TABLE {0} (
flow_id VARCHAR(255) NOT NULL,
run_number BIGINT NOT NULL,
run_id VARCHAR(255),
step_name VARCHAR(255) NOT NULL,
task_id BIGINT NOT NULL,
task_name VARCHAR(255),
name VARCHAR(255) NOT NULL,
location VARCHAR(255) NOT NULL,
ds_type VARCHAR(255) NOT NULL,
sha VARCHAR(255),
type VARCHAR(255),
content_type VARCHAR(255),
user_name VARCHAR(255),
attempt_id SMALLINT NOT NULL,
ts_epoch BIGINT NOT NULL,
tags JSONB,
system_tags JSONB,
PRIMARY KEY(flow_id, run_number, step_name, task_id, attempt_id, name)
)
""".format(
table_name
)
async def add_artifact(
self,
flow_id,
run_number,
run_id,
step_name,
task_id,
task_name,
name,
location,
ds_type,
sha,
type,
content_type,
user_name,
attempt_id,
tags,
system_tags,
):
dict = {
"flow_id": flow_id,
"run_number": str(run_number),
"run_id": run_id,
"step_name": step_name,
"task_id": str(task_id),
"task_name": task_name,
"name": name,
"location": location,
"ds_type": ds_type,
"sha": sha,
"type": type,
"content_type": content_type,
"user_name": user_name,
"attempt_id": str(attempt_id),
"tags": json.dumps(tags),
"system_tags": json.dumps(system_tags),
}
return await self.create_record(dict)
async def get_artifacts_in_runs(self, flow_id: str, run_id: int):
run_id_key, run_id_value = translate_run_key(run_id)
filter_dict = {
"flow_id": flow_id,
run_id_key: run_id_value,
}
return await self.get_records(filter_dict=filter_dict,
ordering=self.ordering)
async def get_artifact_in_steps(self, flow_id: str, run_id: int, step_name: str):
run_id_key, run_id_value = translate_run_key(run_id)
filter_dict = {
"flow_id": flow_id,
run_id_key: run_id_value,
"step_name": step_name,
}
return await self.get_records(filter_dict=filter_dict,
ordering=self.ordering)
async def get_artifact_in_task(
self, flow_id: str, run_id: int, step_name: str, task_id: int
):
run_id_key, run_id_value = translate_run_key(run_id)
task_id_key, task_id_value = translate_task_key(task_id)
filter_dict = {
"flow_id": flow_id,
run_id_key: run_id_value,
"step_name": step_name,
task_id_key: task_id_value,
}
return await self.get_records(filter_dict=filter_dict,
ordering=self.ordering)
async def get_artifact(
self, flow_id: str, run_id: int, step_name: str, task_id: int, name: str
):
run_id_key, run_id_value = translate_run_key(run_id)
task_id_key, task_id_value = translate_task_key(task_id)
filter_dict = {
"flow_id": flow_id,
run_id_key: run_id_value,
"step_name": step_name,
task_id_key: task_id_value,
'"name"': name,
}
return await self.get_records(filter_dict=filter_dict,
fetch_single=True, ordering=self.ordering)