5
0
Fork 0

基础的查询功能

This commit is contained in:
zhbaor 2024-12-31 22:32:16 +08:00
parent 9d3ecd2b0d
commit 82223971ab

32
main.py
View file

@ -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