Skip to content

Commit

Permalink
Merge pull request #1905 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
Launchpad leaderboard api
  • Loading branch information
jelanmathewjames authored Apr 15, 2024
2 parents 449f540 + da5727f commit 450275f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
3 changes: 2 additions & 1 deletion api/common/common_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def get(self, request, circle_id):
return CustomResponse(response=serializer.data).get_success_response()

class LcListAPI(APIView):
def get(self, request):
def get(self, request, ig, district):
all_circles = LearningCircle.objects.all()

ig = request.query_params.get("ig")
org = request.query_params.get("org")
district = request.query_params.get("district")
Expand Down
59 changes: 59 additions & 0 deletions api/launchpad/launchpad_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from django.db import connection
from rest_framework.views import APIView

from utils.response import CustomResponse


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_
LIMIT 30;
"""
with connection.cursor() as cursor:
cursor.execute(query)
results = cursor.fetchall()
column_names = [desc[0] for desc in cursor.description]
list_of_dicts = [dict(zip(column_names, row)) for row in results]
return CustomResponse(response=list_of_dicts).get_success_response()
7 changes: 7 additions & 0 deletions api/launchpad/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path

from . import launchpad_views

urlpatterns = [
path('leaderboard/', launchpad_views.Leaderboard.as_view()),
]
2 changes: 1 addition & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
path('notification/', include('api.notification.urls')),
path('public/', include('api.common.urls')),
path('top100/', include('api.top100_coders.urls')),

path('launchpad/', include('api.launchpad.urls')),
path("__debug__/", include(debug_toolbar.urls)),
]

0 comments on commit 450275f

Please sign in to comment.