Add CA admin UI and extend admin API for the unified platform.
Deliver parsers, events, analytics, and PI management in Vue; fix Telegram session mount and map navigation to events by eventId. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,16 +1,36 @@
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from ..models import Event, ParseJob
|
||||
from ..models import Consumer, Event, ParseJob
|
||||
from ..schemas import (
|
||||
AnalyticsSummary,
|
||||
ConsumerCreate,
|
||||
ConsumerRead,
|
||||
ConsumerUpdate,
|
||||
EventListResponse,
|
||||
EventRead,
|
||||
ParseJobCreate,
|
||||
ParseJobRead,
|
||||
TimelinePoint,
|
||||
TopItem,
|
||||
)
|
||||
from ..services.analytics import (
|
||||
get_analytics_summary,
|
||||
get_timeline,
|
||||
get_top_localities,
|
||||
get_top_regions,
|
||||
)
|
||||
from ..services.events_query import build_events_query
|
||||
from ..services.filtering import (
|
||||
consumer_to_read,
|
||||
create_consumer,
|
||||
list_consumers,
|
||||
rotate_consumer_key,
|
||||
update_consumer,
|
||||
)
|
||||
from ..services.filtering import create_consumer
|
||||
from ..services.jobs import enqueue_job
|
||||
|
||||
router = APIRouter(prefix="/admin", tags=["admin"])
|
||||
@@ -45,19 +65,81 @@ def get_parse_job(job_id: int, db: Session = Depends(get_db)):
|
||||
return job
|
||||
|
||||
|
||||
@router.get("/events", response_model=list[EventRead])
|
||||
@router.post("/jobs/{job_id}/retry", response_model=ParseJobRead)
|
||||
def retry_parse_job(job_id: int, db: Session = Depends(get_db)):
|
||||
job = db.query(ParseJob).filter(ParseJob.id == job_id).first()
|
||||
if not job:
|
||||
raise HTTPException(status_code=404, detail="Job not found")
|
||||
|
||||
job.status = "queued"
|
||||
job.last_error = None
|
||||
db.commit()
|
||||
db.refresh(job)
|
||||
|
||||
enqueue_job(job.id, job.source_type, job.source_config)
|
||||
return job
|
||||
|
||||
|
||||
@router.get("/events", response_model=EventListResponse)
|
||||
def list_events(
|
||||
limit: int = Query(default=100, ge=1, le=1000),
|
||||
limit: int = Query(default=50, ge=1, le=1000),
|
||||
offset: int = Query(default=0, ge=0),
|
||||
source_type: str | None = None,
|
||||
region: str | None = None,
|
||||
topic: str | None = None,
|
||||
locality: str | None = None,
|
||||
date_from: datetime | None = None,
|
||||
date_to: datetime | None = None,
|
||||
search: str | None = None,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
return (
|
||||
db.query(Event)
|
||||
.order_by(Event.ingested_at.desc())
|
||||
.offset(offset)
|
||||
.limit(limit)
|
||||
.all()
|
||||
query = build_events_query(
|
||||
db,
|
||||
source_type=source_type,
|
||||
region=region,
|
||||
topic=topic,
|
||||
locality=locality,
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
search=search,
|
||||
)
|
||||
total = query.count()
|
||||
items = query.offset(offset).limit(limit).all()
|
||||
return EventListResponse(items=items, total=total)
|
||||
|
||||
|
||||
@router.get("/analytics/summary", response_model=AnalyticsSummary)
|
||||
def analytics_summary(db: Session = Depends(get_db)):
|
||||
return get_analytics_summary(db)
|
||||
|
||||
|
||||
@router.get("/analytics/timeline", response_model=list[TimelinePoint])
|
||||
def analytics_timeline(
|
||||
days: int = Query(default=30, ge=1, le=365),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
return get_timeline(db, days=days)
|
||||
|
||||
|
||||
@router.get("/analytics/top-localities", response_model=list[TopItem])
|
||||
def analytics_top_localities(
|
||||
limit: int = Query(default=10, ge=1, le=100),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
return get_top_localities(db, limit=limit)
|
||||
|
||||
|
||||
@router.get("/analytics/top-regions", response_model=list[TopItem])
|
||||
def analytics_top_regions(
|
||||
limit: int = Query(default=10, ge=1, le=100),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
return get_top_regions(db, limit=limit)
|
||||
|
||||
|
||||
@router.get("/consumers", response_model=list[ConsumerRead])
|
||||
def list_pi_consumers(db: Session = Depends(get_db)):
|
||||
return [consumer_to_read(c) for c in list_consumers(db)]
|
||||
|
||||
|
||||
@router.post("/consumers", response_model=ConsumerRead, status_code=201)
|
||||
@@ -69,10 +151,36 @@ def create_pi_consumer(payload: ConsumerCreate, db: Session = Depends(get_db)):
|
||||
topics=payload.topics,
|
||||
date_from=payload.date_from,
|
||||
)
|
||||
return ConsumerRead(
|
||||
id=consumer.id,
|
||||
name=consumer.name,
|
||||
is_active=consumer.is_active,
|
||||
created_at=consumer.created_at,
|
||||
api_key=api_key,
|
||||
return consumer_to_read(consumer, api_key=api_key)
|
||||
|
||||
|
||||
@router.patch("/consumers/{consumer_id}", response_model=ConsumerRead)
|
||||
def patch_pi_consumer(
|
||||
consumer_id: int,
|
||||
payload: ConsumerUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
consumer = db.query(Consumer).filter(Consumer.id == consumer_id).first()
|
||||
if not consumer:
|
||||
raise HTTPException(status_code=404, detail="Consumer not found")
|
||||
|
||||
consumer = update_consumer(
|
||||
db,
|
||||
consumer,
|
||||
name=payload.name,
|
||||
is_active=payload.is_active,
|
||||
regions=payload.regions,
|
||||
topics=payload.topics,
|
||||
date_from=payload.date_from,
|
||||
)
|
||||
return consumer_to_read(consumer)
|
||||
|
||||
|
||||
@router.post("/consumers/{consumer_id}/rotate-key", response_model=ConsumerRead)
|
||||
def rotate_pi_consumer_key(consumer_id: int, db: Session = Depends(get_db)):
|
||||
consumer = db.query(Consumer).filter(Consumer.id == consumer_id).first()
|
||||
if not consumer:
|
||||
raise HTTPException(status_code=404, detail="Consumer not found")
|
||||
|
||||
consumer, api_key = rotate_consumer_key(db, consumer)
|
||||
return consumer_to_read(consumer, api_key=api_key)
|
||||
|
||||
Reference in New Issue
Block a user