fix(agent): persist signal incidents with tenant context
Some checks failed
CD Pipeline / workflow-shape (push) Successful in 0s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / build-and-deploy (push) Has been cancelled
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / tests (push) Has been cancelled

This commit is contained in:
ogt
2026-07-10 21:09:31 +08:00
parent c004615220
commit 8aa8967c2b
5 changed files with 83 additions and 6 deletions

View File

@@ -97,6 +97,8 @@ def _derive_incident_alert_metadata(incident: Incident) -> dict[str, Any]:
async def _add_signal_timeline_event(
incident: Incident,
metadata: dict[str, Any],
*,
project_id: str,
) -> None:
"""Best-effort timeline seed for incidents created outside Alertmanager."""
alertname = metadata.get("alertname")
@@ -115,6 +117,7 @@ async def _add_signal_timeline_event(
actor_role="signal_worker",
risk_level=getattr(incident.severity, "value", incident.severity),
incident_id=incident.incident_id,
project_id=project_id,
)
except Exception as exc:
logger.warning(
@@ -137,10 +140,13 @@ class IncidentDbAdapter:
注入到 lewooogo-brain 的 DualIncidentMemory
"""
def __init__(self, project_id: str = "awoooi") -> None:
self._project_id = project_id
async def load(self, incident_id: str) -> Incident | None:
"""從 PostgreSQL 載入 Incident"""
try:
async with get_db_context() as db:
async with get_db_context(self._project_id) as db:
from sqlalchemy import select
stmt = select(IncidentRecord).where(
IncidentRecord.incident_id == incident_id
@@ -161,7 +167,7 @@ class IncidentDbAdapter:
metadata = _derive_incident_alert_metadata(incident)
created = False
try:
async with get_db_context() as db:
async with get_db_context(self._project_id) as db:
from sqlalchemy import select
stmt = select(IncidentRecord).where(
@@ -191,6 +197,7 @@ class IncidentDbAdapter:
else:
record = IncidentRecord(
incident_id=incident.incident_id,
project_id=self._project_id,
status=incident.status.value,
severity=incident.severity.value,
signals=[
@@ -222,7 +229,11 @@ class IncidentDbAdapter:
created = True
if created:
await _add_signal_timeline_event(incident, metadata)
await _add_signal_timeline_event(
incident,
metadata,
project_id=self._project_id,
)
logger.debug("db_adapter_save_success", incident_id=incident.incident_id)
return True