Skip to content

Commit 17f12d6

Browse files
committed
Support scheduled procedure time filters for find worklist items
1 parent e9b0fd1 commit 17f12d6

2 files changed

Lines changed: 101 additions & 13 deletions

File tree

src/services/storage.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ def find_worklist_items(
366366
self,
367367
modality: Optional[str] = None,
368368
scheduled_date: Optional[str] = None,
369+
scheduled_time: Optional[str] = None,
369370
patient_id: Optional[str] = None,
370371
) -> List[WorklistItem]:
371372
"""
@@ -374,6 +375,7 @@ def find_worklist_items(
374375
Args:
375376
modality: Filter by modality (e.g., "MG")
376377
scheduled_date: Filter by scheduled date (YYYYMMDD, or range like "20240101-20240131")
378+
scheduled_time: Filter by scheduled time (HHMMSS, or range like "080000-170000")
377379
patient_id: Filter by patient ID
378380
379381
Returns:
@@ -393,19 +395,14 @@ def find_worklist_items(
393395
params.append(modality)
394396

395397
if scheduled_date:
396-
if scheduled_date.endswith("-"):
397-
where_clauses.append("scheduled_date >= ?")
398-
params.append(scheduled_date[:-1].strip())
399-
elif scheduled_date.startswith("-"):
400-
where_clauses.append("scheduled_date <= ?")
401-
params.append(scheduled_date[1:].strip())
402-
elif "-" in scheduled_date:
403-
start_date, end_date = [s.strip() for s in scheduled_date.split("-", 1)]
404-
where_clauses.append("scheduled_date >= ? AND scheduled_date <= ?")
405-
params.extend([start_date, end_date])
406-
else:
407-
where_clauses.append("scheduled_date = ?")
408-
params.append(scheduled_date.strip())
398+
where_clause, clause_params = self.scheduled_query_clause("scheduled_date", scheduled_date)
399+
where_clauses.append(where_clause)
400+
params.extend(clause_params)
401+
402+
if scheduled_time:
403+
where_clause, clause_params = self.scheduled_query_clause("scheduled_time", scheduled_time)
404+
where_clauses.append(where_clause)
405+
params.extend(clause_params)
409406

410407
if patient_id:
411408
where_clauses.append("patient_id = ?")
@@ -421,6 +418,27 @@ def find_worklist_items(
421418

422419
return [WorklistItem(**row) for row in cursor.fetchall()]
423420

421+
def scheduled_query_clause(self, param_name: str, param_value: str) -> tuple[str, List[str]]:
422+
"""
423+
Helper to build SQL clause for scheduled date/time parameters.
424+
425+
Args:
426+
param_name: "scheduled_date" or "scheduled_time"
427+
param_value: Value to filter by (e.g., "20240101", "20240101-20240131", "-20240131", "20240101-")
428+
429+
Returns:
430+
Tuple of (SQL clause string, list of parameters)
431+
"""
432+
if param_value.endswith("-"):
433+
return f"{param_name} >= ?", [param_value[:-1].strip()]
434+
elif param_value.startswith("-"):
435+
return f"{param_name} <= ?", [param_value[1:].strip()]
436+
elif "-" in param_value:
437+
start, end = [s.strip() for s in param_value.split("-", 1)]
438+
return f"{param_name} >= ? AND {param_name} <= ?", [start, end]
439+
else:
440+
return f"{param_name} = ?", [param_value.strip()]
441+
424442
def get_worklist_item(self, accession_number: str) -> Optional[WorklistItem]:
425443
"""
426444
Get a single WorklistItem instance by accession number.

tests/services/test_storage.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,76 @@ def test_find_worklist_items_with_open_ended_date_range(self, mock_db, tmp_dir):
344344
["20240101"],
345345
)
346346

347+
def test_find_worklist_items_with_time_range(self, mock_db, tmp_dir):
348+
mock_cursor = MagicMock()
349+
mock_cursor.fetchall.return_value = []
350+
mock_connection = MagicMock()
351+
mock_connection.execute.return_value = mock_cursor
352+
mock_db.connect.return_value = mock_connection
353+
354+
subject = MWLStorage(tmp_dir)
355+
mock_connection.reset_mock()
356+
357+
subject.find_worklist_items(scheduled_time="090000 - 170000")
358+
359+
mock_connection.execute.assert_called_once_with(
360+
(
361+
"SELECT accession_number, modality, patient_birth_date, patient_id, "
362+
"patient_name, patient_sex, procedure_code, scheduled_date, scheduled_time, "
363+
"source_message_id, study_description, study_instance_uid, status, mpps_instance_uid "
364+
"FROM worklist_items WHERE scheduled_time >= ? AND scheduled_time <= ? "
365+
"ORDER BY scheduled_date, scheduled_time"
366+
),
367+
["090000", "170000"],
368+
)
369+
370+
def test_find_worklist_items_with_open_ended_time_range(self, mock_db, tmp_dir):
371+
mock_cursor = MagicMock()
372+
mock_cursor.fetchall.return_value = []
373+
mock_connection = MagicMock()
374+
mock_connection.execute.return_value = mock_cursor
375+
mock_db.connect.return_value = mock_connection
376+
377+
subject = MWLStorage(tmp_dir)
378+
mock_connection.reset_mock()
379+
380+
subject.find_worklist_items(scheduled_time="090000 -")
381+
382+
mock_connection.execute.assert_called_once_with(
383+
(
384+
"SELECT accession_number, modality, patient_birth_date, patient_id, "
385+
"patient_name, patient_sex, procedure_code, scheduled_date, scheduled_time, "
386+
"source_message_id, study_description, study_instance_uid, status, mpps_instance_uid "
387+
"FROM worklist_items WHERE scheduled_time >= ? "
388+
"ORDER BY scheduled_date, scheduled_time"
389+
),
390+
["090000"],
391+
)
392+
393+
def test_find_worklist_items_with_date_and_time_range(self, mock_db, tmp_dir):
394+
mock_cursor = MagicMock()
395+
mock_cursor.fetchall.return_value = []
396+
mock_connection = MagicMock()
397+
mock_connection.execute.return_value = mock_cursor
398+
mock_db.connect.return_value = mock_connection
399+
400+
subject = MWLStorage(tmp_dir)
401+
mock_connection.reset_mock()
402+
403+
subject.find_worklist_items(scheduled_date="20240101 - 20240131", scheduled_time="090000 - 170000")
404+
405+
mock_connection.execute.assert_called_once_with(
406+
(
407+
"SELECT accession_number, modality, patient_birth_date, patient_id, "
408+
"patient_name, patient_sex, procedure_code, scheduled_date, scheduled_time, "
409+
"source_message_id, study_description, study_instance_uid, status, mpps_instance_uid "
410+
"FROM worklist_items WHERE scheduled_date >= ? AND scheduled_date <= ? "
411+
"AND scheduled_time >= ? AND scheduled_time <= ? "
412+
"ORDER BY scheduled_date, scheduled_time"
413+
),
414+
["20240101", "20240131", "090000", "170000"],
415+
)
416+
347417
def test_get_worklist_item(self, mock_db, tmp_dir, result):
348418
mock_cursor = MagicMock()
349419
mock_cursor.fetchone.return_value = result

0 commit comments

Comments
 (0)