fix(automation): expose durable history degradation
All checks were successful
CD Pipeline / workflow-shape (push) Successful in 0s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / tests (push) Successful in 2m40s
CD Pipeline / build-and-deploy (push) Successful in 6m59s
CD Pipeline / post-deploy-checks (push) Successful in 1m51s

This commit is contained in:
ogt
2026-07-11 20:43:03 +08:00
parent e793718707
commit 1ecf10507a
3 changed files with 84 additions and 7 deletions

View File

@@ -17,11 +17,13 @@ from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel, Field
from src.core.csrf import CSRFToken # Phase 20: CSRF Protection
from src.services.auto_repair_service import (
get_auto_repair_service,
)
from src.services.incident_service import get_incident_service
from src.services.incident_service import (
IncidentDurableReadError,
get_incident_service,
)
router = APIRouter(prefix="/auto-repair", tags=["Auto Repair"])
@@ -251,6 +253,12 @@ class RepairHistoryResponse(BaseModel):
@router.get("/history", response_model=RepairHistoryResponse)
async def get_repair_history(
limit: int = Query(20, ge=1, le=100),
project_id: str = Query(
"awoooi",
min_length=1,
max_length=64,
description="租戶 IDlegacy repair history 預設綁定 awoooi。",
),
) -> RepairHistoryResponse:
"""
取得修復歷史記錄。
@@ -259,7 +267,9 @@ async def get_repair_history(
"""
try:
incident_service = get_incident_service()
all_incidents = await incident_service.get_active_incidents()
all_incidents = await incident_service.get_active_incidents(
project_id=project_id,
)
items: list[RepairHistoryItem] = []
for incident in all_incidents:
@@ -307,6 +317,23 @@ async def get_repair_history(
return RepairHistoryResponse(count=len(items), items=items)
except Exception:
# 任何錯誤都回傳空列表,不中斷前端
return RepairHistoryResponse(count=0, items=[])
except IncidentDurableReadError as exc:
raise HTTPException(
status_code=503,
detail={
"status": "degraded",
"reason_code": str(exc),
"source_of_truth": "postgresql",
"redis_fallback_used": False,
},
) from exc
except Exception as exc:
raise HTTPException(
status_code=503,
detail={
"status": "degraded",
"reason_code": "repair_history_readback_failed",
"source_of_truth": "postgresql",
"redis_fallback_used": False,
},
) from exc