Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Reduce API calls needed to get last activity date
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Jul 10, 2021
1 parent ca82f26 commit a09ca29
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions buttercup/cogs/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,13 @@ async def _user_stats(
volunteer_data = volunteer_response.data

# Get the date of last activity
# TODO: Make this more efficient, e.g. by ordering it by complete_time
submission_response = self.blossom_api.get(
"submission/",
params={"completed_by": volunteer_data["id"], "page_size": 1, "page": 1},
)
if submission_response.status_code != 200:
await msg.edit(
content=i18n["user_stats"]["failed_getting_stats"].format(user=user)
)
return
last_page = submission_response.json()["count"]
submission_response = self.blossom_api.get(
"submission/",
params={
"completed_by": volunteer_data["id"],
"ordering": "-complete_time",
"page_size": 1,
"page": last_page,
"page": 1,
},
)
if submission_response.status_code != 200:
Expand All @@ -124,7 +114,12 @@ async def _user_stats(
submission_data = submission_response.json()["results"][0]

date_joined = parse(volunteer_data["date_joined"])
last_active = parse(submission_data["complete_time"])
# For some reason, the complete_time is sometimes None, so we have to fall back
last_active = parse(
submission_data["complete_time"]
or submission_data["claim_time"]
or submission_data["create_time"]
)

description = i18n["user_stats"]["embed_description"].format(
gamma=volunteer_data["gamma"],
Expand Down

0 comments on commit a09ca29

Please sign in to comment.