-
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.
Refactor flow cells endpoint (#3549)
- Loading branch information
Showing
14 changed files
with
110 additions
and
74 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
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,27 @@ | ||
from http import HTTPStatus | ||
import logging | ||
from functools import wraps | ||
from flask import jsonify | ||
|
||
from cg.store.exc import EntryNotFoundError | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
def handle_endpoint_errors(func): | ||
|
||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except EntryNotFoundError as error: | ||
LOG.error(error) | ||
return jsonify(error=str(error)), HTTPStatus.NOT_FOUND | ||
except Exception as error: | ||
LOG.error(f"Unexpected error in flow cells endpoint: {error}") | ||
return ( | ||
jsonify(error="An error occurred while processing your request."), | ||
HTTPStatus.INTERNAL_SERVER_ERROR, | ||
) | ||
|
||
return wrapper |
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,16 @@ | ||
from flask import Blueprint, jsonify | ||
|
||
from cg.server.endpoints.flow_cells.error_handler import handle_endpoint_errors | ||
from cg.server.endpoints.utils import before_request | ||
from cg.server.ext import flow_cell_service | ||
from cg.services.sample_run_metrics_service.dtos import SequencingMetrics | ||
|
||
FLOW_CELLS_BLUEPRINT = Blueprint("flowcells", __name__, url_prefix="/api/v1") | ||
FLOW_CELLS_BLUEPRINT.before_request(before_request) | ||
|
||
|
||
@FLOW_CELLS_BLUEPRINT.route("/flowcells/<flow_cell_name>/sequencing_metrics", methods=["GET"]) | ||
@handle_endpoint_errors | ||
def get_sequencing_metrics(flow_cell_name: str): | ||
metrics: list[SequencingMetrics] = flow_cell_service.get_metrics(flow_cell_name) | ||
return jsonify([metric.model_dump() for metric in metrics]) |
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
Empty file.
4 changes: 1 addition & 3 deletions
4
...ing_metrics/sequencing_metrics_request.py → ...rvices/sample_run_metrics_service/dtos.py
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
15 changes: 15 additions & 0 deletions
15
cg/services/sample_run_metrics_service/sample_run_metrics_service.py
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,15 @@ | ||
from cg.services.sample_run_metrics_service.dtos import SequencingMetrics | ||
from cg.services.sample_run_metrics_service.utils import create_metrics_dto | ||
from cg.store.models import IlluminaSequencingRun | ||
from cg.store.store import Store | ||
|
||
|
||
class SampleRunMetricsService: | ||
def __init__(self, store: Store): | ||
self.store = store | ||
|
||
def get_metrics(self, flow_cell_name: str) -> list[SequencingMetrics]: | ||
run: IlluminaSequencingRun = self.store.get_illumina_sequencing_run_by_device_internal_id( | ||
flow_cell_name | ||
) | ||
return create_metrics_dto(run.sample_metrics) if run else [] |
18 changes: 11 additions & 7 deletions
18
cg/server/utils.py → ...vices/sample_run_metrics_service/utils.py
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 was deleted.
Oops, something went wrong.
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,13 @@ | ||
import pytest | ||
|
||
from cg.services.sample_run_metrics_service.sample_run_metrics_service import ( | ||
SampleRunMetricsService, | ||
) | ||
from cg.store.store import Store | ||
|
||
|
||
@pytest.fixture | ||
def sample_run_metrics_service( | ||
store_with_illumina_sequencing_data: Store, | ||
) -> SampleRunMetricsService: | ||
return SampleRunMetricsService(store_with_illumina_sequencing_data) |
16 changes: 16 additions & 0 deletions
16
tests/services/sample_run_metrics_service/test_sample_metrics_service.py
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,16 @@ | ||
from cg.services.sample_run_metrics_service.sample_run_metrics_service import ( | ||
SampleRunMetricsService, | ||
) | ||
|
||
|
||
def test_get_metrics_for_flow_cell( | ||
sample_run_metrics_service: SampleRunMetricsService, | ||
novaseq_x_flow_cell_id: str, | ||
): | ||
# GIVEN an existing flow cell with sequencing metrics | ||
|
||
# WHEN fetching the metrics for the flow cell | ||
metrics = sample_run_metrics_service.get_metrics(novaseq_x_flow_cell_id) | ||
|
||
# THEN the metrics should be returned | ||
assert metrics |