- Replay models: StepType enum, ReplayStep, ReplayPage frozen dataclasses
- Checkpoint transformer: PostgresSaver JSONB -> structured timeline steps
- Replay API: GET /api/conversations (paginated), GET /api/replay/{thread_id}
- Analytics models: AgentUsage, InterruptStats, AnalyticsResult
- Analytics event recorder: Protocol + PostgresAnalyticsRecorder + NoOp
- Analytics queries: resolution_rate, agent_usage, escalation_rate, cost, interrupts
- Analytics API: GET /api/analytics?range=Xd with envelope response
- DB migration: analytics_events table + conversations column additions
- 74 new tests, 399 total passing, 92.87% coverage
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
"""Analytics API router -- dashboard metrics endpoint."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from dataclasses import asdict
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from fastapi import APIRouter, HTTPException, Query, Request
|
|
|
|
from app.analytics.queries import get_analytics
|
|
|
|
if TYPE_CHECKING:
|
|
from psycopg_pool import AsyncConnectionPool
|
|
|
|
router = APIRouter(prefix="/api/analytics", tags=["analytics"])
|
|
|
|
_RANGE_PATTERN = re.compile(r"^(\d+)d$")
|
|
_DEFAULT_RANGE = "7d"
|
|
|
|
|
|
async def _get_pool(request: Request) -> AsyncConnectionPool:
|
|
"""Dependency: extract the shared pool from app state."""
|
|
return request.app.state.pool
|
|
|
|
|
|
def _envelope(data: Any, *, success: bool = True, error: str | None = None) -> dict:
|
|
return {"success": success, "data": data, "error": error}
|
|
|
|
|
|
def _parse_range(range_str: str) -> int:
|
|
"""Parse 'Xd' range string to integer days. Raises 400 on invalid format."""
|
|
match = _RANGE_PATTERN.match(range_str)
|
|
if not match:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail=f"Invalid range format '{range_str}'. Expected format: '<N>d' e.g. '7d', '30d'.",
|
|
)
|
|
return int(match.group(1))
|
|
|
|
|
|
@router.get("")
|
|
async def analytics(
|
|
request: Request,
|
|
range: str = Query(default=_DEFAULT_RANGE, alias="range"), # noqa: A002
|
|
) -> dict:
|
|
"""Return aggregated analytics metrics for the given time range."""
|
|
range_days = _parse_range(range)
|
|
pool = await _get_pool(request)
|
|
result = await get_analytics(pool, range_days=range_days)
|
|
return _envelope(asdict(result))
|