Skip to content
Snippets Groups Projects
Commit 0e73a899 authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

[experiments,search] Fix Rank reset after experiment migrations

parent 36a80345
No related branches found
No related tags found
1 merge request!194Scheduler
Pipeline #
...@@ -31,6 +31,19 @@ from __future__ import unicode_literals ...@@ -31,6 +31,19 @@ from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
def move_result_to_cache(apps, schema_editor):
'''Moves the result association from the block to the related cache file'''
Result = apps.get_model("experiments", "Result")
total = Result.objects.count()
if total: print('')
for i, r in enumerate(Result.objects.order_by('-id')):
print("Resetting result (%d) %d/%d..." % (r.id, i+1, total))
r.cache = r.block.hashes.first()
r.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
...@@ -44,8 +57,5 @@ class Migration(migrations.Migration): ...@@ -44,8 +57,5 @@ class Migration(migrations.Migration):
field=models.ForeignKey(related_name='results', field=models.ForeignKey(related_name='results',
to='experiments.CachedFile', null=True), to='experiments.CachedFile', null=True),
), ),
migrations.AlterUniqueTogether( migrations.RunPython(move_result_to_cache),
name='result',
unique_together=set([('cache', 'name')]),
),
] ]
...@@ -31,30 +31,28 @@ from __future__ import unicode_literals ...@@ -31,30 +31,28 @@ from __future__ import unicode_literals
from django.db import migrations from django.db import migrations
def move_result_to_cache(apps, schema_editor): def dedup_resuls(apps, schema_editor):
'''Moves the result association from the block to the related cache file''' '''Deletes duplicated results (older ones)'''
Result = apps.get_model("experiments", "Result") Result = apps.get_model("experiments", "Result")
total = Result.objects.count() for i, r in enumerate(Result.objects.order_by('-id')):
if total: print('') older = Result.objects.filter(name=r.name, id__lt=r.id,
for i, r in enumerate(Result.objects.order_by('id')): cache=r.block.hashes.first())
print("Resetting result (%d) %d/%d..." % (r.id, i+1, total))
older = Result.objects.filter(name=r.name, cache=r.block.hashes.first()).exclude(id=r.id).first()
if older: if older:
print("Cache %s already contains Result `%s' - keeping " \ print("Cache %s already contains Result `%s' - keeping " \
"newest..." % (older.cache.hash, older.name)) "newest (out of %d)..." % (r.block.hashes.first().hash, r.name,
older.count()+1))
older.delete() older.delete()
r.cache = r.block.hashes.first()
r.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('experiments', '0002_scheduler_addons'), ('experiments', '0002_scheduler_addons'),
('search', '0002_scheduler_addons'),
] ]
operations = [ operations = [
migrations.RunPython(move_result_to_cache), migrations.RunPython(dedup_resuls),
] ]
...@@ -216,6 +216,10 @@ class Migration(migrations.Migration): ...@@ -216,6 +216,10 @@ class Migration(migrations.Migration):
] ]
operations = [ operations = [
migrations.AlterUniqueTogether(
name='result',
unique_together=set([('cache', 'name')]),
),
migrations.RemoveField( migrations.RemoveField(
model_name='result', model_name='result',
name='block', name='block',
......
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
from __future__ import unicode_literals
from django.db import migrations
def reset_ranks(apps, schema_editor):
'''Reset ranks before older results can be deleted'''
Result = apps.get_model("experiments", "Result")
Rank = apps.get_model("search", "Rank")
total = Result.objects.count()
if total: print('')
for i, r in enumerate(Result.objects.order_by('-id')):
older = Result.objects.filter(name=r.name, id__lt=r.id,
cache=r.block.hashes.first())
for old in older:
# check if any leaderboard ranks require updates
for rank in Rank.objects.filter(result__in=(old,)):
print("Rank %d for search `%s/%s' uses old Result `%d' - " \
"resetting to newer Result `%d'..." % \
(rank.id, rank.leaderboard.search.author.username,
rank.leaderboard.search.name, old.id, r.id))
rank.result.remove(old)
rank.result.add(r)
class Migration(migrations.Migration):
dependencies = [
('search', '0001_initial'),
('experiments', '0002_scheduler_addons'),
]
operations = [
migrations.RunPython(reset_ranks),
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment