SPB Git forge
28commits 1branches 0releases
7.7 MBsize
maindefault branch
10 days agolast push
Python 66.3% TypeScript 22.7% JavaScript 8.6% HTML 1.4% CSS 0.7%
1.5 KB · 30 lines python
Raw Blame History
1"""Rankings over `metrics_current` with window deltas from `metric_series` (pricing_changes counts events in the window)."""2from __future__ import annotations34from typing import Any56from fastapi import APIRouter, Query, Request78from companyatlas.api import aggregates as agg9from companyatlas.api.common import cached, cached_response10from companyatlas.db import connection1112ORDER = 2013router = APIRouter(prefix="/api/v1", tags=["rankings"])14KINDS = "|".join(agg.RANKING_KINDS)151617@router.get("/rankings", summary="Ranked companies by kind and window (cached 120 s)")18async def rankings(request: Request, kind: str = Query("most_active", pattern=f"^({KINDS})$"), window: str = Query("7d", pattern="^(24h|7d|30d|90d|1y)$"),19                   country: str | None = Query(None, max_length=2), industry: str | None = Query(None, max_length=80),20                   limit: int = Query(50, ge=1, le=200), sparkline: str | None = None) -> Any:21    spark = sparkline in ("1", "true", "yes")2223    async def produce() -> dict[str, Any]:24        async with connection() as conn:25            items = await agg.ranking_cards(conn, kind, window, country=(country or None), industry=(industry or None), limit=limit, sparkline=spark)26        return {"kind": kind, "window": window, "country": (country or "").upper() or None, "industry": industry or None, "items": items,27                "kinds": list(agg.RANKING_KINDS)}28    key = f"rankings:{kind}:{window}:{(country or '').upper()}:{industry or ''}:{limit}:{int(spark)}"29    return cached_response(request, await cached(key, 120, produce), 120)30