-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1965 from jelanmathewjames/dev
refactor: optimize launchpad leaderboard API query
- Loading branch information
Showing
2 changed files
with
52 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,46 @@ | ||
from django.db import connection | ||
from django.db.models import Sum, Max, F | ||
|
||
from rest_framework.views import APIView | ||
|
||
from launchpad.serializers import LaunchpadLeaderBoardSerializer | ||
from utils.response import CustomResponse | ||
from utils.utils import CommonUtils | ||
from db.user import User | ||
|
||
|
||
class Leaderboard(APIView): | ||
def get(self, request): | ||
query = """ | ||
SELECT | ||
u.id, | ||
u.full_name, | ||
SUM(kal.karma) AS karma, | ||
COALESCE(org.title, comm.title) AS org, | ||
COALESCE(org.dis, d.name) AS district, | ||
COALESCE(org.state, s.name) AS state, | ||
MAX(kal.created_at) AS time_ | ||
FROM karma_activity_log AS kal | ||
INNER JOIN user AS u ON kal.user_id = u.id | ||
INNER JOIN task_list AS tl ON tl.id = kal.task_id | ||
LEFT JOIN ( | ||
SELECT | ||
uol.user_id, | ||
org.id, | ||
org.title AS title, | ||
d.name dis, | ||
s.name state | ||
FROM user_organization_link AS uol | ||
INNER JOIN organization AS org ON org.id = uol.org_id AND org.org_type IN ("College","School","Company") | ||
LEFT JOIN district AS d ON d.id = org.district_id | ||
LEFT JOIN zone AS z ON z.id = d.zone_id | ||
LEFT JOIN state AS s ON s.id = z.state_id | ||
GROUP BY uol.user_id, org.id, org.title, d.name, s.name | ||
) AS org ON org.user_id = u.id | ||
LEFT JOIN ( | ||
SELECT | ||
uol.user_id, | ||
org.id, | ||
org.title AS title | ||
FROM organization AS org | ||
INNER JOIN user_organization_link AS uol ON org.id = uol.org_id AND org.org_type IN ("Community") | ||
GROUP BY uol.user_id, org.id, org.title | ||
) AS comm ON comm.user_id = u.id | ||
LEFT JOIN district AS d ON d.id = u.district_id | ||
LEFT JOIN zone AS z ON d.zone_id = z.id | ||
LEFT JOIN state AS s ON z.state_id = s.id | ||
WHERE | ||
tl.event = "launchpad" AND | ||
kal.appraiser_approved = TRUE | ||
GROUP BY u.id, u.full_name, org.title, comm.title, COALESCE(org.dis, d.name), COALESCE(org.state, s.name) | ||
ORDER BY karma DESC, time_; | ||
""" | ||
with connection.cursor() as cursor: | ||
cursor.execute(query) | ||
results = cursor.fetchall() | ||
column_names = [desc[0] for desc in cursor.description] | ||
user_ids = set() | ||
list_of_dicts = [] | ||
for row in results: | ||
if row[0] not in user_ids: | ||
user_ids.add(row[0]) | ||
list_of_dicts.append(dict(zip(column_names, row))) | ||
return CustomResponse(response=list_of_dicts).get_success_response() | ||
users = ( | ||
User.objects.filter( | ||
karma_activity_log__task__event="launchpad", | ||
karma_activity_log__appraiser_approved=True, | ||
karma_activity_log__task__hashtag="#lp24-introduction", | ||
) | ||
.annotate( | ||
karma=Sum("karma_activity_log__karma"), | ||
org=F("userorganizationlink__title"), | ||
district=F("district__name"), | ||
state=F("district__zone__state__name"), | ||
time_=Max("karma_activity_log__created_at"), | ||
) | ||
.order_by("-karma", "time_") | ||
) | ||
paginated_queryset = CommonUtils.get_paginated_queryset( | ||
users, | ||
request, | ||
["karma", "org", "district", "state", "time_"], | ||
sort_fields={ | ||
"karma": "karma", | ||
"org": "org", | ||
"district": "district", | ||
"state": "state", | ||
"time_": "time_", | ||
}, | ||
) | ||
|
||
serializer = LaunchpadLeaderBoardSerializer( | ||
paginated_queryset.get("queryset"), many=True | ||
) | ||
return CustomResponse.paginated_response( | ||
data=serializer.data, pagination=paginated_queryset.get("pagination") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from rest_framework import serializers | ||
|
||
from db.user import User | ||
|
||
class LaunchpadLeaderBoardSerializer(serializers.ModelSerializer): | ||
karma = serializers.IntegerField() | ||
org = serializers.CharField() | ||
district = serializers.CharField() | ||
state = serializers.CharField() | ||
|
||
class Meta: | ||
model = User | ||
fields = "__all__" |