fix(health): ignore future article timestamps; flag unset vessel/FIRMS keys

/api/health articles.age_s used max(timestamp) with no future-date filter,
so rows ahead of now() reported a negative age. Use the same closed window
as the news list (timestamp <= now(); NULL timestamps are not fresh). If
every non-null timestamp is in the future, age_s is null and ok is false.

GET /api/stats keeps existing counters and adds feeds.vessels.configured
and feeds.fires.configured from non-empty api_keys values only. The stored
value is never returned.
This commit is contained in:
Sirius DevOps 2026-11-14 00:34:39 -05:00
commit 02a9a5626b
3 changed files with 192 additions and 4 deletions

View file

@ -272,7 +272,14 @@ async def _pipeline_checks(session) -> dict:
await _age("SELECT max(ingested_at) FROM events", "events", 20 * 60)
await _age("SELECT max(ingested_at) FROM fires", "fires", 30 * 60)
await _age("SELECT max(timestamp) FROM articles", "articles", 2 * 3600)
# Same closed-at-now window as GET /api/news. A future max() made age_s
# negative and hid a fresh list. NULL timestamps are not fresh.
await _age(
"SELECT max(timestamp) FROM articles "
"WHERE timestamp IS NOT NULL AND timestamp <= now()",
"articles",
2 * 3600,
)
await _age("SELECT max(batch_timestamp) FROM article_summaries", "summaries", 2 * 3600)
return checks
@ -298,7 +305,7 @@ async def _stats_counts() -> dict:
aircraft_last_known, vessel_last_known, train_count, nws_alert_count,
)
counts: dict[str, int | str] = {
counts: dict = {
"aircraft": len(aircraft_last_known),
"vessels": len(vessel_last_known),
"trains": train_count,
@ -330,9 +337,41 @@ async def _stats_counts() -> dict:
logger.warning("stats_db_failed", error=str(exc))
counts["timestamp"] = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
# Public booleans only. Never include the stored value, a prefix, or last-4.
counts["feeds"] = await _feed_flags()
return counts
async def _key_configured(*names: str) -> bool:
"""True when any named key has a non-empty value in the api_keys store.
Uses keystore.get_api_key — not env, not a second store. The value is
discarded after the emptiness check.
"""
from keystore import get_api_key
for name in names:
value = await get_api_key(name)
if isinstance(value, str) and value.strip():
return True
return False
async def _feed_flags() -> dict:
"""HUD layer flags. Fail closed (configured false) if the store is down."""
try:
vessels = await _key_configured("AISSTREAM_API_KEY", "VESSELAPI_API_KEY")
fires = await _key_configured("FIRMS_MAP_KEY")
except Exception as exc: # noqa: BLE001
logger.warning("stats_feed_flags_failed", error=str(exc))
vessels = False
fires = False
return {
"vessels": {"configured": vessels},
"fires": {"configured": fires},
}
@app.get("/api/stats")
async def api_stats():
"""Cheap HUD counters (counts only — no GeoJSON). Cached ~20 s."""