Skip to content

Commit

Permalink
refactor(submitters) (#3574) (patch)
Browse files Browse the repository at this point in the history
# Description

refactoring of order submitters.
  • Loading branch information
ChrOertlin authored Aug 19, 2024
1 parent f9bd288 commit feb7768
Show file tree
Hide file tree
Showing 52 changed files with 1,929 additions and 1,476 deletions.
52 changes: 11 additions & 41 deletions cg/meta/orders/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,36 @@

from cg.apps.lims import LimsAPI
from cg.apps.osticket import OsTicket
from cg.meta.orders.balsamic_qc_submitter import BalsamicQCSubmitter
from cg.meta.orders.balsamic_submitter import BalsamicSubmitter
from cg.meta.orders.balsamic_umi_submitter import BalsamicUmiSubmitter
from cg.meta.orders.fastq_submitter import FastqSubmitter
from cg.meta.orders.fluffy_submitter import FluffySubmitter
from cg.meta.orders.metagenome_submitter import MetagenomeSubmitter
from cg.meta.orders.microsalt_submitter import MicrosaltSubmitter
from cg.meta.orders.mip_dna_submitter import MipDnaSubmitter
from cg.meta.orders.mip_rna_submitter import MipRnaSubmitter
from cg.meta.orders.rml_submitter import RmlSubmitter
from cg.meta.orders.rnafusion_submitter import RnafusionSubmitter
from cg.meta.orders.sars_cov_2_submitter import SarsCov2Submitter
from cg.meta.orders.submitter import Submitter
from cg.meta.orders.ticket_handler import TicketHandler
from cg.meta.orders.tomte_submitter import TomteSubmitter
from cg.models.orders.order import OrderIn, OrderType
from cg.services.orders.submitters.order_submitter_registry import OrderSubmitterRegistry
from cg.store.store import Store

LOG = logging.getLogger(__name__)


def _get_submit_handler(project: OrderType, lims: LimsAPI, status: Store) -> Submitter:
"""Factory Method"""

submitters = {
OrderType.BALSAMIC: BalsamicSubmitter,
OrderType.BALSAMIC_QC: BalsamicQCSubmitter,
OrderType.BALSAMIC_UMI: BalsamicUmiSubmitter,
OrderType.FASTQ: FastqSubmitter,
OrderType.FLUFFY: FluffySubmitter,
OrderType.METAGENOME: MetagenomeSubmitter,
OrderType.MICROSALT: MicrosaltSubmitter,
OrderType.MIP_DNA: MipDnaSubmitter,
OrderType.MIP_RNA: MipRnaSubmitter,
OrderType.RML: RmlSubmitter,
OrderType.RNAFUSION: RnafusionSubmitter,
OrderType.SARS_COV_2: SarsCov2Submitter,
OrderType.TOMTE: TomteSubmitter,
}
if project in submitters:
return submitters[project](lims=lims, status=status)


class OrdersAPI:
"""Orders API for accepting new samples into the system."""

def __init__(self, lims: LimsAPI, status: Store, osticket: OsTicket):
def __init__(
self,
lims: LimsAPI,
status: Store,
osticket: OsTicket,
submitter_registry: OrderSubmitterRegistry,
):
super().__init__()
self.lims = lims
self.status = status
self.ticket_handler: TicketHandler = TicketHandler(osticket_api=osticket, status_db=status)
self.submitter_registry = submitter_registry

def submit(self, project: OrderType, order_in: OrderIn, user_name: str, user_mail: str) -> dict:
"""Submit a batch of samples.
Main entry point for the class towards interfaces that implements it.
"""
submit_handler: Submitter = _get_submit_handler(project, lims=self.lims, status=self.status)
submit_handler.validate_order(order=order_in)

submit_handler = self.submitter_registry.get_order_submitter(project)
# detect manual ticket assignment
ticket_number: str | None = TicketHandler.parse_ticket_number(order_in.name)
if not ticket_number:
Expand All @@ -86,4 +56,4 @@ def submit(self, project: OrderType, order_in: OrderIn, user_name: str, user_mai
ticket_number=ticket_number,
)
order_in.ticket = ticket_number
return submit_handler.submit_order(order=order_in)
return submit_handler.submit_order(order_in=order_in)
5 changes: 0 additions & 5 deletions cg/meta/orders/balsamic_qc_submitter.py

This file was deleted.

5 changes: 0 additions & 5 deletions cg/meta/orders/balsamic_submitter.py

This file was deleted.

5 changes: 0 additions & 5 deletions cg/meta/orders/balsamic_umi_submitter.py

This file was deleted.

5 changes: 0 additions & 5 deletions cg/meta/orders/fluffy_submitter.py

This file was deleted.

32 changes: 0 additions & 32 deletions cg/meta/orders/lims.py

This file was deleted.

5 changes: 0 additions & 5 deletions cg/meta/orders/microsalt_submitter.py

This file was deleted.

5 changes: 0 additions & 5 deletions cg/meta/orders/mip_dna_submitter.py

This file was deleted.

5 changes: 0 additions & 5 deletions cg/meta/orders/mip_rna_submitter.py

This file was deleted.

5 changes: 0 additions & 5 deletions cg/meta/orders/rml_submitter.py

This file was deleted.

16 changes: 0 additions & 16 deletions cg/meta/orders/rnafusion_submitter.py

This file was deleted.

51 changes: 0 additions & 51 deletions cg/meta/orders/submitter.py

This file was deleted.

5 changes: 0 additions & 5 deletions cg/meta/orders/tomte_submitter.py

This file was deleted.

5 changes: 4 additions & 1 deletion cg/server/endpoints/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
lims,
order_service,
osticket,
order_submitter_registry,
)
from cg.store.models import (
Application,
Expand Down Expand Up @@ -155,7 +156,9 @@ def create_order_from_form():
@ORDERS_BLUEPRINT.route("/submit_order/<order_type>", methods=["POST"])
def submit_order(order_type):
"""Submit an order for samples."""
api = OrdersAPI(lims=lims, status=db, osticket=osticket)
api = OrdersAPI(
lims=lims, status=db, osticket=osticket, submitter_registry=order_submitter_registry
)
error_message: str
try:
request_json = request.get_json()
Expand Down
7 changes: 7 additions & 0 deletions cg/server/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from cg.services.orders.order_summary_service.order_summary_service import (
OrderSummaryService,
)
from cg.services.orders.submitters.order_submitter_registry import (
OrderSubmitterRegistry,
setup_order_submitter_registry,
)
from cg.services.sample_service.sample_service import SampleService
from cg.store.database import initialize_database
from cg.store.store import Store
Expand Down Expand Up @@ -83,3 +87,6 @@ def init_app(self, app):
summary_service = OrderSummaryService(store=db, analysis_client=analysis_client)
order_service = OrderService(store=db, status_service=summary_service)
sample_service = SampleService(db)
order_submitter_registry: OrderSubmitterRegistry = setup_order_submitter_registry(
lims=lims, status_db=db
)
39 changes: 39 additions & 0 deletions cg/services/orders/order_lims_service/order_lims_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import logging

from cg.apps.lims import LimsAPI
from cg.models.lims.sample import LimsSample
from cg.models.orders.order import OrderIn
from cg.models.orders.samples import OrderInSample

LOG = logging.getLogger(__name__)


class OrderLimsService:

def __init__(self, lims_api: LimsAPI):
self.lims_api = lims_api

@staticmethod
def _build_lims_sample(customer: str, samples: list[OrderInSample]) -> list[LimsSample]:
"""Convert order input to lims interface input."""
samples_lims = []
for sample in samples:
dict_sample = sample.__dict__
LOG.debug(f"{sample.name}: prepare LIMS input")
dict_sample["customer"] = customer
lims_sample: LimsSample = LimsSample.parse_obj(dict_sample)
samples_lims.append(lims_sample)
return samples_lims

def process_lims(self, lims_order: OrderIn, new_samples: list[OrderInSample]):
"""Process samples to add them to LIMS."""
samples_lims: list[LimsSample] = self._build_lims_sample(
lims_order.customer, samples=new_samples
)
project_name = lims_order.ticket or lims_order.name
# Create new lims project
project_data = self.lims_api.submit_project(
project_name, [lims_sample.dict() for lims_sample in samples_lims]
)
lims_map = self.lims_api.get_samples(projectlimsid=project_data["id"], map_ids=True)
return project_data, lims_map
Loading

0 comments on commit feb7768

Please sign in to comment.