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:
@@ -2,17 +2,26 @@
|
||||
GeoJSON file parsing utilities.
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from config import WUHAN_BOUNDARY_PATH
|
||||
from utils.risk import risk_value_to_level
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@lru_cache(maxsize=8)
|
||||
def parse_geojson_file(filepath: Path) -> list[dict[str, Any]]:
|
||||
"""Parse GeoJSON file and extract grid data with standard fields."""
|
||||
with open(filepath, "r", encoding="utf-8") as f:
|
||||
geojson = json.load(f)
|
||||
try:
|
||||
with open(filepath, "r", encoding="utf-8") as f:
|
||||
geojson = json.load(f)
|
||||
except (json.JSONDecodeError, OSError) as e:
|
||||
logger.warning("Failed to parse GeoJSON file %s: %s", filepath, e)
|
||||
return []
|
||||
|
||||
grids: list[dict[str, Any]] = []
|
||||
for feature in geojson.get("features", []):
|
||||
@@ -33,6 +42,7 @@ def parse_geojson_file(filepath: Path) -> list[dict[str, Any]]:
|
||||
return grids
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def load_districts() -> list[dict[str, Any]]:
|
||||
"""Load Wuhan district boundaries from GeoJSON."""
|
||||
if not WUHAN_BOUNDARY_PATH.exists():
|
||||
|
||||
Reference in New Issue
Block a user