Skip to content

Commit

Permalink
refactor: improve efficiency of leaderboard ranking query
Browse files Browse the repository at this point in the history
  • Loading branch information
jelanmathewjames committed Jun 11, 2024
1 parent 816ed8b commit 43d3b9b
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions api/launchpad/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from db.user import User
from db.organization import UserOrganizationLink
from db.task import KarmaActivityLog
import json

class LaunchpadLeaderBoardSerializer(serializers.ModelSerializer):
rank = serializers.SerializerMethodField()
Expand Down Expand Up @@ -50,12 +51,14 @@ def get_rank(self, obj):
).annotate(
karma=Subquery(total_karma_subquery, output_field=IntegerField()),
time_=Max("karma_activity_log_user__created_at"),
).order_by("-karma", "time_").annotate(
rank=Window(
expression=Rank(),
order_by=[F("karma").desc(), F("time_").asc()]
)
)

return users.get(id=obj.id).rank
).order_by("-karma", "time_")

# high complexity
rank = 0
for data in users:
rank += 1
if data.id == obj.id:
break

return rank

0 comments on commit 43d3b9b

Please sign in to comment.