-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't send images to imaging queue if they have been exported (#202)
* Create tables for image tracking * Create dummy testing for CLI-database interaction * Create dummy testing for CLI-database interaction * Filter message if image has been exported before * Filter pacs images that have been exported * Use context manager in test session Its a bit nicer * Add postgres config to end to end test * Finish comment 🤦 * Use url creator * Add headers * Add headers * Add headers last one? * Pass in project slug to filtering * Apply suggestions from code review Co-authored-by: Milan Malfait <m.malfait@ucl.ac.uk> * Use pixl schemata for tables * Ensure images rows are added to the database * Add nice representation for database entities * Ensure different extracts can have the same images * Remove old comment * Update documentation of database fixtures --------- Co-authored-by: ruaridhg <ruaridhg@users.noreply.github.com> Co-authored-by: Milan Malfait <m.malfait@ucl.ac.uk>
- Loading branch information
1 parent
38c5040
commit 4cb94ac
Showing
14 changed files
with
476 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright (c) University College London Hospitals NHS Foundation Trust | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Configuration of CLI from config file.""" | ||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
|
||
def _load_config(filename: str = "pixl_config.yml") -> dict: | ||
"""CLI configuration generated from a .yaml file""" | ||
if not Path(filename).exists(): | ||
msg = f"Failed to find {filename}. It must be present in the current working directory" | ||
raise FileNotFoundError(msg) | ||
|
||
with Path(filename).open() as config_file: | ||
config_dict = yaml.safe_load(config_file) | ||
return dict(config_dict) | ||
|
||
|
||
cli_config = _load_config() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Copyright (c) University College London Hospitals NHS Foundation Trust | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Interaction with the PIXL database.""" | ||
|
||
from core.database import Extract, Image | ||
from core.patient_queue.message import Message | ||
from sqlalchemy import URL, create_engine | ||
from sqlalchemy.orm import Session, sessionmaker | ||
|
||
from pixl_cli._config import cli_config | ||
|
||
connection_config = cli_config["postgres"] | ||
|
||
url = URL.create( | ||
drivername="postgresql+psycopg2", | ||
username=connection_config["username"], | ||
password=connection_config["password"], | ||
host=connection_config["host"], | ||
port=connection_config["port"], | ||
database=connection_config["database"], | ||
) | ||
|
||
engine = create_engine(url) | ||
|
||
|
||
def filter_exported_or_add_to_db(messages: list[Message], project_slug: str) -> list[Message]: | ||
""" | ||
Filter exported images for this project, and adds missing extract or images to database. | ||
:param messages: Initial messages to filter if they already exist | ||
:param project_slug: project slug to query on | ||
:return messages that have not been exported | ||
""" | ||
PixlSession = sessionmaker(engine) | ||
with PixlSession() as pixl_session, pixl_session.begin(): | ||
extract, extract_created = _get_or_create_project(project_slug, pixl_session) | ||
|
||
return _filter_exported_messages( | ||
extract, messages, pixl_session, extract_created=extract_created | ||
) | ||
|
||
|
||
def _get_or_create_project(project_slug: str, session: Session) -> tuple[Extract, bool]: | ||
existing_extract = session.query(Extract).filter(Extract.slug == project_slug).one_or_none() | ||
if existing_extract: | ||
return existing_extract, False | ||
new_extract = Extract(slug=project_slug) | ||
session.add(new_extract) | ||
return new_extract, True | ||
|
||
|
||
def _filter_exported_messages( | ||
extract: Extract, messages: list[Message], session: Session, *, extract_created: bool | ||
) -> list[Message]: | ||
output_messages = [] | ||
for message in messages: | ||
_, image_exported = _get_image_and_check_exported( | ||
extract, message, session, extract_created=extract_created | ||
) | ||
if not image_exported: | ||
output_messages.append(message) | ||
return output_messages | ||
|
||
|
||
def _get_image_and_check_exported( | ||
extract: Extract, message: Message, session: Session, *, extract_created: bool | ||
) -> tuple[Image, bool]: | ||
if extract_created: | ||
new_image = _add_new_image_to_session(extract, message, session) | ||
return new_image, False | ||
|
||
existing_image = ( | ||
session.query(Image) | ||
.filter( | ||
Image.extract == extract, | ||
Image.accession_number == message.accession_number, | ||
Image.mrn == message.mrn, | ||
Image.study_date == message.study_date, | ||
) | ||
.one_or_none() | ||
) | ||
|
||
if existing_image: | ||
if existing_image.exported_at is not None: | ||
return existing_image, True | ||
return existing_image, False | ||
|
||
new_image = _add_new_image_to_session(extract, message, session) | ||
return new_image, False | ||
|
||
|
||
def _add_new_image_to_session(extract: Extract, message: Message, session: Session) -> Image: | ||
new_image = Image( | ||
accession_number=message.accession_number, | ||
study_date=message.study_date, | ||
mrn=message.mrn, | ||
extract=extract, | ||
) | ||
session.add(new_image) | ||
return new_image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.