Fix fetch failure

This commit is contained in:
Logan
2026-06-03 00:19:12 -04:00
parent f65873d690
commit fe6bf55c0e
+34 -7
View File
@@ -5,6 +5,22 @@ from app.internal.auth import require_admin_token, require_firebase_token
from app.internal.feature_flags import get_flags, set_flags
from app.internal import firestore as fstore
async def _get_ai_enabled_system_ids(global_flags: dict) -> set[str]:
"""Return system_ids where at least one AI function (STT or correlation) is effectively on."""
global_stt = global_flags.get("stt_enabled", True)
global_corr = global_flags.get("correlation_enabled", True)
all_systems = await fstore.collection_list("systems")
enabled: set[str] = set()
for system in all_systems:
sid = system.get("system_id")
if not sid:
continue
ai_flags = system.get("ai_flags") or {}
if ai_flags.get("stt_enabled", global_stt) or ai_flags.get("correlation_enabled", global_corr):
enabled.add(sid)
return enabled
router = APIRouter(prefix="/admin", tags=["admin"])
@@ -73,10 +89,18 @@ async def debug_correlation(
"skip_reason": call.get("skip_reason"),
}
# ── Fetch recent incidents ────────────────────────────────────────────────
# ── Determine which systems have AI active ────────────────────────────────
global_flags = await get_flags()
ai_systems = await _get_ai_enabled_system_ids(global_flags)
# ── Fetch recent incidents (AI-enabled systems only) ──────────────────────
all_incidents = await fstore.collection_list("incidents")
all_incidents.sort(key=lambda i: i.get("updated_at", ""), reverse=True)
incidents = all_incidents[:limit]
ai_incidents = [
i for i in all_incidents
if any(sid in ai_systems for sid in (i.get("system_ids") or []))
]
incidents = ai_incidents[:limit]
# ── Fetch all linked call docs in parallel ────────────────────────────────
all_call_ids: list[str] = []
@@ -98,15 +122,18 @@ async def debug_correlation(
]
incident_records.append(rec)
# ── Recent orphaned calls ─────────────────────────────────────────────────
# ── Recent orphaned calls (AI-enabled systems only) ───────────────────────
# Use a single-field range query to avoid requiring a composite Firestore index;
# filter status and system in Python.
cutoff = datetime.now(timezone.utc) - timedelta(hours=orphan_hours)
recent_ended = await fstore.collection_where("calls", [
("status", "==", "ended"),
recent_calls = await fstore.collection_where("calls", [
("ended_at", ">=", cutoff),
])
orphans = [
_call_summary(c) for c in recent_ended
if not c.get("incident_ids") and not c.get("incident_id")
_call_summary(c) for c in recent_calls
if c.get("status") == "ended"
and not c.get("incident_ids") and not c.get("incident_id")
and c.get("system_id") in ai_systems
]
orphans.sort(key=lambda c: c.get("started_at", ""), reverse=True)