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:
Kevin Chung
2022-02-09 21:03:44 -05:00
committed by GitHub
parent f89fcea1e2
commit 27e77368c8
2 changed files with 13 additions and 2 deletions

View File

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

View File

@@ -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),
} }