-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57ea4b0
commit 37b1d4f
Showing
11 changed files
with
402 additions
and
176 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
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
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,50 @@ | ||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
from django.db.models import ExpressionWrapper, FloatField, Sum | ||
from django.utils import timezone | ||
|
||
from judge.models import Organization, Submission | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'backfill current credit usage for all organizations' | ||
|
||
def backfill_current_credit(self, org: Organization, month_start): | ||
credit_problem = ( | ||
Submission.objects.filter( | ||
problem__organizations=org, | ||
contest_object__isnull=True, | ||
date__gte=month_start, | ||
) | ||
.annotate( | ||
credit=ExpressionWrapper( | ||
Sum('test_cases__time'), output_field=FloatField(), | ||
), | ||
) | ||
.aggregate(Sum('credit'))['credit__sum'] or 0 | ||
) | ||
|
||
credit_contest = ( | ||
Submission.objects.filter( | ||
contest_object__organizations=org, | ||
date__gte=month_start, | ||
) | ||
.annotate( | ||
credit=ExpressionWrapper( | ||
Sum('test_cases__time'), output_field=FloatField(), | ||
), | ||
) | ||
.aggregate(Sum('credit'))['credit__sum'] or 0 | ||
) | ||
|
||
org.monthly_credit = settings.VNOJ_MONTHLY_FREE_CREDIT | ||
|
||
org.consume_credit(credit_problem + credit_contest) | ||
|
||
def handle(self, *args, **options): | ||
# get current month | ||
start = timezone.now().replace(day=1, hour=0, minute=0, second=0, microsecond=0) | ||
print('Processing', start, 'at time', timezone.now()) | ||
|
||
for org in Organization.objects.all(): | ||
self.backfill_current_credit(org, start) |
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,28 @@ | ||
# Generated by Django 3.2.19 on 2024-09-19 02:02 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('judge', '0206_monthly_credit'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='organization', | ||
name='available_credit', | ||
field=models.FloatField(default=0, help_text='Available credits'), | ||
), | ||
migrations.AddField( | ||
model_name='organization', | ||
name='current_consumed_credit', | ||
field=models.FloatField(default=0, help_text='Total used credit this month'), | ||
), | ||
migrations.AddField( | ||
model_name='organization', | ||
name='monthly_credit', | ||
field=models.FloatField(default=0, help_text='Total monthly free credit left'), | ||
), | ||
] |
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
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
Oops, something went wrong.