forked from DMOJ/online-judge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Celery task for running MOSS; DMOJ#1091
- Loading branch information
1 parent
e72ccf3
commit 43f7be6
Showing
9 changed files
with
291 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Generated by Django 2.1.12 on 2019-10-17 20:52 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('judge', '0092_contest_clone'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ContestMoss', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('language', models.CharField(max_length=10)), | ||
('submission_count', models.PositiveIntegerField(default=0)), | ||
('url', models.URLField(blank=True, null=True)), | ||
], | ||
options={ | ||
'verbose_name': 'contest moss result', | ||
'verbose_name_plural': 'contest moss results', | ||
}, | ||
), | ||
migrations.AlterModelOptions( | ||
name='contest', | ||
options={'permissions': (('see_private_contest', 'See private contests'), ('edit_own_contest', 'Edit own contests'), ('edit_all_contest', 'Edit all contests'), ('clone_contest', 'Clone contest'), ('moss_contest', 'Moss contest'), ('contest_rating', 'Rate contests'), ('contest_access_code', 'Contest access codes'), ('create_private_contest', 'Create private contests')), 'verbose_name': 'contest', 'verbose_name_plural': 'contests'}, | ||
), | ||
migrations.AddField( | ||
model_name='contestmoss', | ||
name='contest', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='moss', to='judge.Contest', verbose_name='contest'), | ||
), | ||
migrations.AddField( | ||
model_name='contestmoss', | ||
name='problem', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='moss', to='judge.Problem', verbose_name='problem'), | ||
), | ||
migrations.AlterUniqueTogether( | ||
name='contestmoss', | ||
unique_together={('contest', 'problem', 'language')}, | ||
), | ||
] |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from judge.tasks.demo import * | ||
from judge.tasks.moss import * | ||
from judge.tasks.submission import * |
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,57 @@ | ||
from celery import shared_task | ||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.utils.translation import gettext as _ | ||
from moss import MOSS | ||
|
||
from judge.models import Contest, ContestMoss, ContestParticipation, Submission | ||
from judge.utils.celery import Progress | ||
|
||
__all__ = ('run_moss',) | ||
|
||
|
||
@shared_task(bind=True) | ||
def run_moss(self, contest_key): | ||
moss_api_key = settings.MOSS_API_KEY | ||
if moss_api_key is None: | ||
raise ImproperlyConfigured('No MOSS API Key supplied') | ||
|
||
contest = Contest.objects.get(key=contest_key) | ||
ContestMoss.objects.filter(contest=contest).delete() | ||
|
||
length = len(ContestMoss.LANG_MAPPING) * contest.problems.count() | ||
moss_results = [] | ||
|
||
with Progress(self, length, stage=_('Running moss')) as p: | ||
for problem in contest.problems.all(): | ||
for dmoj_lang, moss_lang in ContestMoss.LANG_MAPPING: | ||
result = ContestMoss(contest=contest, problem=problem, language=dmoj_lang) | ||
|
||
subs = Submission.objects.filter( | ||
contest__participation__virtual__in=(ContestParticipation.LIVE, ContestParticipation.SPECTATE), | ||
contest_object=contest, | ||
problem=problem, | ||
language__common_name=dmoj_lang, | ||
).order_by('-points').values_list('user__user__username', 'source__source') | ||
|
||
if subs.exists(): | ||
moss_call = MOSS(moss_api_key, language=moss_lang, matching_file_limit=100, | ||
comment='%s - %s' % (contest.key, problem.code)) | ||
|
||
users = set() | ||
|
||
for username, source in subs: | ||
if username in users: | ||
continue | ||
users.add(username) | ||
moss_call.add_file_from_memory(username, source.encode('utf-8')) | ||
|
||
result.url = moss_call.process() | ||
result.submission_count = len(users) | ||
|
||
moss_results.append(result) | ||
p.did(1) | ||
|
||
ContestMoss.objects.bulk_create(moss_results) | ||
|
||
return len(moss_results) |
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,87 @@ | ||
{% extends "common-content.html" %} | ||
|
||
{% block title_ruler %}{% endblock %} | ||
|
||
{% block title_row %} | ||
{% set tab = 'moss' %} | ||
{% set title = contest.name %} | ||
{% include "contest/contest-tabs.html" %} | ||
{% endblock %} | ||
|
||
{% block content_media %} | ||
<style> | ||
.panes { | ||
display: flex; | ||
flex-wrap: wrap; | ||
justify-content: center; | ||
} | ||
.pane { | ||
padding: 20px; | ||
} | ||
</style> | ||
{% endblock %} | ||
|
||
{% block content_js_media %} | ||
<script type="text/javascript"> | ||
$(function () { | ||
$('.contest-moss').click(function () { | ||
return confirm('{{ _('Are you sure you want Moss the contest?') }}'); | ||
}); | ||
}); | ||
$(function () { | ||
$('.contest-moss-delete').click(function () { | ||
return confirm('{{ _('Are you sure you want to delete the Moss results?') }}'); | ||
}); | ||
}); | ||
</script> | ||
{% endblock %} | ||
{% block body %} | ||
{% if has_results %} | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th class="header">{{ _('Problem') }}</th> | ||
{% for lang in languages %} | ||
<th class="header">{{ lang }}</th> | ||
{% endfor %} | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for problem, results in moss_results %} | ||
<tr id="problem-{{ problem.code }}"> | ||
<td> | ||
<a href="{{ url('problem_detail', problem.code) }}">{{ problem.name }}</a> | ||
</td> | ||
{% for result in results %} | ||
<td> | ||
{% if result.submission_count %} | ||
<a href="{{ result.url }}">{{ result.submission_count }} {{_('submissions')}}</a> | ||
{% else %} | ||
{{ _('No submissions') }} | ||
{% endif %} | ||
</td> | ||
{% endfor %} | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
{% endif %} | ||
<div class="panes"> | ||
<div class="pane"> | ||
<form method="post" action="{{ url('contest_moss', contest.key) }}"> | ||
{% csrf_token %} | ||
<input type="submit" class="unselectable button full contest-moss" style="padding: 10px;" | ||
value="{% if has_results %} {{ _('Re-Moss contest') }} {% else %} {{ _('Moss contest') }} {% endif %}"> | ||
</form> | ||
</div> | ||
{% if has_results %} | ||
<div class="pane"> | ||
<form method="post" action="{{ url('contest_moss_delete', contest.key) }}"> | ||
{% csrf_token %} | ||
<input type="submit" class="unselectable button full contest-moss-delete" style="padding: 10px;" | ||
value="{{ _('Delete Moss results') }}"> | ||
</form> | ||
</div> | ||
{% endif %} | ||
</div> | ||
{% endblock %} |