-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add(CLI automatic delivery fastq workflow) (#3692)
# Description add cli command to deliver raw data automatically for analyses that require uploads
- Loading branch information
1 parent
c2efaa3
commit 6ceb0ae
Showing
12 changed files
with
257 additions
and
10 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
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,46 @@ | ||
import logging | ||
from datetime import datetime | ||
from pathlib import Path | ||
|
||
from cg.constants import Workflow | ||
from cg.services.file_delivery.deliver_files_service.deliver_files_service import ( | ||
DeliverFilesService, | ||
) | ||
from cg.services.file_delivery.deliver_files_service.deliver_files_service_factory import ( | ||
DeliveryServiceFactory, | ||
) | ||
from cg.store.models import Case, Analysis | ||
from cg.store.store import Store | ||
|
||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
def deliver_raw_data_for_analyses( | ||
analyses: list[Analysis], | ||
status_db: Store, | ||
delivery_path: Path, | ||
service_builder: DeliveryServiceFactory, | ||
dry_run: bool, | ||
): | ||
"""Deliver raw data for a list of analyses""" | ||
for analysis in analyses: | ||
try: | ||
case: Case = analysis.case | ||
delivery_service: DeliverFilesService = service_builder.build_delivery_service( | ||
delivery_type=case.data_delivery, | ||
workflow=Workflow.FASTQ, | ||
) | ||
|
||
delivery_service.deliver_files_for_case( | ||
case=case, delivery_base_path=delivery_path, dry_run=dry_run | ||
) | ||
status_db.update_analysis_upload_started_at( | ||
analysis_id=analysis.id, upload_started_at=datetime.now() | ||
) | ||
except Exception as error: | ||
status_db.update_analysis_upload_started_at( | ||
analysis_id=analysis.id, upload_started_at=None | ||
) | ||
LOG.error(f"Could not deliver files for analysis {analysis.id}: {error}") | ||
continue |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,45 @@ | ||
from datetime import datetime | ||
|
||
from cg.apps.tb.api import TrailblazerAPI | ||
from cg.apps.tb.models import TrailblazerAnalysis | ||
from cg.constants import Workflow | ||
from cg.store.models import Case, Analysis | ||
from cg.store.store import Store | ||
|
||
|
||
class AnalysisService: | ||
def __init__(self, analysis_client: TrailblazerAPI): | ||
def __init__(self, analysis_client: TrailblazerAPI, status_db: Store): | ||
self.status_db = status_db | ||
self.analysis_client = analysis_client | ||
|
||
def add_upload_job(self, case_id: str, slurm_id: int): | ||
analysis: TrailblazerAnalysis = self.analysis_client.get_latest_completed_analysis(case_id) | ||
self.analysis_client.add_upload_job_to_analysis(slurm_id=slurm_id, analysis_id=analysis.id) | ||
|
||
def get_analyses_to_upload_for_workflow(self, workflow: Workflow) -> list[Analysis]: | ||
""" | ||
Get all analyses that should be uploaded for a specific workflow. | ||
1. Get all analyses that should be uploaded for a specific workflow | ||
2. Check if the analysis has been uploaded and update the uploaded_at field | ||
""" | ||
analysis_to_upload: list[Analysis] = [] | ||
analyses: list[Analysis] = self.status_db.get_analyses_to_upload(workflow=workflow) | ||
for analysis in analyses: | ||
if self._has_uploaded_started(analysis) and self._is_analysis_completed(analysis): | ||
self.status_db.update_analysis_uploaded_at( | ||
analysis_id=analysis.id, uploaded_at=datetime.now() | ||
) | ||
if not self._is_analysis_uploaded(analysis) and self._is_analysis_completed(analysis): | ||
analysis_to_upload.append(analysis) | ||
return analysis_to_upload | ||
|
||
@staticmethod | ||
def _is_analysis_uploaded(analysis: Analysis) -> bool: | ||
return bool(analysis.uploaded_at) | ||
|
||
def _is_analysis_completed(self, analysis: Analysis) -> bool: | ||
return self.analysis_client.is_latest_analysis_completed(case_id=analysis.case.internal_id) | ||
|
||
@staticmethod | ||
def _has_uploaded_started(analysis: Analysis) -> bool: | ||
return bool(analysis.upload_started_at) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from datetime import datetime | ||
from unittest.mock import Mock | ||
|
||
from cg.services.analysis_service.analysis_service import AnalysisService | ||
|
||
from cg.constants import Workflow | ||
from cg.store.models import Analysis | ||
from cg.store.store import Store | ||
from tests.store_helpers import StoreHelpers | ||
|
||
|
||
def test_get_analyses_to_upload_for_workflow( | ||
helpers: StoreHelpers, base_store: Store, timestamp_now: datetime | ||
): | ||
# GIVEN an analysis service and a store with analyses to upload and not to upload | ||
analysis_upload: Analysis = helpers.add_analysis( | ||
store=base_store, | ||
uploaded_at=None, | ||
upload_started=None, | ||
workflow=Workflow.FASTQ, | ||
completed_at=timestamp_now, | ||
) | ||
new_case = helpers.add_case(store=base_store, name="no_upload_case") | ||
analysis_no_upload: Analysis = helpers.add_analysis( | ||
store=base_store, | ||
case=new_case, | ||
uploaded_at=timestamp_now, | ||
upload_started=None, | ||
workflow=Workflow.FASTQ, | ||
completed_at=timestamp_now, | ||
) | ||
analysis_service = AnalysisService(analysis_client=Mock(), status_db=base_store) | ||
|
||
# WHEN getting analyses to upload | ||
analyses: list[Analysis] = analysis_service.get_analyses_to_upload_for_workflow( | ||
workflow=Workflow.FASTQ | ||
) | ||
|
||
# THEN only the analyses to upload should be returned | ||
assert analyses == [analysis_upload] | ||
assert analysis_no_upload not in analyses |
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