From 82223971ab1a2c021710e6f5319912f5b6008c6a Mon Sep 17 00:00:00 2001 From: zhbaor Date: Tue, 31 Dec 2024 22:32:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E7=9A=84=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) 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