Skip to content

Commit

Permalink
Merge pull request #1374 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
top 100 coders
  • Loading branch information
adnankattekaden authored Oct 19, 2023
2 parents 88a2d26 + 2f023e9 commit 3ae9e6d
Showing 1 changed file with 18 additions and 63 deletions.
81 changes: 18 additions & 63 deletions api/top100_coders/top100_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,26 @@

class Leaderboard(APIView):
def get(self, request):
# query = """
# SELECT u.first_name, u.last_name, SUM(kal.karma) AS total_karma, org.title as org, org.dis, org.state, u.profile_pic, 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
# 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
# ) as org on org.user_id = u.id
# WHERE tl.event = 'TOP100' AND kal.appraiser_approved = TRUE
# GROUP BY u.id
# ORDER BY total_karma DESC, time_
# LIMIT 100;
# """
query = """
SELECT
t.user_id,
u.first_name,
u.last_name,
SUM(kal.karma) AS today_total_karma,
y.yesterday_total_karma,
((t.today_total_karma - y.yesterday_total_karma) / y.yesterday_total_karma) * 100 AS percentage_difference,
org.title AS org,
org.dis,
org.state,
u.profile_pic,
MAX(kal.created_at) AS time_
FROM (
SELECT
u.id AS user_id,
SUM(kal.karma) AS today_total_karma
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
WHERE tl.event = 'TOP100'
AND kal.appraiser_approved = TRUE
AND DATE(kal.created_at) = CURDATE()
GROUP BY u.id
) AS t
JOIN (
SELECT
u.id AS user_id,
SUM(kal.karma) AS yesterday_total_karma
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
WHERE tl.event = 'TOP100'
AND kal.approved = TRUE
AND DATE(kal.created_at) = CURDATE() - INTERVAL 1 DAY
GROUP BY u.id
) AS y ON t.user_id = y.user_id
LEFT JOIN user_organization_link AS uol ON uol.user_id = u.id
LEFT JOIN organization AS org ON org.id = uol.org_id
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
ORDER BY percentage_difference DESC
LIMIT 100;
SELECT u.first_name, u.last_name, SUM(kal.karma) AS total_karma, org.title as org, org.dis, org.state, u.profile_pic, 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
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
) as org on org.user_id = u.id
WHERE tl.event = 'TOP100' AND kal.appraiser_approved = TRUE
GROUP BY u.id
ORDER BY total_karma DESC, time_
LIMIT 100;
"""

with connection.cursor() as cursor:
cursor.execute(query)
results = cursor.fetchall()
Expand Down

0 comments on commit 3ae9e6d

Please sign in to comment.