Skip to content

Commit 6535fe9

Browse files
authored
[Backend] Add submissions metrics by PK (Cloud-CV#4353)
1 parent 68c1c3a commit 6535fe9

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

apps/challenges/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
views.get_all_challenges_submission_metrics,
5050
name="get_all_challenges_submission_metrics",
5151
),
52+
url(
53+
r"^challenge/get_submission_metrics_by_pk/(?P<pk>[0-9]+)/$",
54+
views.get_challenge_submission_metrics_by_pk,
55+
name="get_challenge_submission_metrics_by_pk",
56+
),
5257
url(
5358
r"^challenges/participated/(?P<challenge_time>[A-Za-z]+)/$",
5459
views.get_all_participated_challenges,

apps/challenges/views.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,30 @@ def get_all_challenges_submission_metrics(request):
753753
return Response(submission_metrics, status=status.HTTP_200_OK)
754754

755755

756+
@api_view(["GET"])
757+
@throttle_classes([AnonRateThrottle])
758+
def get_challenge_submission_metrics_by_pk(request, pk):
759+
"""
760+
Returns the submission metrics for a given challenge by primary key and their phases
761+
"""
762+
if not is_user_a_staff(request.user):
763+
response_data = {"error": "Sorry, you are not authorized to make this request"}
764+
return Response(response_data, status=status.HTTP_403_FORBIDDEN)
765+
challenge = get_challenge_model(pk)
766+
challenge_phases = ChallengePhase.objects.filter(challenge=challenge)
767+
submission_metrics = {}
768+
769+
submission_statuses = [status[0] for status in Submission.STATUS_OPTIONS]
770+
771+
# Fetch challenge phases for the challenge
772+
challenge_phases = ChallengePhase.objects.filter(challenge=challenge)
773+
for submission_status in submission_statuses:
774+
count = Submission.objects.filter(challenge_phase__in=challenge_phases, status=submission_status).count()
775+
submission_metrics[submission_status] = count
776+
777+
return Response(submission_metrics, status=status.HTTP_200_OK)
778+
779+
756780
@api_view(["GET"])
757781
@throttle_classes([AnonRateThrottle])
758782
def get_featured_challenges(request):

0 commit comments

Comments
 (0)