fix(api): degrade recent event readback failures
All checks were successful
CD Pipeline / workflow-shape (push) Successful in 1s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / tests (push) Successful in 2m26s
CD Pipeline / build-and-deploy (push) Successful in 4m41s
CD Pipeline / post-deploy-checks (push) Successful in 4m49s

This commit is contained in:
ogt
2026-07-09 19:46:14 +08:00
parent 607c5e5a40
commit 13ce6218ae
2 changed files with 52 additions and 6 deletions

View File

@@ -118,6 +118,8 @@ class RecentEventsResponse(BaseModel):
events: list[ChannelEventItem]
total: int
limit: int
source_status: str = "ready"
source_error: str | None = None
class ChannelEventDossierItem(BaseModel):
@@ -667,9 +669,26 @@ async def list_recent_events(
),
limit: int = Query(20, ge=1, le=100, description="最多返回筆數"),
) -> dict[str, Any]:
return await list_recent_channel_events(
project_id=project_id,
channel_type=channel_type,
provider_prefix=provider_prefix,
limit=limit,
)
try:
return await list_recent_channel_events(
project_id=project_id,
channel_type=channel_type,
provider_prefix=provider_prefix,
limit=limit,
)
except Exception as exc:
logger.warning(
"recent_channel_events_source_unavailable",
project_id=project_id or "awoooi",
channel_type=channel_type,
provider_prefix=provider_prefix,
limit=limit,
error=exc.__class__.__name__,
)
return {
"events": [],
"total": 0,
"limit": max(1, min(limit, 100)),
"source_status": "source_unavailable",
"source_error": exc.__class__.__name__,
}

View File

@@ -10,6 +10,33 @@ from src.core.context import (
)
@pytest.mark.asyncio
async def test_recent_events_degrades_when_source_unavailable(monkeypatch):
async def fake_list_recent_channel_events(**kwargs):
raise RuntimeError("db unavailable")
monkeypatch.setattr(
events,
"list_recent_channel_events",
fake_list_recent_channel_events,
)
response = await events.list_recent_events(
project_id="awoooi",
channel_type=None,
provider_prefix="alertmanager",
limit=20,
)
assert response == {
"events": [],
"total": 0,
"limit": 20,
"source_status": "source_unavailable",
"source_error": "RuntimeError",
}
@pytest.mark.asyncio
async def test_source_correlation_review_uses_body_project_context(monkeypatch):
observed: dict[str, object] = {}