mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 20:09:25 +00:00
Optimize barmen timeout and counter state fetch
Le timeout se fait en une seule requête et la récupération de l'état des comptoirs en une seule requête aussi. Grâce à ça, on peut en grande partie retirer le cache pour l'affichage de l'état des comptoirs, ce qui a des implications excellentes en termes d'UX (comme le fait que la redirection vers la page de comptoir ou d'activité aura plus une apparence de truc aléatoire)
This commit is contained in:
@ -358,7 +358,7 @@ class Product(models.Model):
|
||||
|
||||
|
||||
class CounterQuerySet(models.QuerySet):
|
||||
def annotate_has_barman(self, user: User) -> CounterQuerySet:
|
||||
def annotate_has_barman(self, user: User) -> Self:
|
||||
"""Annotate the queryset with the `user_is_barman` field.
|
||||
|
||||
For each counter, this field has value True if the user
|
||||
@ -383,6 +383,29 @@ class CounterQuerySet(models.QuerySet):
|
||||
subquery = user.counters.filter(pk=OuterRef("pk"))
|
||||
return self.annotate(has_annotated_barman=Exists(subquery))
|
||||
|
||||
def annotate_is_open(self) -> Self:
|
||||
"""Annotate tue queryset with the `is_open` field.
|
||||
|
||||
For each counter, if `is_open=True`, then the counter is currently opened.
|
||||
Else the counter is closed.
|
||||
"""
|
||||
return self.annotate(
|
||||
is_open=Exists(
|
||||
Permanency.objects.filter(counter_id=OuterRef("pk"), end=None)
|
||||
)
|
||||
)
|
||||
|
||||
def handle_timeout(self) -> int:
|
||||
"""Disconnect the barmen who are inactive in the given counters.
|
||||
|
||||
Returns:
|
||||
The number of affected rows (ie, the number of timeouted permanences)
|
||||
"""
|
||||
timeout = timezone.now() - timedelta(minutes=settings.SITH_BARMAN_TIMEOUT)
|
||||
return Permanency.objects.filter(
|
||||
counter__in=self, end=None, activity__lt=timeout
|
||||
).update(end=F("activity"))
|
||||
|
||||
|
||||
class Counter(models.Model):
|
||||
name = models.CharField(_("name"), max_length=30)
|
||||
@ -450,20 +473,10 @@ class Counter(models.Model):
|
||||
|
||||
@cached_property
|
||||
def barmen_list(self) -> list[User]:
|
||||
return self.get_barmen_list()
|
||||
|
||||
def get_barmen_list(self) -> list[User]:
|
||||
"""Returns the barman list as list of User.
|
||||
|
||||
Also handle the timeout of the barmen
|
||||
"""
|
||||
perms = self.permanencies.filter(end=None)
|
||||
|
||||
# disconnect barmen who are inactive
|
||||
timeout = timezone.now() - timedelta(minutes=settings.SITH_BARMAN_TIMEOUT)
|
||||
perms.filter(activity__lte=timeout).update(end=F("activity"))
|
||||
|
||||
return [p.user for p in perms.select_related("user")]
|
||||
"""Returns the barman list as list of User."""
|
||||
return [
|
||||
p.user for p in self.permanencies.filter(end=None).select_related("user")
|
||||
]
|
||||
|
||||
def get_random_barman(self) -> User:
|
||||
"""Return a random user being currently a barman."""
|
||||
|
Reference in New Issue
Block a user