Skip to content

Commit

Permalink
[OPTIM] Leaderboard patches (#1348)
Browse files Browse the repository at this point in the history
  • Loading branch information
MZaFaRM authored Oct 17, 2023
1 parent 49c1417 commit b88a61e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
34 changes: 19 additions & 15 deletions api/leaderboard/leaderboard_view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.db.models import Sum, F, Value, OuterRef, Subquery, Count, Q
from django.db.models import Sum, F, Value, OuterRef, Subquery, Count, Q, Prefetch
from django.db.models.functions import Concat, Coalesce
from rest_framework.views import APIView
import datetime
from . import serializers

from db.organization import Organization, UserOrganizationLink
from db.user import User
Expand All @@ -12,14 +13,6 @@

class StudentsLeaderboard(APIView):
def get(self, request):
user_college = (
UserOrganizationLink.objects.filter(
user_id=OuterRef("id"), org__org_type=OrganizationType.COLLEGE.value
)
.order_by("id")
.values("org__title")[:1]
)

students_leaderboard = (
User.objects.filter(
user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
Expand All @@ -28,14 +21,25 @@ def get(self, request):
active=True,
)
.distinct()
.values(
total_karma=F("wallet_user__karma"),
full_name=Concat(F("first_name"), Value(" "), F("last_name")),
institution=Subquery(user_college),
.select_related("wallet_user")
.prefetch_related(
Prefetch(
"user_organization_link_user",
queryset=UserOrganizationLink.objects.filter(
org__org_type=OrganizationType.COLLEGE.value
).select_related("org"),
to_attr="colleges"
)
)
.order_by("-total_karma")[:20]
.order_by("-wallet_user__karma")[:20]
)
serialized_students_leaderboard = serializers.StudentLeaderboardSerializer(
students_leaderboard, many=True
)
return CustomResponse(response=students_leaderboard).get_success_response()

return CustomResponse(
response=serialized_students_leaderboard.data
).get_success_response()


class StudentsMonthlyLeaderboard(APIView):
Expand Down
26 changes: 20 additions & 6 deletions api/leaderboard/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from db.organization import UserOrganizationLink
from db.task import KarmaActivityLog
from db.user import User
from utils.types import OrganizationType
from utils.utils import DateTimeUtils

Expand Down Expand Up @@ -46,12 +47,12 @@ def get_active_members(self, obj):

def get_total_karma(self, obj):
return (
obj.org.user_organization_link_org.filter(
org__org_type=OrganizationType.COLLEGE.value,
verified=True,
user__wallet_user__isnull=False,
).aggregate(total_karma=Sum("user__wallet_user__karma"))["total_karma"]
or 0
obj.org.user_organization_link_org.filter(
org__org_type=OrganizationType.COLLEGE.value,
verified=True,
user__wallet_user__isnull=False,
).aggregate(total_karma=Sum("user__wallet_user__karma"))["total_karma"]
or 0
)

def get_rank(self, obj):
Expand All @@ -74,3 +75,16 @@ def get_rank(self, obj):
keys_list = list(sorted_rank_dict.keys())
position = keys_list.index(obj.org.id)
return position + 1


class StudentLeaderboardSerializer(serializers.ModelSerializer):
institution = serializers.SerializerMethodField()
total_karma = serializers.IntegerField(source="wallet_user.karma", default=0)
full_name = serializers.CharField(source="fullname")

def get_institution(self, user):
return user.colleges[0].org.title if user.colleges else None

class Meta:
model = User
fields = ["full_name", "total_karma", "institution"]

0 comments on commit b88a61e

Please sign in to comment.