"""Compute observations, administrator configuration and independent facility samples.""" from datetime import UTC, datetime from typing import Literal from app.api.v1.visualization import convert_compute_centers_to_geojson from app.core.earth_geography_policy import display_country from app.core.enums import UserRole from app.core.security import get_current_user from app.db.session import get_db from app.models.collected_data import CollectedData from app.models.data_snapshot import DataSnapshot from app.models.user import User from app.schemas.compute_configuration import ( ComputeConfiguration, ComputeConfigurationReset, ComputeConfigurationWrite, ) from app.services.compute_catalog import ( PARTICIPANTS, participant_members, source_country_names, source_country_participants, ) from app.services.compute_center_locations import refresh_compute_center_location_cache from app.services.compute_configuration import ( DEFAULT_CONFIGURATION, configuration_response, publish_configuration, read_configuration, ) from app.services.compute_history import compute_history from fastapi import APIRouter, Depends, HTTPException, Query, Response from sqlalchemy import and_, case, func, or_, select from sqlalchemy.ext.asyncio import AsyncSession router = APIRouter() SOURCES = ("top500", "epoch_ai_gpu") def selected_countries(countries: str) -> list[str]: codes = list(dict.fromkeys(code.strip().upper() for code in countries.split(","))) if not codes or len(codes) > 12 or any(code not in PARTICIPANTS for code in codes): raise HTTPException(422, "Select 1–12 participants from the compute catalog") members = participant_members(codes) flattened = [country for participant in members.values() for country in participant] if len(flattened) != len(set(flattened)): raise HTTPException(422, "Comparison participants cannot have overlapping country members") return codes @router.get("/series") async def get_series( response: Response, countries: str = "CN,US", mode: Literal["global_share", "bilateral_share", "absolute"] = "global_share", db: AsyncSession = Depends(get_db), ): codes = selected_countries(countries) envelope = await read_configuration(db) response.headers["Cache-Control"] = "no-store" result = compute_history(codes, mode, ComputeConfiguration.model_validate(envelope["config"])) result.update( version=envelope["revision"], provenance="bundled_reports" if envelope["is_default"] else "admin_curated_reports", ) return result async def configuration_admin(user: User = Depends(get_current_user)) -> User: if user.role not in {UserRole.ADMIN.value, UserRole.SUPER_ADMIN.value}: raise HTTPException(403, "Administrator access required") return user @router.get("/configuration") async def get_configuration( response: Response, _actor: User = Depends(configuration_admin), db: AsyncSession = Depends(get_db), ): response.headers["Cache-Control"] = "no-store" return configuration_response(await read_configuration(db)) @router.put("/configuration") async def update_configuration( payload: ComputeConfigurationWrite, actor: User = Depends(configuration_admin), db: AsyncSession = Depends(get_db), ): envelope = await publish_configuration( db, revision=payload.revision, config=payload.config, actor=actor, ) return configuration_response(envelope) @router.post("/configuration/reset") async def reset_configuration( payload: ComputeConfigurationReset, actor: User = Depends(configuration_admin), db: AsyncSession = Depends(get_db), ): envelope = await publish_configuration( db, revision=payload.revision, config=DEFAULT_CONFIGURATION, actor=actor, reset=True, ) return configuration_response(envelope) async def sample_query(db: AsyncSession, countries: list[str], year: int | None): now = datetime.now(UTC) cutoff = min(datetime(year + 1, 1, 1, tzinfo=UTC), now) if year else now ranked = ( select( DataSnapshot.id, func.row_number() .over( partition_by=DataSnapshot.source, order_by=(DataSnapshot.completed_at.desc(), DataSnapshot.id.desc()), ) .label("rank"), ) .where( DataSnapshot.source.in_(SOURCES), DataSnapshot.status == "success", DataSnapshot.completed_at < cutoff, ) .subquery() ) snapshots = select(ranked.c.id).where(ranked.c.rank == 1) # Old collector rows have ambiguous units/identity and must not become # national evidence merely because a new UI can display them. query = select(CollectedData).where( CollectedData.snapshot_id.in_(snapshots), CollectedData.is_valid == 1, CollectedData.extra_data["compute_contract"].as_string() == "v1", or_( CollectedData.source == "top500", and_( CollectedData.extra_data["status"].as_string() == "Existing", func.coalesce(CollectedData.extra_data["superseded_by"].as_string(), "") == "", CollectedData.extra_data["possible_duplicate"].as_boolean().is_not(True), ), ), CollectedData.extra_data["country"].as_string().in_(source_country_names(countries)), ) return query @router.get("/facilities") async def get_facilities( countries: str = "CN,US", year: int | None = Query(None, ge=2000, le=2100), limit: int = Query(1000, ge=1, le=2000), db: AsyncSession = Depends(get_db), ): codes = selected_countries(countries) query = await sample_query(db, codes, year) country = CollectedData.extra_data["country"].as_string() source_participants = source_country_participants(codes) # Canonical source subdivisions retain their raw label on each record but # share one owner in the comparison, using the same policy as the globe. owner_cases = [ (country == name, display_country(name)) for name in source_country_names(codes) if display_country(name) != name ] display = case(*owner_cases, else_=country) if owner_cases else country participant_cases = [(country == name, code) for name, code in source_participants.items()] participant = case(*participant_cases, else_=None) counts = ( ( await db.execute( query.with_only_columns( display.label("country"), participant.label("participant_code"), CollectedData.source, func.count().label("count"), ).group_by(display, participant, CollectedData.source) ) ) .mappings() .all() ) participant_counts = ( ( await db.execute( query.with_only_columns( participant.label("participant_code"), CollectedData.source, func.count().label("count"), ).group_by(participant, CollectedData.source) ) ) .mappings() .all() ) records = ( (await db.execute(query.order_by(CollectedData.name, CollectedData.id).limit(limit))) .scalars() .all() ) if year: await refresh_compute_center_location_cache(db, as_of=datetime(year + 1, 1, 1, tzinfo=UTC)) else: await refresh_compute_center_location_cache(db) payload = convert_compute_centers_to_geojson(records) for feature in payload["features"]: feature["properties"]["historical_snapshot"] = year is not None properties = feature["properties"] properties["source_country"] = properties.get("country") properties["country"] = display_country(properties.get("country")) total = sum(row["count"] for row in counts) payload.update( { "counts": [dict(row) for row in counts], "participant_counts": [dict(row) for row in participant_counts], "participants": [PARTICIPANTS[code] for code in codes], "total_count": total, "returned_count": len(records), "truncated": total > limit, "reference_year": year, "scope": "public_source_records", "history_basis": ( "snapshots_collected_by_year_end" if year else "latest_successful_snapshot" ), "coverage": "available" if total else "no_verified_snapshot", "cross_source_deduplicated": False, } ) return payload