Add initial project files
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
41
backend/app/routers/public.py
Normal file
41
backend/app/routers/public.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.dependencies import get_db
|
||||
from app.models.user import User
|
||||
from app.models.entry import Entry, EntryType
|
||||
from app.schemas.entry import PublicUserStats
|
||||
|
||||
router = APIRouter(prefix="/api/public", tags=["public"])
|
||||
|
||||
|
||||
@router.get("/stats", response_model=list[PublicUserStats])
|
||||
async def public_stats(db: AsyncSession = Depends(get_db)):
|
||||
users_result = await db.execute(select(User))
|
||||
users = users_result.scalars().all()
|
||||
|
||||
stats: list[PublicUserStats] = []
|
||||
for user in users:
|
||||
entries_result = await db.execute(select(Entry).where(Entry.user_id == user.id))
|
||||
entries = entries_result.scalars().all()
|
||||
|
||||
adds = [e for e in entries if e.entry_type == EntryType.add]
|
||||
removes = [e for e in entries if e.entry_type == EntryType.remove]
|
||||
|
||||
total_add_shots = sum(e.amount_shots for e in adds)
|
||||
total_remove_shots = sum(e.amount_shots for e in removes)
|
||||
current_total = total_add_shots - total_remove_shots
|
||||
|
||||
weighted_proof_sum = sum(e.proof * e.amount_shots for e in adds if e.proof is not None)
|
||||
proof_shot_total = sum(e.amount_shots for e in adds if e.proof is not None)
|
||||
estimated_proof = round(weighted_proof_sum / proof_shot_total, 1) if proof_shot_total > 0 else None
|
||||
|
||||
stats.append(PublicUserStats(
|
||||
display_name=user.display_name or user.email.split("@")[0],
|
||||
total_add_entries=len(adds),
|
||||
current_total_shots=round(current_total, 2),
|
||||
estimated_proof=estimated_proof,
|
||||
))
|
||||
|
||||
return stats
|
||||
Reference in New Issue
Block a user