Python/FastAPI backend with PostgreSQL for collecting Reddit data via public .json endpoints. React/Vite dashboard for analytics. Docker Compose setup with API and worker services connecting to shared PostgreSQL. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
from datetime import datetime
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from backend.database import get_db
|
|
from backend.services import analytics_service
|
|
|
|
router = APIRouter(prefix="/analytics", tags=["analytics"])
|
|
|
|
|
|
@router.get("/engagement")
|
|
async def engagement(
|
|
subreddit_id: int | None = None,
|
|
granularity: str = Query("day", pattern="^(hour|day|week)$"),
|
|
since: datetime | None = None,
|
|
until: datetime | None = None,
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
return await analytics_service.get_engagement(db, subreddit_id, granularity, since, until)
|
|
|
|
|
|
@router.get("/top-posts")
|
|
async def top_posts(
|
|
subreddit_id: int | None = None,
|
|
metric: str = Query("score", pattern="^(score|num_comments)$"),
|
|
since: datetime | None = None,
|
|
until: datetime | None = None,
|
|
limit: int = Query(10, ge=1, le=50),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
return await analytics_service.get_top_posts(db, subreddit_id, metric, since, until, limit)
|
|
|
|
|
|
@router.get("/top-authors")
|
|
async def top_authors(
|
|
subreddit_id: int | None = None,
|
|
since: datetime | None = None,
|
|
until: datetime | None = None,
|
|
limit: int = Query(10, ge=1, le=50),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
return await analytics_service.get_top_authors(db, subreddit_id, since, until, limit)
|
|
|
|
|
|
@router.get("/subreddit-summary")
|
|
async def subreddit_summary(
|
|
since: datetime | None = None,
|
|
until: datetime | None = None,
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
return await analytics_service.get_subreddit_summary(db, since, until)
|
|
|
|
|
|
@router.get("/flair-distribution")
|
|
async def flair_distribution(
|
|
subreddit_id: int = Query(...),
|
|
since: datetime | None = None,
|
|
until: datetime | None = None,
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
return await analytics_service.get_flair_distribution(db, subreddit_id, since, until)
|