forked from CGM_Public/pretix_original
Waiting list: Fix pathological performance on large series with seating (#4169)
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
# <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
import sys
|
||||
from collections import defaultdict
|
||||
from datetime import timedelta
|
||||
|
||||
from django.db import transaction
|
||||
@@ -49,19 +50,28 @@ def assign_automatically(event: Event, user_id: int=None, subevent_id: int=None)
|
||||
|
||||
quota_cache = {}
|
||||
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
|
||||
num_free_seets_for_product = (m.subevent or event).free_seats().filter(product_id=m.product_id).count()
|
||||
num_valid_vouchers_for_product = event.vouchers.filter(
|
||||
Q(valid_until__isnull=True) | Q(valid_until__gte=now()),
|
||||
block_quota=True,
|
||||
item_id=m.product_id,
|
||||
subevent_id=m.subevent_id,
|
||||
waitinglistentries__isnull=False
|
||||
).aggregate(free=Sum(F('max_usages') - F('redeemed')))['free'] or 0
|
||||
seats_available[(m.product_id, m.subevent_id)] = num_free_seets_for_product - num_valid_vouchers_for_product
|
||||
subevent_id = subevent.pk if subevent else None
|
||||
if (item.pk, subevent_id) not in _seats_available_cache:
|
||||
num_free_seats_for_product = (subevent or event).free_seats().filter(product_id=item.pk).count()
|
||||
num_valid_vouchers_for_product = event.vouchers.filter(
|
||||
Q(valid_until__isnull=True) | Q(valid_until__gte=now()),
|
||||
block_quota=True,
|
||||
item_id=item.pk,
|
||||
subevent_id=subevent_id,
|
||||
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(
|
||||
[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])
|
||||
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
|
||||
ev = (wle.subevent or event)
|
||||
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:
|
||||
continue
|
||||
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
|
||||
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
|
||||
|
||||
if (wle.item_id, wle.subevent_id) in seats_available:
|
||||
if seats_available[wle.item_id, wle.subevent_id] < 1:
|
||||
gone.add((wle.item, wle.variation, wle.subevent))
|
||||
if (wle.item_id, wle.subevent_id) in seated_product_set:
|
||||
if _seats_available(wle.item, wle.subevent) < 1:
|
||||
gone.add((wle.item_id, wle.variation_id, wle.subevent_id))
|
||||
continue
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
if (wle.item_id, wle.subevent_id) in seats_available:
|
||||
seats_available[wle.item_id, wle.subevent_id] -= 1
|
||||
if (wle.item_id, wle.subevent_id) in seated_product_set:
|
||||
seats_used[wle.item_id, wle.subevent_id] += 1
|
||||
else:
|
||||
gone.add((wle.item, wle.variation, wle.subevent))
|
||||
gone.add((wle.item_id, wle.variation_id, wle.subevent_id))
|
||||
|
||||
return sent
|
||||
|
||||
|
||||
Reference in New Issue
Block a user