<commit_before>import logging

from .judgehandler import JudgeHandler
from judge.models import Submission, SubmissionTestCase

logger = logging.getLogger('judge.bridge')


class DjangoJudgeHandler(JudgeHandler):
    def finish(self):
        JudgeHandler.finish(self)
        for id in self._load:
            submission = Submission.objects.get(id=id)
            submission.status = 'IE'
            submission.save()

    def on_grading_begin(self, packet):
        JudgeHandler.on_grading_begin(self, packet)
        submission = Submission.objects.get(id=packet['submission-id'])
        submission.status = 'G'
        submission.save()

    def on_grading_end(self, packet):
        JudgeHandler.on_grading_end(self, packet)
        submission = Submission.objects.get(id=packet['submission-id'])
        submission.status = 'D'
        submission.save()

    def on_compile_error(self, packet):
        JudgeHandler.on_compile_error(self, packet)
        submission = Submission.objects.get(id=packet['submission-id'])
        submission.status = 'CE'
        submission.save()

    def on_test_case(self, packet):
        JudgeHandler.on_test_case(self, packet)
        test_case = SubmissionTestCase.objects.get_or_create(submission__id=packet['submission-id'],
                                                             case=packet['position'])[0]
        status = packet['status']
        if status & 2:
            test_case.status = 'RTE'
        elif status & 4:
            test_case.status = 'TLE'
        elif status & 1:
            test_case.status = 'WA'
        else:
            test_case.status = 'AC'
        test_case.time = packet['time']
        test_case.memory = packet['memory']
        test_case.points = packet['points']
        test_case.total = packet['total-points']
        test_case.save()<commit_msg>Calculates final scores.<commit_after>import logging

from .judgehandler import JudgeHandler
from judge.models import Submission, SubmissionTestCase

logger = logging.getLogger('judge.bridge')


class DjangoJudgeHandler(JudgeHandler):
    def finish(self):
        JudgeHandler.finish(self)
        for id in self._load:
            submission = Submission.objects.get(id=id)
            submission.status = 'IE'
            submission.save()

    def on_grading_begin(self, packet):
        JudgeHandler.on_grading_begin(self, packet)
        submission = Submission.objects.get(id=packet['submission-id'])
        submission.status = 'G'
        submission.save()

    def on_grading_end(self, packet):
        JudgeHandler.on_grading_end(self, packet)
        submission = Submission.objects.get(id=packet['submission-id'])

        time = 0
        memory = 0
        points = 0
        total = 0
        status = 0
        status_codes = ['AC', 'WA', 'TLE', 'RTE']
        for case in SubmissionTestCase.objects.filter(submission=submission):
            time += case.time
            total += case.total
            points += case.points
            memory += case.total
            i = status_codes.index(case.status)
            if i > status:
                status = i

        submission.status = 'D'
        submission.time = time
        submission.memory = memory
        submission.points = points
        submission.total = total
        submission.result = status_codes[status]
        submission.save()

    def on_compile_error(self, packet):
        JudgeHandler.on_compile_error(self, packet)
        submission = Submission.objects.get(id=packet['submission-id'])
        submission.status = 'CE'
        submission.save()

    def on_test_case(self, packet):
        JudgeHandler.on_test_case(self, packet)
        submission = Submission.objects.get(id=packet['submission-id'])
        test_case = SubmissionTestCase.objects.get_or_create(submission=submission, case=packet['position'])[0]
        status = packet['status']
        if status & 2:
            test_case.status = 'RTE'
        elif status & 4:
            test_case.status = 'TLE'
        elif status & 1:
            test_case.status = 'WA'
        else:
            test_case.status = 'AC'
        test_case.time = packet['time']
        test_case.memory = packet['memory']
        test_case.points = packet['points']
        test_case.total = packet['total-points']
        test_case.save()
{'fraction_non_alphanumeric': 0.10640758732361785, 'fraction_numerical': 0.003007170946102244, 'mean_word_length': 2.3441608662026296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2242005', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1219, 'n_words': 235}