SPB Git forge

spb/ai-atlas

Public
41commits 1branches 0releases
4.6 MBsize
maindefault branch
12 days agolast push
HTML 77.2% TypeScript 10.5% Python 9.6% JavaScript 2.5%
1.3 KB · 35 lines python
Raw Blame History
1"""/stats · /stats/history — always live from the database."""2from __future__ import annotations34import asyncio5from datetime import UTC, datetime6from typing import Any78from fastapi import APIRouter, Query, Request910from aiatlas.api.common import MAIN_ENTITY_TYPES, cached11from aiatlas.db import connection12from aiatlas.sdk.archive import archive_size13from aiatlas.services.stats import history, live_counts1415router = APIRouter(prefix="/api/v1/stats", tags=["stats"])161718@router.get("")19@cached(60)20async def stats(request: Request) -> dict[str, Any]:21    async with connection() as conn:22        counts = await live_counts(conn)23    counts["entities"] = {**{t: 0 for t in (*MAIN_ENTITY_TYPES, "artifact", "model_family")}, **counts["entities"]}  # zero-filled: the homepage never renders a missing key24    counts["archive"] = await asyncio.to_thread(archive_size)25    counts["computed_at"] = datetime.now(UTC)26    return counts272829@router.get("/history")30@cached(300)31async def stats_history(request: Request, days: int = Query(90, ge=1, le=730)) -> dict[str, Any]:32    async with connection() as conn:33        rows = await history(conn, days)34    return {"items": [{"day": r["day"].date().isoformat() if hasattr(r["day"], "date") else str(r["day"]), "counts": r["counts"]} for r in rows]}35