diff --git a/bookkeeping/query.py b/bookkeeping/query.py index 02957be..4ab11c8 100644 --- a/bookkeeping/query.py +++ b/bookkeeping/query.py @@ -116,10 +116,13 @@ async def get_task_events(request) -> JSONResponse: subtask_query = sqlalchemy.select(tasks_table.c.id).where(tasks_table.c.parent_id == task_id) # Note: The space at the end is needed for the case that there are no subtasks - subtask_ids_str = "( " + subtask_ids_str = "" for row in await database.fetch_all(subtask_query): subtask_ids_str += f"'{row[0]}'," - subtask_ids_str = subtask_ids_str[:-1] + ")" + + subtask_ids_filter = "" + if subtask_ids_str: + subtask_ids_filter = "or task_events.task_id in (" + subtask_ids_str[:-1] + ")" # Get all the task_events from task `task_id` or any of its subtasks # subtask_ids = [row[0] for row in await database.fetch_all(subtask_query)] @@ -128,12 +131,13 @@ async def get_task_events(request) -> JSONResponse: # .order_by(task_events.c.task_id, task_events.c.time) # .where(sqlalchemy.or_(task_events.c.task_id == task_id, task_events.c.task_id.in_(subtask_ids))) # ) - query = sqlalchemy.text( - f"""select *, time {tz_conversion} as local_time from task_events - where task_events.task_id = '{task_id}' or task_events.task_id in {subtask_ids_str} + + query_string = f"""select *, time {tz_conversion} as local_time from task_events + where task_events.task_id = '{task_id}' {subtask_ids_filter} order by task_events.task_id, task_events.time """ - ) + #print("SQL Query = " + query_string) + query = sqlalchemy.text(query_string) results = await database.fetch_all(query) return CustomJSONResponse(results) @@ -201,18 +205,25 @@ async def find_task(request) -> JSONResponse: if study_filter=="true": study_filter_term = "and tasks.study_uid is not null" - query = sqlalchemy.text( - f""" select tasks.id as task_id, - tag_accessionnumber as acc, - tag_patientid as mrn, - data->'info'->>'uid_type' as scope, - tasks.time::timestamp {tz_conversion} as time - from tasks - left join dicom_series on dicom_series.series_uid = tasks.series_uid - where parent_id is null {filter_term} {study_filter_term} - order by date_trunc('second', tasks.time) desc, tasks.id desc - limit 256 """ - ) + query_string = f"""select max(a.acc) as acc, max(a.mrn) as mrn, max(a.task_id) as task_id, max(a.scope) as scope, max(a.time) as time, + string_agg(b.data->'info'->>'applied_rule', ', ' order by b.id) as rule, + string_agg(b.data->'info'->>'triggered_rules', ',' order by b.id) as triggered_rules + from (select tasks.id as task_id, + tag_accessionnumber as acc, + tag_patientid as mrn, + data->'info'->>'uid_type' as scope, + tasks.time::timestamp {tz_conversion} as time + from tasks + left join dicom_series on dicom_series.series_uid = tasks.series_uid + where parent_id is null {filter_term} {study_filter_term} + order by date_trunc('second', tasks.time) desc, tasks.id desc + limit 512) a + left join tasks b on (b.parent_id = a.task_id or b.id = a.task_id) + group by a.task_id + order by max(a.time) desc + """ + #print(query_string) + query = sqlalchemy.text(query_string) response: Dict = {} result_rows = await database.fetch_all(query) @@ -229,11 +240,33 @@ async def find_task(request) -> JSONResponse: else: job_scope = "SERIES" + if item["rule"]: + item["rule"] = item["rule"].strip() + if item["rule"] == ",": + item["rule"] = "" + + rule_information = "" + if item["rule"]: + rule_information = item["rule"] + else: + if item["triggered_rules"]: + try: + json_data = json.loads("["+item["triggered_rules"]+"]") + for entry in json_data: + rule_information += ", ".join(list(entry.keys())) + ", " + if rule_information: + rule_information = rule_information[:-2] + except json.JSONDecodeError: + rule_information = "ERROR" + else: + rule_information = "(Delegation)" + response[task_id] = { "ACC": acc, "MRN": mrn, "Scope": job_scope, "Time": time, + "Rule": rule_information, } return CustomJSONResponse(response) diff --git a/webinterface/templates/queue.html b/webinterface/templates/queue.html index b7ea3c0..f663f41 100755 --- a/webinterface/templates/queue.html +++ b/webinterface/templates/queue.html @@ -208,6 +208,7 @@