Merge origin/master into place dossier
Keep place-dossier JS (this PR) and conflicts overlay loaders from #39. Disjoint HUD features in index.html.
This commit is contained in:
commit
848ace15d1
8 changed files with 422 additions and 2 deletions
|
|
@ -27,6 +27,8 @@ _DEFAULT_SOURCE_URL = ",".join((
|
|||
"https://raw.githubusercontent.com/willytop8/Live-Environment-Streams/main/streams.geojson",
|
||||
# Official Caltrans CWWP2 JPEG + HLS CCTV (districts 1–12).
|
||||
*CALTRANS_CCTV_URLS,
|
||||
# Oregon DOT TripCheck public CCTV JPEG inventory (Esri JSON).
|
||||
"https://www.tripcheck.com/Scripts/map/data/cctvinventory.js",
|
||||
# Official MDOT MiDrive CCTV (JPEG stills, Michigan).
|
||||
MDOT_CAMERA_URL,
|
||||
))
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import hashlib
|
|||
import ipaddress
|
||||
import json
|
||||
import logging
|
||||
import math
|
||||
import re
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
|
|
@ -379,6 +380,51 @@ def parse_caltrans_json(text: str, source_name: str) -> list[dict]:
|
|||
return out
|
||||
|
||||
|
||||
# Oregon DOT TripCheck inventory bounding box (approx state extent).
|
||||
ODOT_BBOX = (41.9, 46.3, -124.6, -116.4) # lat_min, lat_max, lon_min, lon_max
|
||||
|
||||
|
||||
def parse_odot_json(text: str, source_name: str) -> list[dict]:
|
||||
"""Parse Oregon DOT TripCheck cctvinventory Esri-style JSON.
|
||||
|
||||
Store the JPEG still as snapshot_url (map thumbs); never RTSP. Keep only
|
||||
rows with finite coordinates inside Oregon and a usable filename.
|
||||
"""
|
||||
try:
|
||||
payload = json.loads(text)
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
return []
|
||||
lat_min, lat_max, lon_min, lon_max = ODOT_BBOX
|
||||
out: list[dict] = []
|
||||
for feat in payload.get("features") or []:
|
||||
attrs = (feat or {}).get("attributes") or {}
|
||||
filename = (attrs.get("filename") or "").strip()
|
||||
if not filename:
|
||||
continue
|
||||
try:
|
||||
lat = float(attrs.get("latitude"))
|
||||
lon = float(attrs.get("longitude"))
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if not (math.isfinite(lat) and math.isfinite(lon)):
|
||||
continue
|
||||
if not (lat_min <= lat <= lat_max and lon_min <= lon <= lon_max):
|
||||
continue
|
||||
jpeg = f"https://tripcheck.com/RoadCams/cams/{filename}"
|
||||
title = (attrs.get("title") or "").strip()
|
||||
out.append({
|
||||
"source_url": jpeg,
|
||||
"snapshot_url": jpeg,
|
||||
"discovery_source": "odot",
|
||||
"location_lat": lat,
|
||||
"location_lon": lon,
|
||||
"location_name": title or None,
|
||||
"vendor": "ODOT",
|
||||
"device_type": "http",
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
# MDOT MiDrive field extractors (fields carry rendered HTML).
|
||||
_MDOT_LAT_RE = re.compile(r"lat=(-?\d+(?:\.\d+)?)", re.I)
|
||||
_MDOT_LON_RE = re.compile(r"lon=(-?\d+(?:\.\d+)?)", re.I)
|
||||
|
|
@ -563,6 +609,8 @@ async def scrape_source(client: RateLimitedClient, geo: Geocoder,
|
|||
body = resp.text
|
||||
if "cwwp2.dot.ca.gov" in src_url or "cctvStatus" in src_url:
|
||||
cams = parse_caltrans_json(body, name)
|
||||
elif "cctvinventory" in src_url or "tripcheck.com" in src_url:
|
||||
cams = parse_odot_json(body, name)
|
||||
elif "mdotjboss.state.mi.us" in src_url or "/MiDrive/camera/list" in src_url:
|
||||
cams = parse_mdot_json(body, name)
|
||||
elif ("getCameraDataByLoc" in src_url
|
||||
|
|
|
|||
|
|
@ -97,6 +97,11 @@ _MAX_VESSELS = 6000
|
|||
# Last ADS-B snapshot + WFIGS points for fire↔tanker correlation.
|
||||
aircraft_last_known: dict[str, dict] = {}
|
||||
fire_last_known: list[dict] = []
|
||||
# Last-known counts for the cheap GET /api/stats HUD counter. Updated by the
|
||||
# upstream fetchers so the stats endpoint never does its own network/SQL fan-out
|
||||
# for these layers; reads are O(1) in-process.
|
||||
train_count: int = 0
|
||||
nws_alert_count: int = 0
|
||||
|
||||
|
||||
def overlay_catalog() -> dict:
|
||||
|
|
@ -972,6 +977,8 @@ async def fetch_trains(bbox: str | None, limit: int = DEFAULT_LIMIT) -> list[dic
|
|||
return transform_amtraker(await _get_json(AMTRAKER_TRAINS))
|
||||
|
||||
rows = await _ttl_get("amtraker:trains", 20.0, _load)
|
||||
global train_count
|
||||
train_count = len(rows)
|
||||
if bbox:
|
||||
minlon, minlat, maxlon, maxlat = parse_bbox(bbox)
|
||||
return filter_points_bbox(rows, minlon, minlat, maxlon, maxlat, limit)
|
||||
|
|
@ -1125,6 +1132,8 @@ async def fetch_weather_alerts(area: str | None, bbox: str | None) -> dict:
|
|||
logger.warning("NWS alerts fetch failed: %s", exc)
|
||||
nws_ok = False
|
||||
nws_fc = {"features": []}
|
||||
global nws_alert_count
|
||||
nws_alert_count = len(nws_fc.get("features") or [])
|
||||
sbw_fc = await _ttl_get("iem:sbw", 45.0, _load_iem)
|
||||
features = []
|
||||
for feat in nws_fc.get("features") or []:
|
||||
|
|
|
|||
62
app/main.py
62
app/main.py
|
|
@ -15,6 +15,7 @@ import asyncio
|
|||
import json
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from decimal import Decimal
|
||||
|
|
@ -274,6 +275,67 @@ def overlay_json(data, max_age: int) -> JSONResponse:
|
|||
return resp
|
||||
|
||||
|
||||
# ── HUD counters ─────────────────────────────────────────────────────────
|
||||
|
||||
# Cheap ~100 B–2 KB counts for the layer rail. Cached in-process so the HUD
|
||||
# can poll every second without re-hitting SQL or upstream feeds.
|
||||
_STATS_TTL = 20.0
|
||||
_stats_cache: dict[str, tuple[float, dict]] = {}
|
||||
|
||||
|
||||
async def _stats_counts() -> dict:
|
||||
"""Fan out to in-memory last-known / cheap SQL counts. Never raises."""
|
||||
from live_layers import (
|
||||
aircraft_last_known, vessel_last_known, train_count, nws_alert_count,
|
||||
)
|
||||
|
||||
counts: dict[str, int | str] = {
|
||||
"aircraft": len(aircraft_last_known),
|
||||
"vessels": len(vessel_last_known),
|
||||
"trains": train_count,
|
||||
"cameras": 0,
|
||||
"fires": 0,
|
||||
"quakes": 0,
|
||||
"alerts": nws_alert_count,
|
||||
}
|
||||
|
||||
# SQL counts are best-effort: a down DB or missing table must not 500 the
|
||||
# rail — the frontend still renders with zeros.
|
||||
try:
|
||||
from camera_models import cameras as cam_table
|
||||
async with async_session() as session:
|
||||
counts["cameras"] = int(
|
||||
(await session.execute(select(func.count()).select_from(cam_table))).scalar() or 0
|
||||
)
|
||||
counts["fires"] = int(
|
||||
(await session.execute(select(func.count()).select_from(fires))).scalar() or 0
|
||||
)
|
||||
counts["quakes"] = int(
|
||||
(await session.execute(
|
||||
select(func.count()).select_from(events).where(
|
||||
events.c.source_type == "earthquake"
|
||||
)
|
||||
)).scalar() or 0
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.warning("stats_db_failed", error=str(exc))
|
||||
|
||||
counts["timestamp"] = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
|
||||
return counts
|
||||
|
||||
|
||||
@app.get("/api/stats")
|
||||
async def api_stats():
|
||||
"""Cheap HUD counters (counts only — no GeoJSON). Cached ~20 s."""
|
||||
now = time.monotonic()
|
||||
cached = _stats_cache.get("stats")
|
||||
if cached and now - cached[0] < _STATS_TTL:
|
||||
return cached[1]
|
||||
payload = await _stats_counts()
|
||||
_stats_cache["stats"] = (now, payload)
|
||||
return overlay_json(payload, 15)
|
||||
|
||||
|
||||
# ── Feed Sources ──────────────────────────────────────────────────────────
|
||||
|
||||
@app.get("/api/sources", response_model=list[FeedSourceOut])
|
||||
|
|
|
|||
|
|
@ -352,6 +352,7 @@
|
|||
.lp-dot.wfigs { background: #ef4444; box-shadow: 0 0 7px #ef4444; }
|
||||
.lp-dot.trains { background: #c084fc; box-shadow: 0 0 7px #c084fc; }
|
||||
.lp-dot.storms { background: #f472b6; box-shadow: 0 0 7px #f472b6; }
|
||||
.lp-dot.conflicts { background: #ff2a6d; box-shadow: 0 0 7px #ff2a6d; }
|
||||
.lp-count { font-family: 'Share Tech Mono', monospace; font-size: 0.7rem; color: var(--cyan); }
|
||||
.lp-sub { display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; }
|
||||
.lp-opacity { display: flex; align-items: center; gap: 0.4rem; font-size: 0.62rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
|
||||
|
|
@ -460,6 +461,7 @@
|
|||
.blip-pop { min-width: 200px; max-width: 260px; }
|
||||
.blip-pop .blip-src { font-family: 'Share Tech Mono', monospace; font-size: 0.62rem; color: var(--cyan); text-transform: uppercase; letter-spacing: 0.08em; }
|
||||
.blip-pop .blip-time { font-size: 0.7rem; color: var(--muted); font-family: 'Share Tech Mono', monospace; margin: 0.2rem 0 0.3rem; }
|
||||
.blip-pop .blip-desc { font-size: 0.72rem; color: var(--text); line-height: 1.35; margin-top: 0.15rem; }
|
||||
|
||||
/* ── Sub-views (News / Events / Alerts / ... ) ── */
|
||||
.subview {
|
||||
|
|
@ -998,6 +1000,13 @@
|
|||
<span class="lp-count" id="lp-storms-count">0</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="lp-layer" id="conflicts-layer">
|
||||
<div class="lp-row">
|
||||
<label class="lp-name"><input type="checkbox" id="lp-conflicts-on" onchange="toggleConflicts()"> <span class="lp-dot conflicts"></span> Conflicts</label>
|
||||
<span class="lp-count" id="lp-conflicts-count">0</span>
|
||||
</div>
|
||||
<div class="lp-note">Curated theatres · color by severity · news counts, not LiveUAMap.</div>
|
||||
</div>
|
||||
|
||||
<div class="lp-legend">
|
||||
<h4>Fire heat intensity</h4>
|
||||
|
|
@ -2014,7 +2023,8 @@ let acGroup = null, acOn = true, acMilOn = false, acMilSupported = false;
|
|||
let trainsGroup = null, trainsOn = true;
|
||||
let vesselsGroup = null, vesselsOn = false;
|
||||
let stormsGroup = null, stormsOn = true;
|
||||
let overlayReq = {ac:0, trains:0, vessels:0, alerts:0, perim:0, incidents:0, storms:0, sar:0};
|
||||
let conflictsGroup = null, conflictsOn = false, conflictsCache = null;
|
||||
let overlayReq = {ac:0, trains:0, vessels:0, alerts:0, perim:0, incidents:0, storms:0, sar:0, conflicts:0};
|
||||
let moveDebounce = null;
|
||||
let overlayAbort = null;
|
||||
let lastCell = '';
|
||||
|
|
@ -2295,8 +2305,10 @@ async function initMap() {
|
|||
trainsOn = document.getElementById('lp-trains-on').checked;
|
||||
vesselsOn = document.getElementById('lp-vessels-on').checked;
|
||||
stormsOn = document.getElementById('lp-storms-on').checked;
|
||||
conflictsOn = document.getElementById('lp-conflicts-on').checked;
|
||||
connectLiveWs();
|
||||
loadChokepoints();
|
||||
probeConflicts();
|
||||
requestAnimationFrame(() => {
|
||||
if (firesOn) loadFires();
|
||||
setTimeout(() => {
|
||||
|
|
@ -3734,6 +3746,96 @@ async function openPlaceDossier(latlng) {
|
|||
}
|
||||
}
|
||||
|
||||
function conflictSeverityColor(sev) {
|
||||
const s = String(sev || '').toLowerCase();
|
||||
if (s === 'war') return '#ff2a6d';
|
||||
if (s === 'high') return '#fb923c';
|
||||
if (s === 'elevated') return '#facc15';
|
||||
return '#35e0ff';
|
||||
}
|
||||
function hideConflictsToggle() {
|
||||
const row = document.getElementById('conflicts-layer');
|
||||
if (row) row.hidden = true;
|
||||
const cb = document.getElementById('lp-conflicts-on');
|
||||
if (cb) cb.checked = false;
|
||||
conflictsOn = false;
|
||||
conflictsCache = null;
|
||||
conflictsGroup = dropLayer(conflictsGroup);
|
||||
}
|
||||
function paintConflicts() {
|
||||
if (!map || !conflictsOn) return;
|
||||
const zones = (conflictsCache && Array.isArray(conflictsCache.zones)) ? conflictsCache.zones : [];
|
||||
conflictsGroup = dropLayer(conflictsGroup);
|
||||
const markers = [];
|
||||
for (const z of zones) {
|
||||
if (z.lat == null || z.lon == null) continue;
|
||||
const lat = Number(z.lat), lon = Number(z.lon);
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) continue;
|
||||
const col = conflictSeverityColor(z.severity);
|
||||
const sev = String(z.severity || '').toLowerCase();
|
||||
const radius = sev === 'war' ? 10 : sev === 'high' ? 8 : 7;
|
||||
const m = L.circleMarker([lat, lon], {
|
||||
radius,
|
||||
color: col,
|
||||
fillColor: col,
|
||||
fillOpacity: 0.28,
|
||||
weight: 2,
|
||||
className: 'conflict-zone',
|
||||
});
|
||||
const n = Number(z.eventCount);
|
||||
const count = Number.isFinite(n) ? n : 0;
|
||||
m.bindPopup(
|
||||
`<div class="blip-pop">` +
|
||||
`<div class="blip-src">${esc(z.severity || '')} · ${esc(count)} events</div>` +
|
||||
`<b>${esc(z.label || '')}</b>` +
|
||||
`<div class="blip-desc">${esc(z.description || '')}</div>` +
|
||||
`</div>`
|
||||
);
|
||||
markers.push(m);
|
||||
}
|
||||
conflictsGroup = L.layerGroup(markers).addTo(map);
|
||||
const countEl = document.getElementById('lp-conflicts-count');
|
||||
if (countEl) countEl.textContent = markers.length.toLocaleString();
|
||||
addExtraAttrib('Curated OSINT conflict catalog');
|
||||
}
|
||||
async function probeConflicts() {
|
||||
await loadConflicts(false);
|
||||
}
|
||||
async function toggleConflicts() {
|
||||
const cb = document.getElementById('lp-conflicts-on');
|
||||
conflictsOn = !!(cb && cb.checked);
|
||||
if (conflictsOn) await loadConflicts(true);
|
||||
else conflictsGroup = dropLayer(conflictsGroup);
|
||||
}
|
||||
async function loadConflicts(paint) {
|
||||
const shouldPaint = paint === true || conflictsOn;
|
||||
const req = ++overlayReq.conflicts;
|
||||
const countEl = document.getElementById('lp-conflicts-count');
|
||||
try {
|
||||
if (!conflictsCache) {
|
||||
// Own fetch — catalog is viewport-independent; overlayAbort on
|
||||
// moveend must not cancel this (and we never refetch on pan).
|
||||
const r = await fetch(`${API}/api/conflicts`);
|
||||
if (req !== overlayReq.conflicts) return;
|
||||
if (r.status === 404) {
|
||||
hideConflictsToggle();
|
||||
return;
|
||||
}
|
||||
if (!r.ok) throw new Error('conflicts ' + r.status);
|
||||
const body = await r.json();
|
||||
if (req !== overlayReq.conflicts) return;
|
||||
conflictsCache = body && typeof body === 'object' ? body : { zones: [] };
|
||||
}
|
||||
const zones = Array.isArray(conflictsCache.zones) ? conflictsCache.zones : [];
|
||||
if (countEl) countEl.textContent = zones.length.toLocaleString();
|
||||
if (shouldPaint) paintConflicts();
|
||||
} catch (e) {
|
||||
if (req !== overlayReq.conflicts) return;
|
||||
console.error('Conflicts load failed', e);
|
||||
if (countEl) countEl.textContent = 'err';
|
||||
}
|
||||
}
|
||||
|
||||
/* ═══════════════ INITIAL LOAD ═══════════════ */
|
||||
initNav();
|
||||
initSettings();
|
||||
|
|
|
|||
Loading…
Reference in a new issue