fix(ingest): skip known KEV/FIRMS rows; CI skips unchanged images
CISA KEV republished 1685 NIST URLs every 5 min; FIRMS re-inserted ~325k global hotspots every 15 min. Producer now skips URLs already in event_dedup and FIRMS CSVs that are unchanged (delta-only persist). CI rebuilds only images whose paths changed and never pulls/rebuilds Timescale or bounces osint-db unless Dockerfile.pg changes.
This commit is contained in:
parent
0406eb6b7b
commit
5651d251d0
9 changed files with 438 additions and 94 deletions
|
|
@ -131,6 +131,100 @@ def test_ingest_fires_uses_keystore_key(monkeypatch):
|
|||
assert any("VIIRS_NOAA21_NRT" in u for u in captured["urls"])
|
||||
|
||||
|
||||
def _reset_firms_poll_state():
|
||||
from upstream_cache import firms_cache
|
||||
import fire_sources
|
||||
|
||||
firms_cache.clear()
|
||||
if hasattr(fire_sources, "_csv_digest"):
|
||||
fire_sources._csv_digest.clear()
|
||||
if hasattr(fire_sources, "_seen_ids"):
|
||||
fire_sources._seen_ids.clear()
|
||||
|
||||
|
||||
def _fake_firms_http(monkeypatch, bodies_by_call: list[str] | None = None, body: str = SAMPLE_CSV):
|
||||
hits = {"n": 0}
|
||||
|
||||
class FakeResp:
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def raise_for_status(self):
|
||||
pass
|
||||
|
||||
class FakeClient:
|
||||
def __init__(self, **kw):
|
||||
pass
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *exc):
|
||||
return False
|
||||
|
||||
async def get(self, url):
|
||||
idx = hits["n"]
|
||||
hits["n"] += 1
|
||||
if bodies_by_call is not None:
|
||||
text = bodies_by_call[min(idx, len(bodies_by_call) - 1)]
|
||||
else:
|
||||
text = body
|
||||
return FakeResp(text)
|
||||
|
||||
monkeypatch.setenv("FIRMS_MAP_KEY", "k" * 32)
|
||||
monkeypatch.setattr("fire_sources.FIRMS_DATASETS", ["VIIRS_NOAA20_NRT"])
|
||||
monkeypatch.setattr("fire_sources.httpx.AsyncClient", FakeClient)
|
||||
return hits
|
||||
|
||||
|
||||
def test_ingest_fires_skips_unchanged_csv(monkeypatch):
|
||||
"""Same FIRMS CSV must not be re-parsed into a 100k-row ON CONFLICT insert."""
|
||||
_reset_firms_poll_state()
|
||||
hits = _fake_firms_http(monkeypatch)
|
||||
persisted = []
|
||||
|
||||
async def fake_persist(points):
|
||||
persisted.append(len(points))
|
||||
return len(points)
|
||||
|
||||
monkeypatch.setattr("fire_sources.persist_hotspots", fake_persist)
|
||||
|
||||
assert asyncio.run(ingest_fires()) == 5
|
||||
assert persisted == [5]
|
||||
firms_cache_hits = hits["n"]
|
||||
persisted.clear()
|
||||
assert asyncio.run(ingest_fires()) == 0
|
||||
assert persisted == []
|
||||
# TTL cache may skip HTTP; either way we must not persist again.
|
||||
assert hits["n"] >= firms_cache_hits
|
||||
|
||||
|
||||
def test_ingest_fires_persists_only_new_hotspots(monkeypatch):
|
||||
"""When the CSV grows, persist the delta — not the whole 2-day dump."""
|
||||
_reset_firms_poll_state()
|
||||
extra = (
|
||||
SAMPLE_CSV
|
||||
+ "16.00000,-12.00000,340.00,0.40,0.40,2025-06-06,1500,N20,VIIRS,h,2.0NRT,310.00,8.00,D\n"
|
||||
)
|
||||
hits = _fake_firms_http(monkeypatch, bodies_by_call=[SAMPLE_CSV, extra])
|
||||
persisted = []
|
||||
|
||||
async def fake_persist(points):
|
||||
persisted.append([p["latitude"] for p in points])
|
||||
return len(points)
|
||||
|
||||
monkeypatch.setattr("fire_sources.persist_hotspots", fake_persist)
|
||||
|
||||
from upstream_cache import firms_cache
|
||||
|
||||
assert asyncio.run(ingest_fires()) == 5
|
||||
firms_cache.clear() # force the next poll to see the grown CSV
|
||||
persisted.clear()
|
||||
assert asyncio.run(ingest_fires()) == 1
|
||||
assert persisted == [[16.0]]
|
||||
assert hits["n"] == 2
|
||||
|
||||
|
||||
def _async_return(value):
|
||||
async def inner():
|
||||
return value
|
||||
|
|
|
|||
Loading…
Reference in a new issue