From 5ba2a5c607ad82c504904187247dc20f78fccab3 Mon Sep 17 00:00:00 2001 From: Cizz22 Date: Mon, 2 Feb 2026 17:25:28 +0700 Subject: [PATCH] refactor: Improve Temporal simulation status validation and error handling in the router, and remove unused imports from the overhaul scope model. --- src/calculation_target_reliability/router.py | 42 +++++++++++++------- src/overhaul_scope/model.py | 3 +- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/calculation_target_reliability/router.py b/src/calculation_target_reliability/router.py index 951b56d..813a30b 100644 --- a/src/calculation_target_reliability/router.py +++ b/src/calculation_target_reliability/router.py @@ -46,10 +46,10 @@ async def get_target_reliability( # cut_hours = Query(0) ): """Get all scope pagination.""" - oh_session_id = params.oh_session_id, - eaf_input = params.eaf_input, - duration = params.duration, - simulation_id = params.simulation_id, + oh_session_id = params.oh_session_id + eaf_input = params.eaf_input + duration = params.duration + simulation_id = params.simulation_id cut_hours = params.cut_hours if not oh_session_id: @@ -68,20 +68,34 @@ async def get_target_reliability( if duration != 17520: if not simulation_id: raise HTTPException( - status_code=status.HTTP_425_TOO_EARLY, # or 409 Conflict - detail="Simulation still running. Please wait.", + status_code=status.HTTP_400_BAD_REQUEST, + detail="Simulation ID is required for non-default duration. Please run simulation first.", ) else: - temporal_client = await Client.connect(TEMPORAL_URL) - handle = temporal_client.get_workflow_handle(f"simulation-{simulation_id}") - desc = await handle.describe() - status_name = desc.status.name + try: + temporal_client = await Client.connect(TEMPORAL_URL) + handle = temporal_client.get_workflow_handle(f"simulation-{simulation_id}") + desc = await handle.describe() + status_name = desc.status.name - if status_name in ["RUNNING", "CONTINUED_AS_NEW"]: + if status_name in ["RUNNING", "CONTINUED_AS_NEW"]: + raise HTTPException( + status_code=status.HTTP_425_TOO_EARLY, + detail="Simulation is still running.", + ) + elif status_name != "COMPLETED": + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=f"Simulation failed with status: {status_name}", + ) + except HTTPException: + raise + except Exception as e: + # Handle connection errors or invalid workflow IDs raise HTTPException( - status_code=status.HTTP_425_TOO_EARLY, # or 409 Conflict - detail="Simulation still running. Please wait.", - ) + status_code=status.HTTP_404_NOT_FOUND, + detail=f"Simulation not found or error checking status: {str(e)}", + ) else: simulation_id = TR_RBD_ID diff --git a/src/overhaul_scope/model.py b/src/overhaul_scope/model.py index db717b9..1b228ba 100644 --- a/src/overhaul_scope/model.py +++ b/src/overhaul_scope/model.py @@ -1,6 +1,5 @@ -from sqlalchemy import null from sqlalchemy import JSON -from sqlalchemy import Column, DateTime, Float, Integer, String, ForeignKey, UUID +from sqlalchemy import Column, DateTime , Integer, String, ForeignKey, UUID from sqlalchemy.orm import relationship from src.database.core import Base