fix: analysis 500s, caching, alert page perf

P0: Fix KeyError in 3 analysis endpoints. geojson.py stores 1d risk
as "risk_value" but analysis.py accessed "risk_1d" — always crashed.

Backend: Add lru_cache to GeoJSON/CSV/Parquet loaders, date helpers,
and district loader. Add try/except and FileNotFoundError guards.

Frontend: Debounce riskRange, merge counts into useMemo, stabilize
handleGridClick with ref, memoize nearest-grid scan, wrap AlertMap
in React.memo, switch useLodGrid from fetch to cachedGet.
This commit is contained in:
2026-06-05 02:27:10 +08:00
parent fc468464b2
commit e64ca3b4f5
9 changed files with 154 additions and 114 deletions

View File

@@ -3,6 +3,7 @@ Date utilities: finding latest dates from GeoJSON files, parsing date strings.
"""
import glob
import re
from functools import lru_cache
from pathlib import Path
from fastapi import HTTPException
@@ -10,6 +11,7 @@ from fastapi import HTTPException
from config import DATA_DIR, DATE_FORMAT_GEOJSON
@lru_cache(maxsize=1)
def get_latest_date() -> str:
"""Get latest available date from GeoJSON files in DATA_DIR."""
pattern = str(DATA_DIR / "risk_*.geojson")
@@ -29,6 +31,7 @@ def get_latest_date() -> str:
return max(dates)
@lru_cache(maxsize=1)
def get_available_dates(days: int = 30) -> list[str]:
"""Get list of available dates, most recent first."""
pattern = str(DATA_DIR / "risk_*.geojson")