nws: prefix Update: when incoming alert supersedes a prior broadcast

Add _is_update() helper that checks CAP references field against
nws_alerts table. When any referenced CAP id has last_broadcast_at
set, the wire gets an Update: prefix via _render(prefix=). Applied
in both the new-alert and cold-start-race branches.

References field shape: list of dicts with identifier key containing
the superseded CAP id (urn:oid:...).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Matt Johnson (via Claude) 2026-06-10 02:40:07 +00:00
commit d88c6273ec

View file

@ -130,6 +130,24 @@ def _parse_motion(params: dict) -> tuple:
def _now() -> int: return int(time.time())
def _is_update(conn, d: dict) -> bool:
"""Return True if any CAP id in `references` was previously broadcast."""
refs = d.get("references") or []
if not refs:
return False
ref_ids = [r["identifier"] for r in refs
if isinstance(r, dict) and r.get("identifier")]
if not ref_ids:
return False
placeholders = ",".join("?" * len(ref_ids))
row = conn.execute(
f"SELECT 1 FROM nws_alerts WHERE event_id IN ({placeholders}) "
"AND last_broadcast_at IS NOT NULL LIMIT 1",
ref_ids,
).fetchone()
return row is not None
def _parse_iso(s: Optional[str]) -> Optional[int]:
if not s: return None
try: return int(datetime.fromisoformat(s.replace("Z", "+00:00")).timestamp())
@ -270,19 +288,21 @@ def handle_nws(envelope: dict, subject: str,
(cap_id, event_type, cap_severity, county, state,
headline, description, expires_epoch, now, None),
)
_prefix = "Update" if _is_update(conn, d) else ""
wire = _render(event_type=event_type, area_desc=area_desc,
geocoder_city=ge.get("city"), county=county, state=state,
expires_epoch=expires_epoch, lat=lat, lon=lon, now=now,
d=d)
prefix=_prefix, d=d)
_attach_commit(data, cap_id=cap_id, event_log_row_id=log_id)
return wire
if row["last_broadcast_at"] is None:
# Cold-start race: row exists but broadcast was previously dropped.
_prefix = "Update" if _is_update(conn, d) else ""
wire = _render(event_type=event_type, area_desc=area_desc,
geocoder_city=ge.get("city"), county=county, state=state,
expires_epoch=expires_epoch, lat=lat, lon=lon, now=now,
d=d)
prefix=_prefix, d=d)
_attach_commit(data, cap_id=cap_id, event_log_row_id=log_id)
return wire