Waiting list: Fix pathological performance on large series with seating (#4169)

This commit is contained in:
Raphael Michel
2024-05-23 11:51:48 +02:00
committed by GitHub
parent 2619a658c9
commit e93e5c047c
2 changed files with 50 additions and 20 deletions
+30 -20
View File
@@ -20,6 +20,7 @@
# <https://www.gnu.org/licenses/>. # <https://www.gnu.org/licenses/>.
# #
import sys import sys
from collections import defaultdict
from datetime import timedelta from datetime import timedelta
from django.db import transaction from django.db import transaction
@@ -49,19 +50,28 @@ def assign_automatically(event: Event, user_id: int=None, subevent_id: int=None)
quota_cache = {} quota_cache = {}
gone = set() gone = set()
seats_available = {} _seats_available_cache = {}
seats_used = defaultdict(int)
for m in SeatCategoryMapping.objects.filter(event=event).select_related('subevent'): seated_product_set = set(
SeatCategoryMapping.objects.filter(event=event).values_list('product_id', 'subevent_id')
)
def _seats_available(item, subevent):
# See comment in WaitingListEntry.send_voucher() for rationale # See comment in WaitingListEntry.send_voucher() for rationale
num_free_seets_for_product = (m.subevent or event).free_seats().filter(product_id=m.product_id).count() subevent_id = subevent.pk if subevent else None
num_valid_vouchers_for_product = event.vouchers.filter( if (item.pk, subevent_id) not in _seats_available_cache:
Q(valid_until__isnull=True) | Q(valid_until__gte=now()), num_free_seats_for_product = (subevent or event).free_seats().filter(product_id=item.pk).count()
block_quota=True, num_valid_vouchers_for_product = event.vouchers.filter(
item_id=m.product_id, Q(valid_until__isnull=True) | Q(valid_until__gte=now()),
subevent_id=m.subevent_id, block_quota=True,
waitinglistentries__isnull=False item_id=item.pk,
).aggregate(free=Sum(F('max_usages') - F('redeemed')))['free'] or 0 subevent_id=subevent_id,
seats_available[(m.product_id, m.subevent_id)] = num_free_seets_for_product - num_valid_vouchers_for_product waitinglistentries__isnull=False
).aggregate(free=Sum(F('max_usages') - F('redeemed')))['free'] or 0
_seats_available_cache[item.pk, subevent_id] = num_free_seats_for_product - num_valid_vouchers_for_product
return _seats_available_cache[item.pk, subevent_id] - seats_used[item.pk, subevent_id]
prefetch_related_objects( prefetch_related_objects(
[event.organizer], [event.organizer],
@@ -103,7 +113,7 @@ def assign_automatically(event: Event, user_id: int=None, subevent_id: int=None)
lock_objects(quotas, shared_lock_objects=[event]) lock_objects(quotas, shared_lock_objects=[event])
for wle in qs: for wle in qs:
if (wle.item, wle.variation, wle.subevent) in gone: if (wle.item_id, wle.variation_id, wle.subevent_id) in gone:
continue continue
ev = (wle.subevent or event) ev = (wle.subevent or event)
if not ev.presale_is_running or (wle.subevent and not wle.subevent.active): if not ev.presale_is_running or (wle.subevent and not wle.subevent.active):
@@ -111,15 +121,15 @@ def assign_automatically(event: Event, user_id: int=None, subevent_id: int=None)
if wle.subevent and not wle.subevent.presale_is_running: if wle.subevent and not wle.subevent.presale_is_running:
continue continue
if event.settings.waiting_list_auto_disable and event.settings.waiting_list_auto_disable.datetime(wle.subevent or event) <= now(): if event.settings.waiting_list_auto_disable and event.settings.waiting_list_auto_disable.datetime(wle.subevent or event) <= now():
gone.add((wle.item, wle.variation, wle.subevent)) gone.add((wle.item_id, wle.variation_id, wle.subevent_id))
continue continue
if not wle.item.is_available(): if not wle.item.is_available():
gone.add((wle.item, wle.variation, wle.subevent)) gone.add((wle.item_id, wle.variation_id, wle.subevent_id))
continue continue
if (wle.item_id, wle.subevent_id) in seats_available: if (wle.item_id, wle.subevent_id) in seated_product_set:
if seats_available[wle.item_id, wle.subevent_id] < 1: if _seats_available(wle.item, wle.subevent) < 1:
gone.add((wle.item, wle.variation, wle.subevent)) gone.add((wle.item_id, wle.variation_id, wle.subevent_id))
continue continue
availability = ( availability = (
@@ -141,10 +151,10 @@ def assign_automatically(event: Event, user_id: int=None, subevent_id: int=None)
quota_cache[q.pk][1] - 1 if quota_cache[q.pk][1] is not None else sys.maxsize quota_cache[q.pk][1] - 1 if quota_cache[q.pk][1] is not None else sys.maxsize
) )
if (wle.item_id, wle.subevent_id) in seats_available: if (wle.item_id, wle.subevent_id) in seated_product_set:
seats_available[wle.item_id, wle.subevent_id] -= 1 seats_used[wle.item_id, wle.subevent_id] += 1
else: else:
gone.add((wle.item, wle.variation, wle.subevent)) gone.add((wle.item_id, wle.variation_id, wle.subevent_id))
return sent return sent
+20
View File
@@ -194,6 +194,26 @@ class WaitingListTestCase(TestCase):
assert WaitingListEntry.objects.filter(voucher__isnull=True).count() == 10 assert WaitingListEntry.objects.filter(voucher__isnull=True).count() == 10
assert Voucher.objects.count() == 10 assert Voucher.objects.count() == 10
def test_send_auto_no_seat(self):
with scope(organizer=self.o):
self.quota.items.add(self.item1)
self.quota.size = 10
self.quota.save()
self.event.seat_category_mappings.create(
layout_category='Stalls', product=self.item1
)
self.event.seats.create(seat_number="Foo", product=self.item1, seat_guid="Foo", blocked=True)
self.event.seats.create(seat_number="Bar", product=self.item1, seat_guid="Bar", blocked=True)
self.event.seats.create(seat_number="Baz", product=self.item1, seat_guid="Baz", blocked=True)
WaitingListEntry.objects.create(
event=self.event, item=self.item1, email='foo@bar.com'
)
assign_automatically.apply(args=(self.event.pk,))
assert Voucher.objects.count() == 0
self.event.seats.create(seat_number="Baz", product=self.item1, seat_guid="Baz", blocked=False)
assign_automatically.apply(args=(self.event.pk,))
assert Voucher.objects.count() == 1
def test_send_periodic_event_over(self): def test_send_periodic_event_over(self):
self.event.settings.set('waiting_list_enabled', True) self.event.settings.set('waiting_list_enabled', True)
self.event.settings.set('waiting_list_auto', True) self.event.settings.set('waiting_list_auto', True)