fix: pipeline reliability — WS, summarizer, ingest, health
Stop the live HUD reconnect storm (nginx WS snippet + backoff), copy intel/nous_client into the summarizer image, and make event ingest idempotent on URL. GDELT uses the DOC API; NWS no longer sends bbox; FIRMS is one ON CONFLICT batch; GET /api/aircraft serves last-known. Health reports freshness without 503ing docker. EONET + CISA KEV added.
This commit is contained in:
parent
c6bfc50705
commit
d81ea68dec
26 changed files with 1164 additions and 110 deletions
|
|
@ -14,7 +14,9 @@ from sqlalchemy.dialects.postgresql import insert as pg_insert
|
|||
from database import async_session
|
||||
from models import events as events_table
|
||||
from models import fires as fires_table
|
||||
from models import event_dedup as event_dedup_table
|
||||
from config import NATS_URL
|
||||
from sources import event_dedup_key
|
||||
|
||||
logger = logging.getLogger("osint.ingestor")
|
||||
|
||||
|
|
@ -115,6 +117,42 @@ async def ingest_fire_row(msg: dict) -> bool:
|
|||
return inserted
|
||||
|
||||
|
||||
async def ingest_fire_rows(msgs: list[dict]) -> int:
|
||||
"""Bulk-insert FIRMS hotspots: one INSERT, one ON CONFLICT, one commit."""
|
||||
rows = []
|
||||
for msg in msgs:
|
||||
row = _fire_row_from_msg(msg)
|
||||
if row is not None:
|
||||
rows.append(row)
|
||||
if not rows:
|
||||
return 0
|
||||
async with async_session() as session:
|
||||
stmt = (
|
||||
pg_insert(fires_table)
|
||||
.values(rows)
|
||||
.on_conflict_do_nothing(constraint="pk_fires_natural_key")
|
||||
)
|
||||
result = await session.execute(stmt)
|
||||
await session.commit()
|
||||
inserted = int(result.rowcount or 0)
|
||||
if inserted:
|
||||
logger.info("bulk ingested %d/%d FIRMS hotspots", inserted, len(rows))
|
||||
from live_layers import aircraft_last_known
|
||||
from fire_aircraft import correlate_and_notify
|
||||
from tracks import recent_markers
|
||||
acs = list(aircraft_last_known.values()) or await recent_markers("aircraft")
|
||||
if acs:
|
||||
fires = [
|
||||
{
|
||||
"id": f"firms:{r['latitude']:.4f},{r['longitude']:.4f}",
|
||||
"lat": r["latitude"], "lon": r["longitude"], "label": "FIRMS",
|
||||
}
|
||||
for r in rows[:500]
|
||||
]
|
||||
await correlate_and_notify(fires, acs)
|
||||
return inserted
|
||||
|
||||
|
||||
async def ingest_event(msg: dict):
|
||||
"""Ingest a single event from NATS into PostgreSQL."""
|
||||
# Active fire/hotspot messages carry a dedicated schema and land in the
|
||||
|
|
@ -149,10 +187,26 @@ async def ingest_event(msg: dict):
|
|||
}
|
||||
|
||||
# Parse timestamp if string
|
||||
if isinstance(event_row["source_timestamp"], str):
|
||||
event_row["source_timestamp"] = datetime.fromisoformat(event_row["source_timestamp"])
|
||||
ts = event_row["source_timestamp"]
|
||||
if isinstance(ts, str):
|
||||
ts = datetime.fromisoformat(ts.replace("Z", "+00:00"))
|
||||
if isinstance(ts, datetime) and ts.tzinfo is None:
|
||||
ts = ts.replace(tzinfo=timezone.utc)
|
||||
event_row["source_timestamp"] = ts
|
||||
|
||||
key = event_dedup_key(event_row)
|
||||
async with async_session() as session:
|
||||
if key:
|
||||
dedup = (
|
||||
pg_insert(event_dedup_table)
|
||||
.values(url=key)
|
||||
.on_conflict_do_nothing(index_elements=["url"])
|
||||
)
|
||||
claimed = await session.execute(dedup)
|
||||
if not claimed.rowcount:
|
||||
await session.commit()
|
||||
logger.info("skip duplicate event url=%s", key)
|
||||
return None
|
||||
result = await session.execute(events_table.insert().values(**event_row))
|
||||
await session.commit()
|
||||
event_id = result.inserted_primary_key[0] # type: ignore[union-attr]
|
||||
|
|
|
|||
Loading…
Reference in a new issue