Fewer db requests on bda tirage.

bda.algorithm
- use iterator to find max_groups, instead of a db request

bda.views.do_tirage
- select_related() are now focused on some relationships (they were
  taking useless relationships)
- bda-revente filling takes 1 request (each save and add was issuing
  1 request)
This commit is contained in:
Aurélien Delobelle 2017-04-07 16:22:10 +02:00
parent 3e0bd2e758
commit 9f307c1bd0
2 changed files with 17 additions and 9 deletions

View file

@ -22,8 +22,7 @@ class Algorithm(object):
show.requests
- on crée des tables de demandes pour chaque personne, afin de
pouvoir modifier les rankings"""
self.max_group = \
2 * choices.aggregate(Max('priority'))['priority__max']
self.max_group = 2*max(choice.priority for choice in choices)
self.shows = []
showdict = {}
for show in shows:

View file

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from collections import defaultdict
from functools import partial
import random
import hashlib
@ -262,9 +263,9 @@ def do_tirage(tirage_elt, token):
# Initialisation du dictionnaire data qui va contenir les résultats
start = time.time()
data = {
'shows': tirage_elt.spectacle_set.select_related().all(),
'shows': tirage_elt.spectacle_set.select_related('location'),
'token': token,
'members': tirage_elt.participant_set.all(),
'members': tirage_elt.participant_set.select_related('user'),
'total_slots': 0,
'total_losers': 0,
'total_sold': 0,
@ -277,7 +278,7 @@ def do_tirage(tirage_elt, token):
ChoixSpectacle.objects
.filter(spectacle__tirage=tirage_elt)
.order_by('participant', 'priority')
.select_related().all()
.select_related('participant', 'participant__user', 'spectacle')
)
results = Algorithm(data['shows'], data['members'], choices)(token)
@ -334,10 +335,18 @@ def do_tirage(tirage_elt, token):
])
# On inscrit à BdA-Revente ceux qui n'ont pas eu les places voulues
for (show, _, losers) in results:
for (loser, _, _, _) in losers:
loser.choicesrevente.add(show)
loser.save()
ChoixRevente = Participant.choicesrevente.through
lost_by = defaultdict(set)
for show, _, losers in results:
for loser, _, _, _ in losers:
lost_by[loser].add(show)
ChoixRevente.objects.bulk_create(
ChoixRevente(participant=member, spectacle=show)
for member, shows in lost_by.items()
for show in shows
)
data["duration"] = time.time() - start
data["results"] = results