diff --git a/backend/app/services/strategy_service.py b/backend/app/services/strategy_service.py index da38584..8f5f643 100644 --- a/backend/app/services/strategy_service.py +++ b/backend/app/services/strategy_service.py @@ -705,21 +705,23 @@ def resume_strategy(user_id: str): return {"status": "resumed", "run_id": run_id} def get_strategy_status(user_id: str): - run_id = get_active_run_id(user_id) + running_run_id = get_running_run_id(user_id) + run_id = running_run_id or get_active_run_id(user_id) cfg = _load_config(user_id, run_id) if run_id else {} + default_status = "RUNNING" if running_run_id else ("STOPPED" if run_id else "IDLE") with db_connection() as conn: with conn.cursor() as cur: cur.execute( - "SELECT status, last_updated FROM engine_status WHERE user_id = %s AND run_id = %s", + "SELECT last_updated FROM engine_status WHERE user_id = %s AND run_id = %s", (user_id, run_id), ) row = cur.fetchone() if not row: - status = {"status": "IDLE", "last_updated": None} + status = {"status": default_status, "last_updated": None} else: status = { - "status": row[0], - "last_updated": _format_local_ts(row[1]), + "status": default_status, + "last_updated": _format_local_ts(row[0]), } status["run_id"] = run_id sip_frequency = cfg.get("sip_frequency") @@ -742,7 +744,7 @@ def get_strategy_status(user_id: str): "broker": cfg.get("broker"), "active": cfg.get("active"), } - if status.get("status") == "RUNNING": + if running_run_id: mode = (cfg.get("mode") or "LIVE").strip().upper() with engine_context(user_id, run_id): state = load_state(mode=mode) @@ -765,35 +767,37 @@ def get_strategy_status(user_id: str): return status def get_engine_status(user_id: str): - run_id = get_active_run_id(user_id) + running_run_id = get_running_run_id(user_id) + run_id = running_run_id or get_active_run_id(user_id) status = { "state": "STOPPED", "run_id": run_id, "user_id": user_id, "last_heartbeat_ts": None, } - with db_connection() as conn: - with conn.cursor() as cur: - cur.execute( - """ - SELECT status, last_updated - FROM engine_status - WHERE user_id = %s AND run_id = %s - ORDER BY last_updated DESC - LIMIT 1 - """, - (user_id, run_id), - ) - row = cur.fetchone() - if row: - status["state"] = row[0] - last_updated = row[1] - if last_updated is not None: - status["last_heartbeat_ts"] = ( - last_updated.astimezone(timezone.utc) - .isoformat() - .replace("+00:00", "Z") - ) + if running_run_id: + with db_connection() as conn: + with conn.cursor() as cur: + cur.execute( + """ + SELECT status, last_updated + FROM engine_status + WHERE user_id = %s AND run_id = %s + ORDER BY last_updated DESC + LIMIT 1 + """, + (user_id, run_id), + ) + row = cur.fetchone() + if row: + status["state"] = row[0] + last_updated = row[1] + if last_updated is not None: + status["last_heartbeat_ts"] = ( + last_updated.astimezone(timezone.utc) + .isoformat() + .replace("+00:00", "Z") + ) cfg = _load_config(user_id, run_id) mode = (cfg.get("mode") or "LIVE").strip().upper() with engine_context(user_id, run_id):