diff --git a/main.py b/main.py index 75e6eda..8d26924 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,5 @@ from contextlib import asynccontextmanager -from datetime import datetime, timezone +from datetime import datetime, timedelta, timezone from typing import Annotated from fastapi import Depends, FastAPI @@ -77,3 +77,33 @@ def upload(data: PostData, session: SessionDep): session.add(system_db) session.commit() return data + + +@app.get("/user/today") +def user_today(session: SessionDep) -> int: + user_set = set() + yesterday = datetime.now(timezone.utc) - timedelta(days=1) + for system in session.exec(select(System).where(System.upload_time > yesterday)): + user_set.add(system.user_id) + return len(user_set) + + +@app.get("/user/week") +def user_week(session: SessionDep) -> int: + user_set = set() + last_week = datetime.now(timezone.utc) - timedelta(weeks=1) + for system in session.exec(select(System).where(System.upload_time > last_week)): + user_set.add(system.user_id) + return len(user_set) + + +@app.get("/screenshot/{screenshot_time}") +def screenshot_rank(screenshot_time: int, session: SessionDep) -> float: + yesterday = datetime.now(timezone.utc) - timedelta(days=1) + total = 0 + slower = 0 + for system in session.exec(select(System).where(System.upload_time > yesterday)): + total += 1 + if system.avg_time > screenshot_time: + slower += 1 + return slower / total if total > 0 else 0