perf: slim fires/cameras/events/news map payloads
Heatmap uses {lat,lon,i,c}. Camera list drops URLs (detail via GET
/api/cameras/{id}). Event blips skip body. News omits content unless
include_content=true.
This commit is contained in:
parent
9c14b899ee
commit
8f89994201
3 changed files with 131 additions and 24 deletions
96
app/main.py
96
app/main.py
|
|
@ -69,17 +69,17 @@ def event_to_out(row: dict) -> EventOut:
|
|||
return EventOut(
|
||||
id=row["id"],
|
||||
source_type=row["source_type"],
|
||||
source_id=row["source_id"],
|
||||
title=row["title"],
|
||||
body=row["body"],
|
||||
url=row["url"],
|
||||
sentiment_score=row["sentiment_score"],
|
||||
sentiment_label=row["sentiment_label"],
|
||||
location_lat=row["location_lat"],
|
||||
location_lon=row["location_lon"],
|
||||
location_name=row["location_name"],
|
||||
entities=row["entities"],
|
||||
tags=row["tags"],
|
||||
source_id=row.get("source_id"),
|
||||
title=row.get("title"),
|
||||
body=row.get("body"),
|
||||
url=row.get("url"),
|
||||
sentiment_score=row.get("sentiment_score"),
|
||||
sentiment_label=row.get("sentiment_label"),
|
||||
location_lat=row.get("location_lat"),
|
||||
location_lon=row.get("location_lon"),
|
||||
location_name=row.get("location_name"),
|
||||
entities=row.get("entities"),
|
||||
tags=row.get("tags"),
|
||||
ingested_at=row["ingested_at"],
|
||||
source_timestamp=row["source_timestamp"],
|
||||
)
|
||||
|
|
@ -218,7 +218,15 @@ async def list_events(
|
|||
):
|
||||
"""List recent ingested events."""
|
||||
async with async_session() as session:
|
||||
stmt = select(events).order_by(events.c.ingested_at.desc())
|
||||
if has_coords:
|
||||
stmt = select(
|
||||
events.c.id, events.c.source_type, events.c.source_id,
|
||||
events.c.title, events.c.url,
|
||||
events.c.location_lat, events.c.location_lon, events.c.location_name,
|
||||
events.c.ingested_at, events.c.source_timestamp,
|
||||
).order_by(events.c.ingested_at.desc())
|
||||
else:
|
||||
stmt = select(events).order_by(events.c.ingested_at.desc())
|
||||
if source_type:
|
||||
stmt = stmt.where(events.c.source_type == source_type.value)
|
||||
if since:
|
||||
|
|
@ -272,7 +280,29 @@ async def create_event(payload: EventCreate):
|
|||
|
||||
# ── Active Fires / Hotspots (NASA FIRMS) ─────────────────────────────────
|
||||
|
||||
@app.get("/api/fires", response_model=list[FireOut])
|
||||
def fire_heat_row(r) -> dict:
|
||||
"""Minimal FIRMS point for the heatmap overlay."""
|
||||
return {
|
||||
"lat": r["latitude"],
|
||||
"lon": r["longitude"],
|
||||
"i": r["brightness"],
|
||||
"c": r["confidence"],
|
||||
}
|
||||
|
||||
|
||||
def camera_map_row(r) -> dict:
|
||||
"""Minimal camera pin — URLs stay off the list payload."""
|
||||
return {
|
||||
"id": str(r["id"]),
|
||||
"lat": r["location_lat"],
|
||||
"lon": r["location_lon"],
|
||||
"device_type": r["device_type"],
|
||||
"discovery_source": r["discovery_source"],
|
||||
"location_name": r["location_name"],
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/fires", response_model=None)
|
||||
async def list_fires(
|
||||
bbox: str | None = Query(
|
||||
None,
|
||||
|
|
@ -286,12 +316,16 @@ async def list_fires(
|
|||
"(ISO 8601, e.g. '2026-08-24T12:00:00Z').",
|
||||
),
|
||||
limit: int = Query(2000, ge=1, le=10000),
|
||||
format: str = Query("full", description="'full' FireOut rows or 'heat' {lat,lon,i,c}"),
|
||||
):
|
||||
"""List stored FIRMS active fire/hotspot detections as JSON.
|
||||
|
||||
This is the data contract for the map's fire heatmap overlay: the frontend
|
||||
calls `GET /api/fires?bbox=...&since=...` and renders the returned points.
|
||||
calls `GET /api/fires?bbox=...&since=...&format=heat` and renders the points.
|
||||
"""
|
||||
fmt = (format or "full").lower()
|
||||
if fmt not in ("full", "heat"):
|
||||
raise HTTPException(422, "format must be 'full' or 'heat'")
|
||||
async with async_session() as session:
|
||||
stmt = select(fires).order_by(fires.c.acq_time.desc())
|
||||
if since:
|
||||
|
|
@ -316,6 +350,8 @@ async def list_fires(
|
|||
)
|
||||
stmt = stmt.limit(limit)
|
||||
rows = (await session.execute(stmt)).mappings().all()
|
||||
if fmt == "heat":
|
||||
return [fire_heat_row(r) for r in rows]
|
||||
return [
|
||||
FireOut(
|
||||
latitude=r["latitude"], longitude=r["longitude"],
|
||||
|
|
@ -798,7 +834,11 @@ async def list_cameras(
|
|||
stmt = stmt.where(cam_table.c.discovery_source == source)
|
||||
rows = (await session.execute(stmt.limit(limit))).mappings().all()
|
||||
|
||||
return [{
|
||||
return [camera_map_row(r) for r in rows]
|
||||
|
||||
|
||||
def camera_detail_row(r) -> dict:
|
||||
return {
|
||||
"id": str(r["id"]),
|
||||
"source_url": r["source_url"],
|
||||
"snapshot_url": r["snapshot_url"],
|
||||
|
|
@ -808,9 +848,23 @@ async def list_cameras(
|
|||
"location_name": r["location_name"],
|
||||
"vendor": r["vendor"],
|
||||
"device_type": r["device_type"],
|
||||
"first_seen": r["first_seen"].isoformat(),
|
||||
"last_seen": r["last_seen"].isoformat(),
|
||||
} for r in rows]
|
||||
"first_seen": r["first_seen"].isoformat() if r["first_seen"] else None,
|
||||
"last_seen": r["last_seen"].isoformat() if r["last_seen"] else None,
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/cameras/{camera_id}")
|
||||
async def get_camera(camera_id: UUID):
|
||||
"""Full camera row for a map popup. List endpoint stays slim."""
|
||||
from camera_models import cameras as cam_table
|
||||
|
||||
async with async_session() as session:
|
||||
row = (await session.execute(
|
||||
select(cam_table).where(cam_table.c.id == camera_id)
|
||||
)).mappings().one_or_none()
|
||||
if not row:
|
||||
raise HTTPException(404, "Camera not found")
|
||||
return camera_detail_row(row)
|
||||
|
||||
|
||||
@app.get("/api/cameras/{camera_id}/snapshot")
|
||||
|
|
@ -959,6 +1013,10 @@ async def list_news(
|
|||
),
|
||||
limit: int = Query(50, ge=1, le=500),
|
||||
offset: int = Query(0, ge=0),
|
||||
include_content: bool = Query(
|
||||
False,
|
||||
description="Include full article body. Default false — ticker/list only need title/url.",
|
||||
),
|
||||
):
|
||||
"""Most recent scraped news articles (newest first)."""
|
||||
async with async_session() as session:
|
||||
|
|
@ -974,7 +1032,7 @@ async def list_news(
|
|||
return [
|
||||
NewsArticleOut(
|
||||
id=r["id"], title=r["title"], url=r["url"],
|
||||
content=r["content"], domain=r["domain"],
|
||||
content=r["content"] if include_content else None, domain=r["domain"],
|
||||
timestamp=r["timestamp"],
|
||||
)
|
||||
for r in rows
|
||||
|
|
|
|||
Loading…
Reference in a new issue