1"""/benchmarks listing, /benchmarks/{slug}, /benchmarks/{slug}/results (leaderboard), /benchmarks/{slug}/history."""2from __future__ import annotations34from typing import Any56from fastapi import APIRouter, Query, Request78from aiatlas.api.common import (9 ENTITY_COLS,10 ENTITY_FROM,11 PAGINATION,12 RESULT_COLS,13 RESULT_FROM,14 Pagination,15 cached,16 entity_cols,17 entity_summary,18 page,19 resolve_entity,20 resolve_id,21 result_row,22)23from aiatlas.api.detail import leaderboard24from aiatlas.api.routers.entities import detail_for_type25from aiatlas.db import connection, fetch_all, fetch_val2627router = APIRouter(prefix="/api/v1/benchmarks", tags=["benchmarks"])282930@router.get("")31@cached(300)32async def list_benchmarks(request: Request, category: str | None = None) -> dict[str, Any]:33 where = "e.entity_type = 'benchmark' and e.merged_into is null" + (" and e.attributes->>'category' ilike :cat" if category else "")34 async with connection() as conn:35 rows = await fetch_all(conn, f"""36 select {ENTITY_COLS},37 (select count(*) from benchmark_results r where r.benchmark_id = e.id and r.valid_to is null) as result_count,38 (select count(distinct r.model_id) from benchmark_results r where r.benchmark_id = e.id and r.valid_to is null) as model_count,39 top.score as top_score, {entity_cols("tm", "t_")}40 from {ENTITY_FROM}41 left join lateral (select r.model_id, r.score from benchmark_results r where r.benchmark_id = e.id and r.valid_to is null42 order by case when r.higher_is_better then -r.score else r.score end limit 1) top on true43 left join entities tm on tm.id = top.model_id left join entities tmo on tmo.id = tm.organization_id44 where {where} order by result_count desc, e.canonical_name""", cat=category)45 items = []46 for r in rows:47 top_model = entity_summary(r, "t_")48 items.append({**(entity_summary(r) or {}), "result_count": int(r["result_count"] or 0), "model_count": int(r["model_count"] or 0),49 "top": {"model": top_model, "score": r["top_score"]} if top_model else None})50 return {"items": items}515253@router.get("/{slug}")54@cached(300)55async def get_benchmark(request: Request, slug: str) -> dict[str, Any]:56 return await detail_for_type(slug, ("benchmark",))575859@router.get("/{slug}/results")60@cached(300)61async def benchmark_results(request: Request, slug: str, p: Pagination = PAGINATION, config: str | None = Query(None, max_length=200), history: int = Query(0, ge=0, le=1)) -> dict[str, Any]:62 async with connection() as conn:63 bench = await resolve_entity(conn, slug, ("benchmark",))64 items = await leaderboard(conn, bench["id"], limit=p.limit, offset=p.offset, config=config, history=bool(history))65 where = "r.benchmark_id = :id" + ("" if history else " and r.valid_to is null") + (" and r.config::text ilike :cfg" if config else "")66 total = await fetch_val(conn, f"select count(*) from benchmark_results r where {where}", id=bench["id"], cfg=f"%{config}%" if config else None)67 out = page(items, int(total or 0), p)68 out["benchmark"] = entity_summary(bench)69 return out707172@router.get("/{slug}/history")73@cached(300)74async def benchmark_history(request: Request, slug: str, model: str | None = None, limit: int = Query(1000, ge=1, le=5000)) -> dict[str, Any]:75 async with connection() as conn:76 bench = await resolve_entity(conn, slug, ("benchmark",))77 where = ["r.benchmark_id = :id"]78 params: dict[str, Any] = {"id": bench["id"], "lim": limit}79 if model:80 where.append("r.model_id = :model")81 params["model"] = await resolve_id(conn, model)82 rows = await fetch_all(conn, f"select {RESULT_COLS} from {RESULT_FROM} where {' and '.join(where)} order by r.observed_at asc, r.id limit :lim", **params)83 return {"benchmark": entity_summary(bench), "items": [result_row(r) for r in rows]}84