gestioCOF/tirage_bda.py

63 lines
2.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# coding: utf-8
import os
import sys
import time
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cof.settings")
from django.conf import settings
settings.DEBUG = True
from bda.models import Spectacle, Participant, ChoixSpectacle
from bda.algorithm import Algorithm
from django.db.models import Sum
from django.db import connection
start = time.time()
shows = Spectacle.objects.all()
members = Participant.objects.all()
choices = ChoixSpectacle.objects.order_by('participant', 'priority').select_related().all()
available_slots = Spectacle.objects.aggregate(Sum('slots'))['slots__sum']
cursor = connection.cursor()
cursor.execute("SELECT SUM(`slots` * `price`) AS `total` FROM `bda_spectacle`;")
total_price = cursor.fetchone()[0]
print "%d spectacles" % len(shows)
print "%d places" % available_slots
print "%d participants" % len(members)
print "%d demandes" % len(choices)
print "%d places demandées" % (len(choices) + len(choices.filter(double = True).all()))
print "%.02f€ à brasser" % total_price
print "Récupération: %.2fs" % (time.time() - start)
start_init = time.time()
algo = Algorithm(shows, members, choices)
print "Initialisation: %.2fs" % (time.time() - start_init)
start_algo = time.time()
results = algo(sys.argv[1])
print "Traitement: %.2fs" % (time.time() - start_algo)
print len(connection.queries), "requêtes SQL effectuées"
queries = list(connection.queries)
total_slots = 0
total_losers = 0
for (_, members, losers) in results:
total_slots += len(members)
total_losers += len(losers)
print "Placés %d\nDécus %d" % (total_slots, total_losers)
print "Total %d" % (total_slots + total_losers)
members2 = {}
members_uniq = {}
for (show, members, _) in results:
for (member, _, _, _) in members:
if member.id not in members_uniq:
members_uniq[member.id] = member
members2[member] = []
member.total = 0
member = members_uniq[member.id]
members2[member].append(show)
member.total += show.price
if len(members) < show.slots:
print "%d place(s) invendue(s) pour %s" % (show.slots - len(members), show)
members2 = members2.items()
print "Temps total: %.2fs" % (time.time() - start)
print "Requêtes SQL:"
for query in queries:
print query['sql']