Skip to content

Commit

Permalink
add cli command for status update
Browse files Browse the repository at this point in the history
  • Loading branch information
fevac committed Jun 22, 2023
1 parent 487c1e1 commit c43e099
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
52 changes: 51 additions & 1 deletion tests/cli/test_cli_core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import pytest

import trailblazer
from trailblazer.cli.core import base, cancel, delete, ls_cmd, scan, set_analysis_completed, user
from trailblazer.cli.core import (
base,
cancel,
delete,
ls_cmd,
scan,
set_analysis_completed,
set_analysis_status,
user,
)


def test_base(cli_runner):
Expand Down Expand Up @@ -36,6 +45,47 @@ def test_set_analysis_completed(cli_runner, trailblazer_context, caplog):
assert analysis_obj.status == "completed"


def test_set_analysis_status(cli_runner, trailblazer_context, caplog) -> None:
"""Test that the lastest analysis status is updated for a given case ID."""

# GIVEN an analysis with status FAILED
failed_analysis = "crackpanda"
analysis_obj = trailblazer_context["trailblazer"].get_latest_analysis(case_id=failed_analysis)

# Make sure status is not "completed"
assert analysis_obj.status != "qc"

# WHEN running command
result = cli_runner.invoke(
set_analysis_status, ["--status", "qc", failed_analysis], obj=trailblazer_context
)

# THEN command runs successfully
assert result.exit_code == 0

# THEN status will be set to COMPLETED
analysis_obj = trailblazer_context["trailblazer"].get_latest_analysis(case_id=failed_analysis)
assert analysis_obj.status == "qc"


def test_set_analysis_status_error(cli_runner, trailblazer_context, caplog) -> None:
"""Test that setting the status to a non-accepted value raises an error."""

# GIVEN an analysis with status FAILED
failed_analysis = "crackpanda"

# WHEN running command
result = cli_runner.invoke(
set_analysis_status,
["--status", "non_existing_status", failed_analysis],
obj=trailblazer_context,
)

# THEN an error should be raised
assert result.exit_code != 0
assert "Invalid status" in caplog.text


def test_cancel_nonexistent(cli_runner, trailblazer_context, caplog):
with caplog.at_level("ERROR"):
# GIVEN Trailblazer database with analyses and jobs
Expand Down
24 changes: 24 additions & 0 deletions trailblazer/cli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,30 @@ def set_analysis_completed(context, analysis_id):
LOG.error(e)


@base.command("set-status")
@click.option(
"--status",
is_flag=False,
type=str,
help=f"Status to be set. Can take the values:{STATUS_OPTIONS}",
)
@click.argument("case_id", type=str)
@click.pass_context
def set_analysis_status(
context,
case_id: str,
status: str,
):
"""Set the status of the latest analysis for a given CASE_ID."""
try:
context.obj["trailblazer"].set_analysis_status(case_id=case_id, status=status)
except ValueError as e:
LOG.error(e)
raise click.Abort from e
except Exception as e:
LOG.error(e)


@base.command()
@click.option("--force", is_flag=True, help="Force delete if analysis ongoing")
@click.option("--cancel-jobs", is_flag=True, help="Cancel all ongoing jobs before deleting")
Expand Down
13 changes: 9 additions & 4 deletions trailblazer/store/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
ONGOING_STATUSES,
SLURM_ACTIVE_CATEGORIES,
STARTED_STATUSES,
STATUS_OPTIONS,
FileFormat,
TrailblazerStatus,
WorkflowManager,
FileFormat,
)
from trailblazer.exc import EmptySqueueError, TowerRequirementsError, TrailblazerError
from trailblazer.io.controller import ReadFile
from trailblazer.store.models import Model, User, Analysis, Job, Info
from trailblazer.store.models import Analysis, Info, Job, Model, User
from trailblazer.store.utils import formatters

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -249,6 +250,7 @@ def mark_analyses_deleted(self, case_id: str) -> Query:
return old_analyses

def set_analysis_completed(self, analysis_id: int) -> None:
"""Set an analysis status to completed."""
analysis_obj = self.analysis(analysis_id=analysis_id)
analysis_obj.status = "completed"
self.commit()
Expand All @@ -262,10 +264,13 @@ def set_analysis_uploaded(self, case_id: str, uploaded_at: dt.datetime) -> None:

def set_analysis_status(self, case_id: str, status: str):
"""Setting analysis status."""
status = status.lower()
if status not in set(STATUS_OPTIONS):
raise ValueError(f"Invalid status. Allowed values are: {STATUS_OPTIONS}")
analysis_obj: Analysis = self.get_latest_analysis(case_id=case_id)
analysis_obj.status: str = status
self.commit()
LOG.info(f"{analysis_obj.family} - Status set to {status}")
LOG.info(f"{analysis_obj.family} - Status set to {status.upper()}")

def add_comment(self, case_id: str, comment: str):
analysis_obj: Analysis = self.get_latest_analysis(case_id=case_id)
Expand Down Expand Up @@ -494,7 +499,7 @@ def query_tower(config_file: str, case_id: str) -> TowerAPI:
Currently only one tower ID is supported."""
workflow_id: int = ReadFile.get_content_from_file(
file_format=FileFormat.YAML, file_path=Path(config_file)
).get(case_id)[0]
).get(case_id)[-1]
tower_api = TowerAPI(workflow_id=workflow_id)
if not tower_api.tower_client.meets_requirements:
raise TowerRequirementsError
Expand Down

0 comments on commit c43e099

Please sign in to comment.