5
0
Fork 0

上报数据返回截图速度排名

This commit is contained in:
zhbaor 2025-01-01 10:07:26 +08:00
parent 82223971ab
commit dfa2373008

26
main.py
View file

@ -64,8 +64,16 @@ async def lifespan(app: FastAPI):
app = FastAPI(lifespan=lifespan) app = FastAPI(lifespan=lifespan)
@app.post("/stat", response_model=SystemData) @app.post("/stat")
def upload(data: PostData, session: SessionDep): 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)) user_list = session.exec(select(User).where(User.name == data.name))
if (user_db := user_list.one_or_none()) is None: if (user_db := user_list.one_or_none()) is None:
user_db = User.model_validate(data) user_db = User.model_validate(data)
@ -76,7 +84,7 @@ def upload(data: PostData, session: SessionDep):
system_db.user_id = user_db.id system_db.user_id = user_db.id
session.add(system_db) session.add(system_db)
session.commit() session.commit()
return data return ratio
@app.get("/user/today") @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)): for system in session.exec(select(System).where(System.upload_time > last_week)):
user_set.add(system.user_id) user_set.add(system.user_id)
return len(user_set) 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