From bdee3d9ba40cacecf5bbe3b72b75a2b957f76998 Mon Sep 17 00:00:00 2001 From: ric-evans Date: Wed, 29 Jan 2025 15:43:12 -0600 Subject: [PATCH] continue redefining what finished/completed scan is - final --- README.md | 4 ++-- skydriver/rest_handlers.py | 13 ++++++------- skydriver/utils.py | 23 +++++++++++++---------- tests/unit/test_scan_state.py | 19 ++++++++----------- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 088271b4..822b0a8d 100644 --- a/README.md +++ b/README.md @@ -325,8 +325,8 @@ None There are several codes for `scan_state`: -- Successful state - * `SCAN_FINISHED_SUCCESSFULLY` +- Successful state (completed scan) + * `SCAN_HAS_FINAL_RESULT` - Non-finished scan states (in reverse order of occurrence) * `IN_PROGRESS__PARTIAL_RESULT_GENERATED` * `IN_PROGRESS__WAITING_ON_FIRST_PIXEL_RECO` diff --git a/skydriver/rest_handlers.py b/skydriver/rest_handlers.py index 190c85a1..91014e01 100644 --- a/skydriver/rest_handlers.py +++ b/skydriver/rest_handlers.py @@ -42,7 +42,7 @@ from .ewms import request_stop_on_ewms from .k8s.scan_backlog import put_on_backlog from .k8s.scanner_instance import SkyScanK8sJobFactory -from .utils import get_scan_state +from .utils import does_scan_state_indicate_final_result_received, get_scan_state LOGGER = logging.getLogger(__name__) @@ -741,8 +741,9 @@ async def delete(self, scan_id: str) -> None: # check DB states manifest = await self.manifests.get(scan_id, True) - _, scan_complete = await get_scan_state(manifest, self.ewms_rc, self.results) - if scan_complete: + if does_scan_state_indicate_final_result_received( + await get_scan_state(manifest, self.ewms_rc, self.results) + ): msg = "Attempted to delete a completed scan (must use `delete_completed_scan=True`)" raise web.HTTPError( 400, @@ -1046,9 +1047,7 @@ async def get(self, scan_id: str) -> None: LOGGER.exception(e) # scan state - scan_state, scan_complete = await get_scan_state( - manifest, self.ewms_rc, self.results - ) + scan_state = await get_scan_state(manifest, self.ewms_rc, self.results) # ewms if ( @@ -1065,7 +1064,7 @@ async def get(self, scan_id: str) -> None: resp = { "scan_state": scan_state, "is_deleted": manifest.is_deleted, - "scan_complete": scan_complete, + "scan_complete": does_scan_state_indicate_final_result_received(scan_state), "pods": pods_411, "clusters": clusters, } diff --git a/skydriver/utils.py b/skydriver/utils.py index ec3900e1..6e43f095 100644 --- a/skydriver/utils.py +++ b/skydriver/utils.py @@ -11,7 +11,7 @@ class _ScanState(enum.Enum): """A non-persisted scan state.""" - SCAN_FINISHED_SUCCESSFULLY = enum.auto() + SCAN_HAS_FINAL_RESULT = enum.auto() # ^^^ indicates the scanner sent finished results. in reality, the scanner or ewms # could've crashed immediately after BUT the user only cares about the RESULTS--so, # this would still be considered a SUCCESS in *this* context @@ -23,20 +23,23 @@ class _ScanState(enum.Enum): PENDING__PRESTARTUP = enum.auto() +def does_scan_state_indicate_final_result_received(state: str) -> bool: + """Does the scan state indicate has result?""" + return state == _ScanState.SCAN_HAS_FINAL_RESULT.value + + async def get_scan_state( manifest: Manifest, ewms_rc: RestClient, results: database.interface.ResultClient, -) -> tuple[str, bool]: +) -> str: """Determine the state of the scan by parsing attributes and talking with EWMS. - Returns tuple: - 1. the state as a human-readable string - 2. a bool for whether the scan was successful or not + Returns the state as a human-readable string """ if (await results.get(manifest.scan_id)).is_final: - # NOTE: see note on 'SCAN_FINISHED_SUCCESSFULLY' above - return _ScanState.SCAN_FINISHED_SUCCESSFULLY.name, True + # NOTE: see note on 'SCAN_HAS_FINAL_RESULT' above + return _ScanState.SCAN_HAS_FINAL_RESULT.name def _has_cleared_backlog() -> bool: return bool( @@ -80,11 +83,11 @@ def get_nonfinished_state() -> _ScanState: and manifest.ewms_task.get("complete") ): # we didn't have info on what kind of stop - return f"STOPPED__{state.split('__')[1]}", False + return f"STOPPED__{state.split('__')[1]}" # has EWMS ceased running the scan workers? elif dtype := await ewms.get_deactivated_type(ewms_rc, manifest.ewms_workflow_id): # -> yes, the ewms workflow has been deactivated - return f"{dtype.upper()}__{state.split('__')[1]}", False + return f"{dtype.upper()}__{state.split('__')[1]}" else: # -> no, this is a non-finished scan - return state, False + return state diff --git a/tests/unit/test_scan_state.py b/tests/unit/test_scan_state.py index d38b445e..e154247a 100644 --- a/tests/unit/test_scan_state.py +++ b/tests/unit/test_scan_state.py @@ -13,12 +13,12 @@ "processing_stats_is_finished", [True, False], ) -async def test_00__scan_finished_successfully( +async def test_00__scan_has_final_result( processing_stats_is_finished: bool, ) -> None: - """Test with SCAN_FINISHED_SUCCESSFULLY. + """Test with SCAN_HAS_FINAL_RESULT. - `processing_stats.is_finished` does not affect "SCAN_FINISHED_SUCCESSFULLY" + `processing_stats.is_finished` does not affect "SCAN_HAS_FINAL_RESULT" """ ewms_rc = MagicMock() results = MagicMock(get=AsyncMock(return_value=MagicMock(is_final=True))) @@ -41,10 +41,7 @@ async def test_00__scan_finished_successfully( ), ) - assert await get_scan_state(manifest, ewms_rc, results) == ( - "SCAN_FINISHED_SUCCESSFULLY", - True, - ) + assert await get_scan_state(manifest, ewms_rc, results) == "SCAN_HAS_FINAL_RESULT" @pytest.mark.parametrize( @@ -83,7 +80,7 @@ async def test_10__partial_result_generated(ewms_dtype: str | None, state: str) ) with patch("skydriver.ewms.get_deactivated_type", return_value=ewms_dtype): - assert await get_scan_state(manifest, ewms_rc, results) == (state, False) + assert await get_scan_state(manifest, ewms_rc, results) == state @pytest.mark.parametrize( @@ -124,7 +121,7 @@ async def test_20__waiting_on_first_pixel_reco( ) with patch("skydriver.ewms.get_deactivated_type", return_value=ewms_dtype): - assert await get_scan_state(manifest, ewms_rc, results) == (state, False) + assert await get_scan_state(manifest, ewms_rc, results) == state @pytest.mark.parametrize( @@ -155,7 +152,7 @@ async def test_40__waiting_on_scanner_server_startup( ) with patch("skydriver.ewms.get_deactivated_type", return_value=ewms_dtype): - assert await get_scan_state(manifest, ewms_rc, results) == (state, False) + assert await get_scan_state(manifest, ewms_rc, results) == state @pytest.mark.parametrize( @@ -184,4 +181,4 @@ async def test_50__prestartup(ewms_dtype: str | None, state: str) -> None: ) with patch("skydriver.ewms.get_deactivated_type", return_value=ewms_dtype): - assert await get_scan_state(manifest, ewms_rc, results) == (state, False) + assert await get_scan_state(manifest, ewms_rc, results) == state