feat: news ticker and map API contracts

This commit is contained in:
Sirius DevOps 2026-08-27 22:49:59 -04:00
commit 5b23578302
3 changed files with 230 additions and 12 deletions

View file

@ -31,12 +31,13 @@ from sqlalchemy.ext.asyncio import AsyncSession
from database import async_session, init_extensions
from models import (
alerts, documents, entities, entity_events, events, feed_sources, fires,
articles, article_summaries,
articles, article_summaries, news_items,
)
from schemas import (
AlertCreate, AlertOut, AlertSeverity, AlertType, AlertUpdate,
DashboardSummary, EntityCreate, EntityKind, EntityOut,
EventCreate, EventOut, FireOut, NewsArticleOut, NewsSummaryOut,
EventCreate, EventOut, FireOut, NewsArticleOut, NewsMapItemOut,
NewsSummaryOut, NewsTickerItemOut,
FeedSourceCreate, FeedSourceOut,
KeyOut, KeyValueIn,
SearchResult, SentimentSummary, SourceType,
@ -1128,7 +1129,102 @@ async def list_news_summaries(
return [
NewsSummaryOut(
id=r["id"], summary_text=r["summary_text"],
batch_timestamp=r["batch_timestamp"],
batch_timestamp=r["batch_timestamp"], model=r["model"],
)
for r in rows
]
_FLAGGED = ("critical", "high")
@app.get("/api/news/ticker", response_model=list[NewsTickerItemOut])
async def list_news_ticker(
since: datetime | None = Query(
None,
description="Only ticker items created at/after this UTC instant.",
),
limit: int = Query(20, ge=1, le=50),
):
"""Flagged ticker rows (critical/high), newest first. No LLM required."""
async with async_session() as session:
stmt = (
select(news_items)
.where(
news_items.c.kind == "ticker",
news_items.c.importance.in_(_FLAGGED),
)
.order_by(news_items.c.created_at.desc())
)
if since:
stmt = stmt.where(news_items.c.created_at >= since)
stmt = stmt.limit(limit)
rows = (await session.execute(stmt)).mappings().all()
return [
NewsTickerItemOut(
id=r["id"], headline=r["headline"], importance=r["importance"],
location_name=r["location_name"], url=r["url"],
created_at=r["created_at"],
)
for r in rows
]
@app.get("/api/news/map", response_model=list[NewsMapItemOut])
async def list_news_map(
bbox: str | None = Query(
None,
description="Comma-separated 'minlon,minlat,maxlon,maxlat' to bound the "
"result set by item coordinates. Omit for all flagged pins.",
),
since: datetime | None = Query(
None,
description="Only map items created at/after this UTC instant. "
"Defaults to the last 24 hours.",
),
limit: int = Query(200, ge=1, le=500),
):
"""Flagged map pins (critical/high with coords). No zoom skip — world view."""
if since is None:
since = datetime.now(timezone.utc) - timedelta(hours=24)
async with async_session() as session:
stmt = (
select(news_items)
.where(
news_items.c.kind == "map",
news_items.c.lat.isnot(None),
news_items.c.lon.isnot(None),
news_items.c.importance.in_(_FLAGGED),
news_items.c.created_at >= since,
)
.order_by(news_items.c.created_at.desc())
)
if bbox:
parts = [p.strip() for p in bbox.split(",")]
if len(parts) != 4:
raise HTTPException(
422, "bbox must be 'minlon,minlat,maxlon,maxlat' (4 comma-separated values)"
)
try:
minlon, minlat, maxlon, maxlat = (float(p) for p in parts)
except ValueError:
raise HTTPException(
422, "bbox values must be floats: 'minlon,minlat,maxlon,maxlat'"
)
stmt = stmt.where(
and_(
news_items.c.lon >= minlon, news_items.c.lon <= maxlon,
news_items.c.lat >= minlat, news_items.c.lat <= maxlat,
)
)
stmt = stmt.limit(limit)
rows = (await session.execute(stmt)).mappings().all()
return [
NewsMapItemOut(
id=r["id"], headline=r["headline"], importance=r["importance"],
location_name=r["location_name"], lat=r["lat"], lon=r["lon"],
location_confidence=r["location_confidence"], category=r["category"],
url=r["url"], created_at=r["created_at"],
)
for r in rows
]