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>
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
import logging
|
|
from datetime import datetime, timezone, timedelta
|
|
|
|
from sqlalchemy import select, create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from backend.config import settings
|
|
from backend.models.post import Post
|
|
from backend.models.metric_snapshot import MetricSnapshot
|
|
from backend.models.subreddit import MonitoredSubreddit
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_engine = create_engine(settings.database_url_sync, pool_size=2, pool_recycle=3600)
|
|
SyncSession = sessionmaker(_engine)
|
|
|
|
|
|
def take_metric_snapshots():
|
|
"""Snapshot current metrics for recent posts."""
|
|
now = datetime.now(timezone.utc)
|
|
|
|
with SyncSession() as db:
|
|
# Posts < 48h old: snapshot every run (every 30 min)
|
|
cutoff_recent = now - timedelta(hours=48)
|
|
stmt = (
|
|
select(Post.id, Post.score, Post.num_comments, Post.upvote_ratio)
|
|
.join(MonitoredSubreddit)
|
|
.where(
|
|
MonitoredSubreddit.is_active == True, # noqa: E712
|
|
Post.created_utc >= cutoff_recent,
|
|
)
|
|
)
|
|
result = db.execute(stmt)
|
|
snapshots = []
|
|
for post_id, score, num_comments, upvote_ratio in result:
|
|
snapshots.append(MetricSnapshot(
|
|
post_id=post_id,
|
|
score=score,
|
|
num_comments=num_comments,
|
|
upvote_ratio=upvote_ratio,
|
|
snapshot_at=now,
|
|
))
|
|
|
|
if snapshots:
|
|
db.add_all(snapshots)
|
|
db.commit()
|
|
logger.info(f"Took {len(snapshots)} metric snapshots")
|