Fix rating (DMOJ)

This commit is contained in:
cuom1999 2021-05-24 20:10:26 -05:00
parent 3b98f975a0
commit c76b1a4ed1

View file

@ -2,7 +2,7 @@ import math
from bisect import bisect from bisect import bisect
from operator import attrgetter, itemgetter from operator import attrgetter, itemgetter
from django.db import connection, transaction from django.db import transaction
from django.db.models import Count, OuterRef, Subquery from django.db.models import Count, OuterRef, Subquery
from django.db.models.functions import Coalesce from django.db.models.functions import Coalesce
from django.utils import timezone from django.utils import timezone
@ -152,18 +152,12 @@ def rate_contest(contest):
now = timezone.now() now = timezone.now()
ratings = [Rating(user_id=i, contest=contest, rating=r, volatility=v, last_rated=now, participation_id=p, rank=z) ratings = [Rating(user_id=i, contest=contest, rating=r, volatility=v, last_rated=now, participation_id=p, rank=z)
for i, p, r, v, z in zip(user_ids, participation_ids, rating, volatility, ranking)] for i, p, r, v, z in zip(user_ids, participation_ids, rating, volatility, ranking)]
cursor = connection.cursor()
cursor.execute('CREATE TEMPORARY TABLE _profile_rating_update(id integer, rating integer)')
cursor.executemany('INSERT INTO _profile_rating_update VALUES (%s, %s)', list(zip(user_ids, rating)))
with transaction.atomic(): with transaction.atomic():
Rating.objects.filter(contest=contest).delete() Rating.objects.filter(contest=contest).delete()
Rating.objects.bulk_create(ratings) Rating.objects.bulk_create(ratings)
cursor.execute(''' Profile.objects.filter(contest_history__contest=contest, contest_history__virtual=0) \
UPDATE `%s` p INNER JOIN `_profile_rating_update` tmp ON (p.id = tmp.id) .update(rating=Subquery(Rating.objects.filter(user=OuterRef('id')) \
SET p.rating = tmp.rating .order_by('-contest__end_time').values('rating')[:1]))
''' % Profile._meta.db_table)
cursor.execute('DROP TABLE _profile_rating_update')
cursor.close()
return old_rating, old_volatility, ranking, times_ranked, rating, volatility return old_rating, old_volatility, ranking, times_ranked, rating, volatility