<commit_before>import logging
from datetime import datetime

import pytz
from django import db
from django.utils import timezone

from judge import event_poster as event
from judge.caching import finished_submission
from judge.models import Submission, SubmissionTestCase, Judge, Problem, Language
from .daemon import AMQPResponseDaemon

import time

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

UPDATE_RATE_LIMIT = 5
UPDATE_RATE_TIME = 0.5

def _ensure_connection():
    try:
        db.connection.cursor().execute('SELECT 1').fetchall()
    except Exception:
        db.connection.close()


class AMQPJudgeResponseDaemon(AMQPResponseDaemon):

    def __init__(self):
        # each value is (updates, last reset)
        self.update_counter = {}
    
    def on_acknowledged(self, packet):
        super(AMQPJudgeResponseDaemon, self).on_acknowledged(packet)
        _ensure_connection()

        try:
            submission = Submission.objects.get(id=packet['id'])
        except Submission.DoesNotExist:
            logger.warning('Unknown submission: %d', packet['id'])
            return
        submission.status = 'P'<commit_msg>Close #398<commit_after>import logging
from datetime import datetime

import pytz
from django import db
from django.utils import timezone

from judge import event_poster as event
from judge.caching import finished_submission
from judge.models import Submission, SubmissionTestCase, Judge, Problem, Language
from .daemon import AMQPResponseDaemon

import time

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

UPDATE_RATE_LIMIT = 5
UPDATE_RATE_TIME = 0.5

def _ensure_connection():
    try:
        db.connection.cursor().execute('SELECT 1').fetchall()
    except Exception:
        db.connection.close()


class AMQPJudgeResponseDaemon(AMQPResponseDaemon):

    def __init__(self):
        super(AMQPJudgeResponseDaemon, self).__init__()
        # each value is (updates, last reset)
        self.update_counter = {}
    
    def on_acknowledged(self, packet):
        super(AMQPJudgeResponseDaemon, self).on_acknowledged(packet)
        _ensure_connection()

        try:
            submission = Submission.objects.get(id=packet['id'])
        except Submission.DoesNotExist:
            logger.warning('Unknown submission: %d', packet['id'])
            return
        submission.status = 'P'
{'fraction_non_alphanumeric': 0.08630313439244311, 'fraction_numerical': 0.0047230571060541005, 'mean_word_length': 3.559686888454012, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1748360', 'n_tokens_mistral': 676, 'n_tokens_neox': 643, 'n_words': 168}