perf: UX-safe fetch trimming and instant layer-enable refresh.

Drop duplicate slow-tier weather/ukraine jobs, gate correlations when off, slim health probes, keyed layer-panel subscriptions, align backend layer defaults with the dashboard, and fetch CCTV/FIRMS/PSK/etc. synchronously on enable so toggles stay responsive without background prefetch waste.
This commit is contained in:
BigBodyCobain
2026-06-23 00:16:38 -06:00
parent 4968f706bb
commit 53ed63ffcf
12 changed files with 532 additions and 59 deletions
@@ -0,0 +1,18 @@
"""Integration: layer enable triggers immediate data availability."""
from __future__ import annotations
from services.fetchers._store import active_layers, latest_data, _data_lock
def test_firms_enable_populates_slow_payload(client):
with _data_lock:
active_layers["firms"] = False
latest_data["firms_fires"] = []
r = client.post("/api/layers", json={"layers": {"firms": True}})
assert r.status_code == 200
slow = client.get("/api/live-data/slow")
assert slow.status_code == 200
fires = slow.json().get("firms_fires") or []
assert len(fires) > 0, "firms layer should populate on enable without waiting for scheduler"
@@ -0,0 +1,36 @@
"""Tests for on-enable layer refresh (Phase 2 UX guardrail)."""
from __future__ import annotations
from unittest.mock import patch
from services.fetchers._store import active_layers, bump_active_layers_version
from services.layer_enable_refresh import refresh_newly_enabled_layers, snapshot_active_layers
def test_refresh_firms_on_enable_only():
before = snapshot_active_layers()
active_layers["firms"] = True
bump_active_layers_version()
with (
patch("services.fetchers.earth_observation.fetch_firms_fires") as firms,
patch("services.fetchers.earth_observation.fetch_firms_country_fires") as country,
patch("services.fetchers._store.bump_data_version") as bump,
):
refresh_newly_enabled_layers({**before, "firms": False})
firms.assert_called_once()
country.assert_called_once()
bump.assert_called_once()
active_layers["firms"] = before.get("firms", False)
def test_refresh_skips_when_layer_stays_off():
before = {**snapshot_active_layers(), "cctv": False}
active_layers["cctv"] = False
with patch("services.fetchers.infrastructure.fetch_cctv") as fetch_cctv:
refresh_newly_enabled_layers(before)
fetch_cctv.assert_not_called()
@@ -0,0 +1,59 @@
"""Regression tests for UX-safe performance optimizations."""
from __future__ import annotations
import inspect
def test_slow_tier_skips_duplicate_time_critical_fetchers():
"""Weather + Ukraine alerts have dedicated scheduler jobs — not slow tier."""
from services import data_fetcher
source = inspect.getsource(data_fetcher.update_slow_data)
slow_block = source.split("_run_tasks(\"slow-tier\"", 1)[0]
assert "fetch_weather_alerts" not in slow_block
assert "fetch_ukraine_air_raid_alerts" not in slow_block
def test_slow_tier_gates_correlation_engine_on_active_layer():
from services import data_fetcher
source = inspect.getsource(data_fetcher.update_slow_data)
assert 'is_any_active("correlations")' in source
def test_health_uses_subset_refs_not_full_deepcopy():
from routers import health as health_router
source = inspect.getsource(health_router.health_check)
assert "_health_data_snapshot()" in source
assert "get_latest_data()" not in source
snap_source = inspect.getsource(health_router._health_data_snapshot)
assert "get_latest_data_subset_refs" in snap_source
assert "deepcopy" not in snap_source
def test_active_layers_defaults_match_dashboard_first_paint():
"""Backend must not prefetch layers the dashboard starts with disabled."""
from services.fetchers import _store
off_by_default = {
"cctv": False,
"firms": False,
"datacenters": False,
"power_plants": False,
"psk_reporter": False,
"viirs_nightlights": False,
"crowdthreat": False,
"gt_risk": False,
}
for key, expected in off_by_default.items():
assert _store.active_layers.get(key) is expected, key
def test_layer_enable_refresh_covers_cold_toggle_layers():
from services import layer_enable_refresh
source = inspect.getsource(layer_enable_refresh.refresh_newly_enabled_layers)
for key in ("cctv", "firms", "power_plants", "psk_reporter", "datacenters"):
assert f'"{key}"' in source or f"'{key}'" in source