mirror of
https://gitlab.crans.org/bde/nk20
synced 2024-11-26 18:37:12 +00:00
[WEI] Cache optimization
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
parent
aa75ce5c7a
commit
79a116d9c6
@ -50,15 +50,19 @@ class WEIBusInformation:
|
|||||||
self.bus.information = d
|
self.bus.information = d
|
||||||
self.bus.save()
|
self.bus.save()
|
||||||
|
|
||||||
def free_seats(self, surveys: List["WEISurvey"] = None):
|
def free_seats(self, surveys: List["WEISurvey"] = None, quotas=None):
|
||||||
size = self.bus.size
|
if not quotas:
|
||||||
already_occupied = WEIMembership.objects.filter(bus=self.bus).count()
|
size = self.bus.size
|
||||||
|
already_occupied = WEIMembership.objects.filter(bus=self.bus).count()
|
||||||
|
quotas = {self.bus: size - already_occupied}
|
||||||
|
|
||||||
|
quota = quotas[self.bus]
|
||||||
valid_surveys = sum(1 for survey in surveys if survey.information.valid
|
valid_surveys = sum(1 for survey in surveys if survey.information.valid
|
||||||
and survey.information.get_selected_bus() == self.bus) if surveys else 0
|
and survey.information.get_selected_bus() == self.bus) if surveys else 0
|
||||||
return size - already_occupied - valid_surveys
|
return quota - valid_surveys
|
||||||
|
|
||||||
def has_free_seats(self, surveys=None):
|
def has_free_seats(self, surveys=None, quotas=None):
|
||||||
return self.free_seats(surveys) > 0
|
return self.free_seats(surveys, quotas) > 0
|
||||||
|
|
||||||
|
|
||||||
class WEISurveyAlgorithm:
|
class WEISurveyAlgorithm:
|
||||||
@ -86,14 +90,20 @@ class WEISurveyAlgorithm:
|
|||||||
"""
|
"""
|
||||||
Queryset of all first year registrations
|
Queryset of all first year registrations
|
||||||
"""
|
"""
|
||||||
return WEIRegistration.objects.filter(wei__year=cls.get_survey_class().get_year(), first_year=True)
|
if not hasattr(cls, '_registrations'):
|
||||||
|
cls._registrations = WEIRegistration.objects.filter(wei__year=cls.get_survey_class().get_year(),
|
||||||
|
first_year=True).all()
|
||||||
|
|
||||||
|
return cls._registrations
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_buses(cls) -> QuerySet:
|
def get_buses(cls) -> QuerySet:
|
||||||
"""
|
"""
|
||||||
Queryset of all buses of the associated wei.
|
Queryset of all buses of the associated wei.
|
||||||
"""
|
"""
|
||||||
return Bus.objects.filter(wei__year=cls.get_survey_class().get_year(), size__gt=0)
|
if not hasattr(cls, '_buses'):
|
||||||
|
cls._buses = Bus.objects.filter(wei__year=cls.get_survey_class().get_year(), size__gt=0).all()
|
||||||
|
return cls._buses
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_bus_information(cls, bus):
|
def get_bus_information(cls, bus):
|
||||||
@ -135,7 +145,10 @@ class WEISurvey:
|
|||||||
"""
|
"""
|
||||||
The WEI associated to this kind of survey.
|
The WEI associated to this kind of survey.
|
||||||
"""
|
"""
|
||||||
return WEIClub.objects.get(year=cls.get_year())
|
if not hasattr(cls, '_wei'):
|
||||||
|
cls._wei = WEIClub.objects.get(year=cls.get_year())
|
||||||
|
|
||||||
|
return cls._wei
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_survey_information_class(cls):
|
def get_survey_information_class(cls):
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
|
# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
from functools import lru_cache
|
||||||
from random import Random
|
from random import Random
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
@ -135,15 +137,18 @@ class WEISurvey2021(WEISurvey):
|
|||||||
"""
|
"""
|
||||||
return self.information.step == 20
|
return self.information.step == 20
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
def score(self, bus):
|
def score(self, bus):
|
||||||
if not self.is_complete():
|
if not self.is_complete():
|
||||||
raise ValueError("Survey is not ended, can't calculate score")
|
raise ValueError("Survey is not ended, can't calculate score")
|
||||||
bus_info = self.get_algorithm_class().get_bus_information(bus)
|
bus_info = self.get_algorithm_class().get_bus_information(bus)
|
||||||
return sum(bus_info.scores[getattr(self.information, 'word' + str(i))] for i in range(1, 21)) / 20
|
return sum(bus_info.scores[getattr(self.information, 'word' + str(i))] for i in range(1, 21)) / 20
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
def scores_per_bus(self):
|
def scores_per_bus(self):
|
||||||
return {bus: self.score(bus) for bus in self.get_algorithm_class().get_buses()}
|
return {bus: self.score(bus) for bus in self.get_algorithm_class().get_buses()}
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
def ordered_buses(self):
|
def ordered_buses(self):
|
||||||
values = list(self.scores_per_bus().items())
|
values = list(self.scores_per_bus().items())
|
||||||
values.sort(key=lambda item: -item[1])
|
values.sort(key=lambda item: -item[1])
|
||||||
|
Loading…
Reference in New Issue
Block a user