上报数据返回截图速度排名
This commit is contained in:
parent
82223971ab
commit
dfa2373008
1 changed files with 11 additions and 15 deletions
26
main.py
26
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
|
||||
|
|
Loading…
Add table
Reference in a new issue