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%
6.6 KB · 86 lines python
Raw Blame History
1"""Live counters and aggregate statistics — always computed from the database, never hardcoded.23API 1.1 semantics: `entities.model` counts CANONICAL model releases (entity_type 'model', not merged) — artifacts (checkpoints, quantisations,4conversions) are `entities.artifact`, folded evaluation variants are excluded through `merged_into`. `organizations_total` is the number5shown by /companies (company + organization + lab + university). `definitions` says how every counter is counted."""6from __future__ import annotations78from typing import Any910from sqlalchemy.ext.asyncio import AsyncConnection1112from aiatlas.db import execute, fetch_all, fetch_one, jsonb, transaction13from aiatlas.sdk.archive import archive_size1415ORG_TYPES = ("company", "organization", "lab", "university")1617DEFINITIONS: dict[str, str] = {18    "models": "Canonical model releases (artifacts, quantisations, conversions and folded evaluation variants excluded; merged duplicates excluded).",19    "artifacts": "Checkpoints, quantisations, conversions and packagings of a canonical model (entity_type 'artifact').",20    "model_families": "Model families (Llama 4, Qwen3.6, Claude…) grouping canonical releases.",21    "organizations_total": "Companies + organizations + labs + universities, merged duplicates excluded — the same universe as /companies.",22    "entities_total": "All live entities of every type (merged duplicates excluded).",23    "change_events": "All change events ever recorded, including the initial back-filled corpus.",24    "change_events_24h": "Events OBSERVED in the last 24 hours (legacy counter — includes back-filled history when a connector first runs).",25    "change_events_live_24h": "Events that OCCURRED in the last 24 hours, excluding back-fill and source-document changes — what actually happened today.",26    "change_events_7d": "Events observed in the last 7 days (legacy counter).",27    "benchmark_results": "Current benchmark result rows (one per model × benchmark × metric × configuration run).",28    "prices_current": "Live provider price offers (model × provider × provider model id).",29    "claims_current": "Current temporal claims (one per entity × property).",30    "relations": "Live relations in the knowledge graph.",31    "sources": "Enabled sources (websites, registries, leaderboards) crawled by AI Atlas connectors.",32}333435async def live_counts(conn: AsyncConnection) -> dict[str, Any]:36    by_type = await fetch_all(conn, "select entity_type, count(*) as n from entities where merged_into is null group by 1")37    counts = {r["entity_type"]: int(r["n"]) for r in by_type}38    infra = await fetch_one(conn, """select (select count(*) from sources where enabled) as sources,39                                            (select count(*) from connectors) as connectors,40                                            (select count(*) from connectors where enabled) as connectors_enabled,41                                            (select count(*) from documents) as documents,42                                            (select count(*) from snapshots) as snapshots,43                                            (select count(*) from claims) as claims,44                                            (select count(*) from claims where status = 'current') as claims_current,45                                            (select count(*) from relations where valid_to is null) as relations,46                                            (select count(*) from change_events) as change_events,47                                            (select count(*) from change_events where observed_at > now() - interval '24 hours') as change_events_24h,48                                            (select count(*) from change_events where is_backfill = false and event_type <> 'DOCUMENT_CHANGED'49                                                    and occurred_at > now() - interval '24 hours') as change_events_live_24h,50                                            (select count(*) from change_events where is_backfill = false and event_type <> 'DOCUMENT_CHANGED'51                                                    and occurred_at > now() - interval '7 days') as change_events_live_7d,52                                            (select count(*) from change_events where observed_at > now() - interval '7 days') as change_events_7d,53                                            (select count(*) from benchmark_results where valid_to is null and is_current) as benchmark_results,54                                            (select count(*) from prices where valid_to is null) as prices_current,55                                            (select count(*) from prices) as prices_total,56                                            (select count(*) from jobs where status = 'queued') as jobs_queued,57                                            (select count(*) from jobs where status = 'dead') as jobs_dead,58                                            (select count(*) from review_queue where status = 'pending') as review_pending,59                                            (select count(*) from llm_jobs) as llm_jobs,60                                            (select coalesce(sum(input_tokens),0) + coalesce(sum(output_tokens),0) from llm_jobs) as llm_tokens,61                                            (select max(observed_at) from snapshots) as last_snapshot_at,62                                            (select max(observed_at) from change_events) as last_event_at,63                                            (select min(first_seen_at) from entities) as first_entity_at""")64    out = {"entities": counts, "entities_total": sum(counts.values()), **{k: (int(v) if isinstance(v, int) else v) for k, v in (infra or {}).items()}}65    out["organizations_total"] = sum(counts.get(t, 0) for t in ORG_TYPES)66    out["artifacts"] = counts.get("artifact", 0)67    out["model_families"] = counts.get("model_family", 0)68    out["definitions"] = DEFINITIONS69    return out707172async def compute_stats() -> dict[str, Any]:73    async with transaction() as conn:74        counts = await live_counts(conn)75        counts["archive"] = archive_size()76        await execute(conn, "insert into stats_snapshots (counts) values (cast(:c as jsonb))", c=jsonb(counts))77    return counts787980async def history(conn: AsyncConnection, days: int = 90) -> list[dict[str, Any]]:81    return await fetch_all(conn, """select distinct on (date_trunc('day', computed_at)) date_trunc('day', computed_at) as day, counts82                                    from stats_snapshots where computed_at > now() - make_interval(days => :d) order by 1 desc, computed_at desc""", d=days)838485__all__ = ["DEFINITIONS", "ORG_TYPES", "compute_stats", "history", "live_counts"]86