From dfa23730084b6c61641d403594127e4673443d6d Mon Sep 17 00:00:00 2001 From: zhbaor Date: Wed, 1 Jan 2025 10:07:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E6=8A=A5=E6=95=B0=E6=8D=AE=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=88=AA=E5=9B=BE=E9=80=9F=E5=BA=A6=E6=8E=92=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 8d26924..dc5443f 100644 --- a/main.py +++ b/main.py @@ -64,8 +64,16 @@ async def lifespan(app: FastAPI): app = FastAPI(lifespan=lifespan) -@app.post("/stat", response_model=SystemData) -def upload(data: PostData, session: SessionDep): +@app.post("/stat") +def upload(data: PostData, session: SessionDep) -> int: + 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 > data.avg_time: + slower += 1 + ratio = round(slower / total * 100) if total > 0 else 0 user_list = session.exec(select(User).where(User.name == data.name)) if (user_db := user_list.one_or_none()) is None: user_db = User.model_validate(data) @@ -76,7 +84,7 @@ def upload(data: PostData, session: SessionDep): system_db.user_id = user_db.id session.add(system_db) session.commit() - return data + return ratio @app.get("/user/today") @@ -95,15 +103,3 @@ def user_week(session: SessionDep) -> int: 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