@@ -18,7 +18,7 @@ class ModelRun(DbObject):
18
18
model_id = Field .String ("model_id" )
19
19
20
20
def upsert_labels (self , label_ids , timeout_seconds = 60 ):
21
- """ Calls GraphQL API to start the MEA labels registration process
21
+ """ Adds data rows and labels to a model run
22
22
Args:
23
23
label_ids (list): label ids to insert
24
24
timeout_seconds (float): Max waiting time, in seconds.
@@ -29,8 +29,6 @@ def upsert_labels(self, label_ids, timeout_seconds=60):
29
29
if len (label_ids ) < 1 :
30
30
raise ValueError ("Must provide at least one label id" )
31
31
32
- sleep_time = 5
33
-
34
32
mutation_name = 'createMEAModelRunLabelRegistrationTask'
35
33
create_task_query_str = """mutation createMEAModelRunLabelRegistrationTaskPyApi($modelRunId: ID!, $labelIds : [ID!]!) {
36
34
%s(where : { id : $modelRunId}, data : {labelIds: $labelIds})}
@@ -46,18 +44,54 @@ def upsert_labels(self, label_ids, timeout_seconds=60):
46
44
MEALabelRegistrationTaskStatus(where: $where) {status errorMessage}
47
45
}
48
46
"""
47
+ return self ._wait_until_done (lambda : self .client .execute (
48
+ status_query_str , {'where' : {
49
+ 'id' : task_id
50
+ }})['MEALabelRegistrationTaskStatus' ],
51
+ timeout_seconds = timeout_seconds )
52
+
53
+ def upsert_data_rows (self , data_row_ids , timeout_seconds = 60 ):
54
+ """ Adds data rows to a model run without any associated labels
55
+ Args:
56
+ data_row_ids (list): data row ids to add to mea
57
+ timeout_seconds (float): Max waiting time, in seconds.
58
+ Returns:
59
+ ID of newly generated async task
60
+ """
61
+
62
+ if len (data_row_ids ) < 1 :
63
+ raise ValueError ("Must provide at least one data row id" )
64
+
65
+ mutation_name = 'createMEAModelRunDataRowRegistrationTask'
66
+ create_task_query_str = """mutation createMEAModelRunDataRowRegistrationTaskPyApi($modelRunId: ID!, $dataRowIds : [ID!]!) {
67
+ %s(where : { id : $modelRunId}, data : {dataRowIds: $dataRowIds})}
68
+ """ % (mutation_name )
49
69
70
+ res = self .client .execute (create_task_query_str , {
71
+ 'modelRunId' : self .uid ,
72
+ 'dataRowIds' : data_row_ids
73
+ })
74
+ task_id = res [mutation_name ]
75
+
76
+ status_query_str = """query MEADataRowRegistrationTaskStatusPyApi($where: WhereUniqueIdInput!){
77
+ MEADataRowRegistrationTaskStatus(where: $where) {status errorMessage}
78
+ }
79
+ """
80
+ return self ._wait_until_done (lambda : self .client .execute (
81
+ status_query_str , {'where' : {
82
+ 'id' : task_id
83
+ }})['MEADataRowRegistrationTaskStatus' ],
84
+ timeout_seconds = timeout_seconds )
85
+
86
+ def _wait_until_done (self , status_fn , timeout_seconds = 60 , sleep_time = 5 ):
87
+ # Do not use this function outside of the scope of upsert_data_rows or upsert_labels. It could change.
50
88
while True :
51
- res = self .client .execute (status_query_str ,
52
- {'where' : {
53
- 'id' : task_id
54
- }})['MEALabelRegistrationTaskStatus' ]
89
+ res = status_fn ()
55
90
if res ['status' ] == 'COMPLETE' :
56
- return res
91
+ return True
57
92
elif res ['status' ] == 'FAILED' :
58
93
raise Exception (
59
- f"MEA Label Import Failed. Details : { res ['errorMessage' ]} " )
60
-
94
+ f"MEA Import Failed. Details : { res ['errorMessage' ]} " )
61
95
timeout_seconds -= sleep_time
62
96
if timeout_seconds <= 0 :
63
97
raise TimeoutError (
0 commit comments