release: bump version to 0.84.0
ci / backend (push) Canceled after 0s
ci / frontend (push) Canceled after 0s
ci / delivery (push) Canceled after 0s

This commit is contained in:
linkong committed 2026-12-04 07:07:19 +08:00
1 parent 3b1b9ef68c
commit e1cc27f13d
241 files changed
+42779 -6027

No files matched your search

+149 -22
View File
@@ -1,6 +1,6 @@
"""Compute observations, administrator configuration and independent facility samples."""
from datetime import UTC, datetime
from datetime import UTC, date, datetime
from app.core.compute_types import COMPUTE_SITE_TYPES, compute_record_filter, compute_site_type_expression
from app.api.v1.visualization import convert_compute_centers_to_geojson
@@ -8,10 +8,11 @@ from app.core.access_gate import require_earth_or_api_access
from app.core.earth_geography_policy import display_country
from app.core.enums import UserRole
from app.core.logging import get_logger
from app.core.security import get_current_user
from app.core.security import get_current_user, get_optional_current_user
from app.core.websocket.broadcaster import broadcaster
from app.db.session import get_db
from app.models.collected_data import CollectedData
from app.models.compute_configuration_revision import ComputeConfigurationRevision
from app.models.data_snapshot import DataSnapshot
from app.models.user import User
from app.schemas.compute_configuration import (
@@ -29,10 +30,12 @@ from app.services.compute_center_locations import refresh_compute_center_locatio
from app.services.compute_configuration import (
DEFAULT_CONFIGURATION,
configuration_response,
list_configuration_revisions,
publish_configuration,
read_configuration,
)
from app.services.compute_history import ComputeSeriesMode, compute_history
from app.services.compute_lineage import build_observation_lineage, is_admin_user
from fastapi import APIRouter, Depends, HTTPException, Query, Response
from sqlalchemy import and_, case, func, or_, select
from sqlalchemy.ext.asyncio import AsyncSession
@@ -70,13 +73,35 @@ async def get_series(
countries: str = "CN,US",
mode: ComputeSeriesMode = "global_share",
db: AsyncSession = Depends(get_db),
as_of: str | None = None,
publish_id: str | None = None,
):
codes = selected_countries(countries)
envelope = await read_configuration(db)
envelope = await read_configuration(db, as_of=as_of, publish_id=publish_id)
response.headers["Cache-Control"] = "no-store"
if envelope.get("snapshot_unavailable"):
empty_cfg = ComputeConfiguration(
reviewed_at=date.today(), sources=[], observations=[], innovation=[]
)
result = compute_history(codes, mode, empty_cfg)
result.update(
version=None,
publish_id=None,
published_at=None,
as_of=envelope.get("as_of"),
snapshot_unavailable=True,
notice=envelope.get("notice"),
provenance="snapshot_unavailable",
)
return result
result = compute_history(codes, mode, ComputeConfiguration.model_validate(envelope["config"]))
effective_pid = envelope.get("publish_id") or envelope["revision"]
result.update(
version=envelope["revision"],
version=effective_pid,
publish_id=effective_pid,
published_at=envelope.get("published_at"),
as_of=envelope.get("as_of"),
snapshot_unavailable=False,
provenance="bundled_reports" if envelope["is_default"] else "admin_curated_reports",
)
return result
@@ -98,6 +123,108 @@ async def get_configuration(
return configuration_response(await read_configuration(db))
@router.get("/revisions")
async def get_configuration_revisions(
response: Response,
limit: int = Query(20, ge=1, le=100),
_actor: User = Depends(configuration_admin),
db: AsyncSession = Depends(get_db),
):
response.headers["Cache-Control"] = "no-store"
items = await list_configuration_revisions(db, limit=limit, redact=False)
return {"items": items}
@router.get(
"/revisions/public",
dependencies=[Depends(require_earth_or_api_access("data_products:read"))],
)
async def get_public_configuration_revisions(
response: Response,
limit: int = Query(20, ge=1, le=50),
db: AsyncSession = Depends(get_db),
):
response.headers["Cache-Control"] = "no-store"
items = await list_configuration_revisions(db, limit=limit, redact=True)
return {"items": items}
@router.get(
"/lineage",
dependencies=[Depends(require_earth_or_api_access("data_products:read"))],
)
async def get_compute_observation_lineage(
response: Response,
code: str = Query(..., min_length=2, max_length=32),
metric: ComputeSeriesMode = "global_share",
year: int = Query(..., ge=2000, le=2100),
publish_id: str | None = None,
as_of: str | None = None,
current_user: User | None = Depends(get_optional_current_user),
db: AsyncSession = Depends(get_db),
):
response.headers["Cache-Control"] = "no-store"
return await build_observation_lineage(
db,
code=code,
metric=metric,
year=year,
publish_id=publish_id,
as_of=as_of,
redact=not is_admin_user(current_user),
)
async def _deliver_revision_presentation(
db: AsyncSession,
envelope: dict,
) -> dict:
"""Trigger revision-driven presentation after commit; record failures for scheduled retry."""
publish_id = envelope.get("publish_id") or envelope.get("revision")
if not publish_id or envelope.get("no_changes"):
return envelope
rev_row = (
await db.execute(
select(ComputeConfigurationRevision).where(
ComputeConfigurationRevision.publish_id == publish_id
)
)
).scalar_one_or_none()
if not isinstance(rev_row, ComputeConfigurationRevision):
return envelope
if rev_row.presentation_status == "skipped":
envelope["presentation_status"] = "skipped"
return envelope
from app.services.ai.proactive_pipeline import process_compute_revision_presentation
try:
res = await process_compute_revision_presentation(db, rev_row.id)
await db.commit()
envelope["presentation_status"] = str(res.get("status") or rev_row.presentation_status)
except Exception as exc:
await db.rollback()
logger.warning_event(
"Compute revision presentation failed; queued for retry",
event="compute.revision.presentation_failed",
context={"publish_id": publish_id, "revision_id": rev_row.id, "error": str(exc)},
)
failed_row = (
await db.execute(
select(ComputeConfigurationRevision).where(
ComputeConfigurationRevision.publish_id == publish_id
)
)
).scalar_one_or_none()
if isinstance(failed_row, ComputeConfigurationRevision):
failed_row.presentation_status = "failed"
failed_row.presentation_attempts = int(failed_row.presentation_attempts or 0) + 1
failed_row.presentation_error = str(exc)[:1000]
await db.commit()
envelope["presentation_status"] = "failed"
return envelope
@router.put("/configuration")
async def update_configuration(
payload: ComputeConfigurationWrite,
@@ -105,16 +232,16 @@ async def update_configuration(
db: AsyncSession = Depends(get_db),
):
envelope = await publish_configuration(
db, revision=payload.revision, config=payload.config, actor=actor,
db,
revision=payload.revision,
config=payload.config,
actor=actor,
publish_note=payload.publish_note,
source_candidate_ids=payload.source_candidate_ids,
)
try:
from app.services.ai.proactive_pipeline import process_compute_config_proactive_delta
await process_compute_config_proactive_delta(db)
await db.commit()
except Exception:
pass
await _broadcast_compute_configuration_update()
if not envelope.get("no_changes"):
envelope = await _deliver_revision_presentation(db, envelope)
await _broadcast_compute_configuration_update()
return configuration_response(envelope)
@@ -125,16 +252,16 @@ async def reset_configuration(
db: AsyncSession = Depends(get_db),
):
envelope = await publish_configuration(
db, revision=payload.revision, config=DEFAULT_CONFIGURATION, actor=actor, reset=True,
db,
revision=payload.revision,
config=DEFAULT_CONFIGURATION,
actor=actor,
reset=True,
publish_note=payload.publish_note,
)
try:
from app.services.ai.proactive_pipeline import process_compute_config_proactive_delta
await process_compute_config_proactive_delta(db)
await db.commit()
except Exception:
pass
await _broadcast_compute_configuration_update()
if not envelope.get("no_changes"):
envelope = await _deliver_revision_presentation(db, envelope)
await _broadcast_compute_configuration_update()
return configuration_response(envelope)