Skip to content

Commit

Permalink
Fixed display of jobs on Queue page. Added rule information to Queue …
Browse files Browse the repository at this point in the history
…page.
  • Loading branch information
tblock79 committed Aug 20, 2024
1 parent c6fbf0d commit 36c828f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 21 deletions.
69 changes: 51 additions & 18 deletions bookkeeping/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions webinterface/templates/queue.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ <h5 class="title is-5 configtitle" style="margin-top: 60px;"><i
<th>ACC</th>
<th>MRN</th>
<th>Scope</th>
<th>Rule</th>
<th>Time</th>
<th>ID</th>
</tr>
Expand Down Expand Up @@ -877,7 +878,7 @@ <h5 class="title is-5 configtitle" style="margin-top: 60px;"><i
"scrollY": "740px",
"scrollX": false,
"scrollCollapse": true,
"order": [[3, 'desc']],
"order": [[4, 'desc'], [5, 'desc']],
initComplete: function() {
$('.queueArchiveFilter > .dt-down-arrow').hide()
$('.queueArchiveFilter > .icon').hide()
Expand Down Expand Up @@ -963,7 +964,7 @@ <h5 class="title is-5 configtitle" style="margin-top: 60px;"><i
var jobtable = $('#jobs_archive').DataTable();
if (Object.keys(data).length) {
Object.keys(data).forEach(function (key) {
jobtable.row.add( [ data[key]["ACC"], data[key]["MRN"], data[key]["Scope"], data[key]["Time"], key ] );
jobtable.row.add( [ data[key]["ACC"], data[key]["MRN"], data[key]["Scope"], data[key]["Rule"], data[key]["Time"], key ] );
})
}
$('#jobs_archive').DataTable().button(1).enable(false);
Expand Down Expand Up @@ -995,6 +996,7 @@ <h5 class="title is-5 configtitle" style="margin-top: 60px;"><i
dataType: 'json',
error: function () {
// $('#erroralert').show();
$("#process_logs_display").text("Unable to load processing logs for the selected task.")
},
success: function (data) {
if (data && data.length > 0) {
Expand Down Expand Up @@ -1028,6 +1030,7 @@ <h5 class="title is-5 configtitle" style="margin-top: 60px;"><i
dataType: 'json',
error: function () {
// $('#erroralert').show();
$("#process_results_display").text("Unable to load processing results for the selected task.")
},
success: function (data) {
if (data && data.length > 0) {
Expand All @@ -1053,6 +1056,8 @@ <h5 class="title is-5 configtitle" style="margin-top: 60px;"><i
return;
}

console.log("Showing audit trail for job " + jobId);

$("#jobaudittrail").addClass("is-active");
$("#jobaudittrail").addClass("is-loading");

Expand All @@ -1063,9 +1068,13 @@ <h5 class="title is-5 configtitle" style="margin-top: 60px;"><i
dataType: 'json',
error: function () {
// $('#erroralert').show();
var eventstable = $('#job-events-table').DataTable();
eventstable.clear();
eventstable.draw();
},
success: function (data) {
var eventstable = $('#job-events-table').DataTable().clear();
var eventstable = $('#job-events-table').DataTable();
eventstable.clear();
Object.keys(data).forEach(function (key) {
eventstable.row.add([ data[key]["task_id"].slice(0,8) || "", data[key]["local_time"], data[key]["event"], data[key]["sender"], data[key]["target"], data[key]["file_count"], data[key]["info"] ])
})
Expand Down

0 comments on commit 36c828f

Please sign in to comment.