-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1669 from codalab/submissions_and_participants_co…
…unts Optimization `PR#2` - Submissions and Participants Count
- Loading branch information
Showing
13 changed files
with
252 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/apps/api/tests/test_competition_submissions_participants_counts.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from django.test import TestCase | ||
from competitions.models import Submission, CompetitionParticipant | ||
from factories import UserFactory, CompetitionFactory, PhaseFactory, CompetitionParticipantFactory, SubmissionFactory | ||
|
||
|
||
class CompetitionSubmissionsParticipantsCountsTests(TestCase): | ||
def setUp(self): | ||
|
||
# User | ||
self.creator = UserFactory(username='creator', password='creator') | ||
# Competition | ||
self.competition = CompetitionFactory(created_by=self.creator) | ||
# Phase | ||
self.phase = PhaseFactory(competition=self.competition) | ||
|
||
# Create a submission for the delete test | ||
self.submission = SubmissionFactory(phase=self.phase, owner=self.creator, status=CompetitionParticipant.APPROVED) | ||
self.competition.refresh_from_db() | ||
|
||
def test_adding_submission_updates_submission_count(self): | ||
initial_count = self.competition.submissions_count | ||
|
||
self.assertEqual(initial_count, 1) # one submission created in the setup | ||
|
||
# Add a new submission | ||
_ = SubmissionFactory(phase=self.phase, owner=self.creator, status=Submission.SUBMITTED) | ||
self.competition.refresh_from_db() | ||
|
||
# Assert that the count increased by 1 | ||
self.assertEqual(self.competition.submissions_count, initial_count + 1) | ||
|
||
def test_deleting_submission_updates_submission_count(self): | ||
initial_count = self.competition.submissions_count | ||
|
||
self.assertEqual(initial_count, 1) # one submission created in the setup | ||
|
||
# Delete the existing submission | ||
self.submission.delete() | ||
self.competition.refresh_from_db() | ||
|
||
# Assert that the count decreased by 1 | ||
self.assertEqual(self.competition.submissions_count, initial_count - 1) | ||
|
||
def test_adding_participant_updates_participants_count(self): | ||
initial_count = self.competition.participants_count | ||
|
||
self.assertEqual(initial_count, 1) # default count is 1 | ||
|
||
# Add a new approved participant | ||
new_participant = UserFactory(username='new_participant', password='test') | ||
CompetitionParticipantFactory(user=new_participant, competition=self.competition, status=CompetitionParticipant.APPROVED) | ||
self.competition.refresh_from_db() | ||
|
||
# Assert that the count increased by 1 | ||
self.assertEqual(self.competition.participants_count, initial_count + 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/apps/competitions/migrations/0049_auto_20241118_1106.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Generated by Django 2.2.17 on 2024-11-18 11:06 | ||
|
||
from django.db import migrations, models | ||
import storages.backends.s3boto3 | ||
import utils.data | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('competitions', '0048_auto_20240401_1646'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='competition', | ||
name='participants_count', | ||
field=models.PositiveIntegerField(default=0), | ||
), | ||
migrations.AddField( | ||
model_name='competition', | ||
name='submissions_count', | ||
field=models.PositiveIntegerField(default=0), | ||
), | ||
migrations.AlterField( | ||
model_name='submission', | ||
name='detailed_result', | ||
field=models.FileField(blank=True, null=True, storage=storages.backends.s3boto3.S3Boto3Storage(), upload_to=utils.data.PathWrapper('detailed_result')), | ||
), | ||
migrations.AlterField( | ||
model_name='submission', | ||
name='prediction_result', | ||
field=models.FileField(blank=True, null=True, storage=storages.backends.s3boto3.S3Boto3Storage(), upload_to=utils.data.PathWrapper('prediction_result')), | ||
), | ||
migrations.AlterField( | ||
model_name='submission', | ||
name='scoring_result', | ||
field=models.FileField(blank=True, null=True, storage=storages.backends.s3boto3.S3Boto3Storage(), upload_to=utils.data.PathWrapper('scoring_result')), | ||
), | ||
migrations.AlterField( | ||
model_name='submissiondetails', | ||
name='data_file', | ||
field=models.FileField(storage=storages.backends.s3boto3.S3Boto3Storage(), upload_to=utils.data.PathWrapper('submission_details')), | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
src/apps/competitions/migrations/0050_auto_20241128_0814.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 2.2.28 on 2024-11-28 08:14 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('competitions', '0049_auto_20241118_1106'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='competition', | ||
name='participants_count', | ||
field=models.PositiveIntegerField(default=1), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
This script is created to fill newly added fields in the competition modal with the correct data | ||
The new fields are: | ||
- submissions_count | ||
- participants_count | ||
This script should be used only after the new changes are deployed on the server. | ||
Usage: | ||
Bash into django console | ||
``` | ||
docker compose exec django ./manage.py shell_plus | ||
``` | ||
Import and call the function | ||
``` | ||
from competitions.submission_participant_counts import compute_submissions_p | ||
articipants_counts | ||
compute_submissions_participants_counts() | ||
``` | ||
""" | ||
from competitions.models import Competition, CompetitionParticipant, Phase, Submission | ||
|
||
|
||
def compute_submissions_participants_counts(): | ||
""" | ||
This function counts submissions and participants of competitions and updates all competitions | ||
""" | ||
competitions = Competition.objects.all() | ||
|
||
for competition in competitions: | ||
# Count participants for the competition | ||
participants_count = CompetitionParticipant.objects.filter(competition=competition).count() | ||
|
||
# Get all phases related to the competition | ||
phases = Phase.objects.filter(competition=competition) | ||
|
||
# Count submissions across all phases of the competition | ||
submissions_count = Submission.objects.filter(phase__in=phases).count() | ||
|
||
# Update the competition fields | ||
competition.participants_count = participants_count | ||
competition.submissions_count = submissions_count | ||
competition.save() | ||
|
||
print(f"{len(competitions)} Competitions updated successfully!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 2.2.17 on 2024-11-18 11:06 | ||
|
||
from django.db import migrations, models | ||
import storages.backends.s3boto3 | ||
import utils.data | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('datasets', '0007_auto_20230609_1738'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='data', | ||
name='data_file', | ||
field=models.FileField(blank=True, null=True, storage=storages.backends.s3boto3.S3Boto3Storage(), upload_to=utils.data.PathWrapper('dataset')), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.