Skip to content

Commit

Permalink
Merge pull request #1650 from codalab/develop
Browse files Browse the repository at this point in the history
Merge develop into master
  • Loading branch information
ihsaan-ullah authored Nov 7, 2024
2 parents 6eda3ca + e352bec commit fd81bdc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
54 changes: 38 additions & 16 deletions src/apps/competitions/utils.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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):
Expand All @@ -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 []
30 changes: 19 additions & 11 deletions src/apps/pages/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,15 +14,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)),
)
# 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

public_competitions = data['published_comps']
users = User.objects.all().count()
submissions = Submission.objects.all().count()
# 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},
Expand Down

0 comments on commit fd81bdc

Please sign in to comment.