mirror of
https://github.com/aljazceru/CTFd.git
synced 2025-12-17 14:04:20 +01:00
Improve speed of the ChallengeSolves API endpoint (#2046)
* Improve speed of the ChallengeSolves API (`/api/v1/challenges/[challenge_id]/solves`) endpoint
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
# UNRELEASED
|
||||||
|
|
||||||
|
- Improved speed of the `/api/v1/challenges/[challenge_id]/solves` endpoint
|
||||||
|
|
||||||
# 3.4.0 / 2021-08-11
|
# 3.4.0 / 2021-08-11
|
||||||
|
|
||||||
**General**
|
**General**
|
||||||
|
|||||||
@@ -764,8 +764,13 @@ class ChallengeSolves(Resource):
|
|||||||
|
|
||||||
Model = get_model()
|
Model = get_model()
|
||||||
|
|
||||||
|
# Note that we specifically query for the Solves.account.name
|
||||||
|
# attribute here because it is faster than having SQLAlchemy
|
||||||
|
# query for the attribute directly and it's unknown what the
|
||||||
|
# affects of changing the relationship lazy attribute would be
|
||||||
solves = (
|
solves = (
|
||||||
Solves.query.join(Model, Solves.account_id == Model.id)
|
Solves.query.add_columns(Model.name.label("account_name"))
|
||||||
|
.join(Model, Solves.account_id == Model.id)
|
||||||
.filter(
|
.filter(
|
||||||
Solves.challenge_id == challenge_id,
|
Solves.challenge_id == challenge_id,
|
||||||
Model.banned == False,
|
Model.banned == False,
|
||||||
@@ -782,10 +787,12 @@ class ChallengeSolves(Resource):
|
|||||||
solves = solves.filter(Solves.date < dt)
|
solves = solves.filter(Solves.date < dt)
|
||||||
|
|
||||||
for solve in solves:
|
for solve in solves:
|
||||||
|
# Seperate out the account name and the Solve object from the SQLAlchemy tuple
|
||||||
|
solve, account_name = solve
|
||||||
response.append(
|
response.append(
|
||||||
{
|
{
|
||||||
"account_id": solve.account_id,
|
"account_id": solve.account_id,
|
||||||
"name": solve.account.name,
|
"name": account_name,
|
||||||
"date": isoformat(solve.date),
|
"date": isoformat(solve.date),
|
||||||
"account_url": generate_account_url(account_id=solve.account_id),
|
"account_url": generate_account_url(account_id=solve.account_id),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user