Add Reddit monitoring bot — backend, frontend, and Docker config

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>
This commit is contained in:
2026-03-09 19:29:58 -05:00
parent aaa240dbf0
commit bc2203524f
76 changed files with 7570 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
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")