From 65a5c462eb415dd7360e49ac95366735ace5a1b8 Mon Sep 17 00:00:00 2001 From: Ihsan Ullah Date: Thu, 7 Nov 2024 15:36:26 +0500 Subject: [PATCH 1/4] commented dynamic fetching of comp, users, submissions --- src/apps/pages/views.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/apps/pages/views.py b/src/apps/pages/views.py index c0042ed98..22f4a98fc 100644 --- a/src/apps/pages/views.py +++ b/src/apps/pages/views.py @@ -15,15 +15,24 @@ class HomeView(TemplateView): def get_context_data(self, *args, **kwargs): context = super().get_context_data(*args, **kwargs) - data = Competition.objects.aggregate( - count=Count('*'), - published_comps=Count('pk', filter=Q(published=True)), - unpublished_comps=Count('pk', filter=Q(published=False)), - ) - - public_competitions = data['published_comps'] - users = User.objects.all().count() - submissions = Submission.objects.all().count() + # TODO: Optimize fetching the statistics + # Possibly from a file where they are written by an automated script once a day + # For now showing latest numbers from live codabench + # The following commented code is slowing down the loading of the home page + + # data = Competition.objects.aggregate( + # count=Count('*'), + # published_comps=Count('pk', filter=Q(published=True)), + # unpublished_comps=Count('pk', filter=Q(published=False)), + # ) + + # public_competitions = data['published_comps'] + # users = User.objects.all().count() + # submissions = Submission.objects.all().count() + + public_competitions = 204 + users = 12216 + submissions = 70276 context['general_stats'] = [ {'label': "Public Competitions", 'count': public_competitions}, From 75574881d26f7b51813dfbdb234199433380e2cd Mon Sep 17 00:00:00 2001 From: Ihsan Ullah Date: Thu, 7 Nov 2024 15:42:52 +0500 Subject: [PATCH 2/4] flake fixes --- src/apps/pages/views.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/apps/pages/views.py b/src/apps/pages/views.py index 22f4a98fc..d82d4b2cd 100644 --- a/src/apps/pages/views.py +++ b/src/apps/pages/views.py @@ -1,9 +1,8 @@ from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage from django.views.generic import TemplateView -from django.db.models import Count, Q +from django.db.models import Q -from competitions.models import Competition, Submission -from profiles.models import User +from competitions.models import Submission from announcements.models import Announcement, NewsPost from django.shortcuts import render From 7792f390c3a03aded8716f312458dfa640aee8fa Mon Sep 17 00:00:00 2001 From: Ihsan Ullah Date: Thu, 7 Nov 2024 19:25:09 +0500 Subject: [PATCH 3/4] hide featured and popular competitions from home page --- src/templates/pages/home.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/templates/pages/home.html b/src/templates/pages/home.html index 3316a58b5..487df1b19 100644 --- a/src/templates/pages/home.html +++ b/src/templates/pages/home.html @@ -48,7 +48,7 @@ {% endif %}
- +
From 23820cfa526c6ef869223c8522ce1fc6ae868792 Mon Sep 17 00:00:00 2001 From: Ihsan Ullah Date: Thu, 7 Nov 2024 20:29:30 +0500 Subject: [PATCH 4/4] show static competiitons in popular and featured --- src/apps/competitions/utils.py | 54 ++++++++++++++++++++++++---------- src/templates/pages/home.html | 2 +- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/apps/competitions/utils.py b/src/apps/competitions/utils.py index 54998b608..1e7f0b285 100644 --- a/src/apps/competitions/utils.py +++ b/src/apps/competitions/utils.py @@ -1,9 +1,8 @@ ''' This file contains utilities for competitions ''' -import random - -from django.db.models import Count +# import random +# from django.db.models import Count from competitions.models import Competition @@ -16,14 +15,26 @@ def get_popular_competitions(limit=4): :rtype: list :return: Most popular competitions. ''' - competitions = Competition.objects.filter(published=True) \ - .annotate(participant_count=Count('participants')) \ - .order_by('-participant_count') - if len(competitions) <= limit: - return competitions + # TODO: Fix the fetching of the popular competitions + # Uncomment and update the following code when a long term fix is implemented for participants count + + # competitions = Competition.objects.filter(published=True) \ + # .annotate(participant_count=Count('participants')) \ + # .order_by('-participant_count') + + # if len(competitions) <= limit: + # return competitions - return competitions[:limit] + # return competitions[:limit] + + # Temporary solution to show specific popular competitions + try: + popular_competiion_ids = [1752, 1772, 2338, 3863] + competitions = Competition.objects.filter(id__in=popular_competiion_ids) + return competitions + except Exception: + return [] def get_featured_competitions(limit=4, excluded_competitions=None): @@ -36,13 +47,24 @@ def get_featured_competitions(limit=4, excluded_competitions=None): :return: list of featured competitions ''' - competitions = Competition.objects.filter(published=True) \ - .annotate(participant_count=Count('participants')) + # TODO: Fix the fetching of the featured competitions + # Uncomment and update the following code when a long term fix is implemented for participants count + + # competitions = Competition.objects.filter(published=True) \ + # .annotate(participant_count=Count('participants')) + + # if excluded_competitions: + # competitions = competitions.exclude(pk__in=[c.pk for c in excluded_competitions]) - if excluded_competitions: - competitions = competitions.exclude(pk__in=[c.pk for c in excluded_competitions]) + # if len(competitions) <= limit: + # return competitions + # else: + # return random.sample(list(competitions), limit) - if len(competitions) <= limit: + # Temporary solution to show specific featured competitions + try: + featured_competiion_ids = [3523, 2745, 3160, 1567] + competitions = Competition.objects.filter(id__in=featured_competiion_ids) return competitions - else: - return random.sample(list(competitions), limit) + except Exception: + return [] diff --git a/src/templates/pages/home.html b/src/templates/pages/home.html index 487df1b19..3316a58b5 100644 --- a/src/templates/pages/home.html +++ b/src/templates/pages/home.html @@ -48,7 +48,7 @@ {% endif %}
- +