Refactor timeteabel to speed up dashboard

This commit is contained in:
Tobias 2018-12-08 19:25:14 +13:00
parent 45b65340e1
commit 9c64a33fd2
2 changed files with 27 additions and 19 deletions

View file

@ -46,12 +46,12 @@
aria-labelledby="schedule_day_{{ timetable.day.date|date:"l"|lower}}-tab"
>
<div class="table-responsive hidden-md-down">
{% cache 180 cache-schedule timetable.day.date %}
{% cache 0 cach1e-schedule timetable.day.date %}
{% include "symposion/schedule/_grid.html" %}
{% endcache %}
</div>
<div class="mobile-schedule">
{% cache 180 cache-schedule-mobile timetable.day.date %}
{% cache 0 cache-schedule-mobile timetable.day.date %}
{% include "symposion/schedule/_mobile.html" %}
{% endcache %}
</div>

View file

@ -9,32 +9,40 @@ class TimeTable(object):
def __init__(self, day):
self.day = day
self.slots_qs = Slot.objects.filter(day=day)\
.select_related('kind', 'content_ptr__speaker__user')\
.prefetch_related('content_ptr__additional_speakers')
self._times = sorted(
set(itertools.chain(*self.slots_qs.values_list("start", "end")))
)
def slots_qs(self):
qs = Slot.objects.all()
qs = qs.filter(day=self.day)
return qs
def slots(self):
if not hasattr(self, '_slots'):
filters = {
"room_count": Count("slotroom"),
"order": Min("slotroom__room__order")
}
self._slots = self.slots_qs.annotate(**filters).order_by("order")
return self._slots
def rooms(self):
qs = Room.objects.all()
qs = qs.filter(schedule=self.day.schedule)
qs = qs.filter(
pk__in=SlotRoom.objects.filter(slot__in=self.slots_qs().values("pk")).values("room"))
qs = qs.order_by("order")
return qs
if not hasattr(self, '_rooms'):
self._rooms = Room.objects\
.filter(slotroom__slot__in=self.slots_qs)\
.distinct()\
.order_by("order")
return self._rooms
def __iter__(self):
times = sorted(set(itertools.chain(*self.slots_qs().values_list("start", "end"))))
slots = Slot.objects.filter(pk__in=self.slots_qs().values("pk"))
slots = slots.annotate(room_count=Count("slotroom"), order=Min("slotroom__room__order"))
slots = slots.order_by("order")
slots = self.slots()
row = []
total_room_count = self.rooms().count()
for time, next_time in pairwise(times):
row = {"time": time, "slots": []}
for time, next_time in pairwise(self._times):
row = {"time": time, "end": next_time, "slots": []}
row_slots = [ slot for slot in slots if slot.start == time]
for slot in row_slots:
slot.rowspan = TimeTable.rowspan(times, slot.start, slot.end)
slot.rowspan = TimeTable.rowspan(self._times, slot.start, slot.end)
slot.colspan = slot.room_count if not slot.exclusive else total_room_count
row["slots"].append(slot)
if row["slots"] or next_time is None: