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" aria-labelledby="schedule_day_{{ timetable.day.date|date:"l"|lower}}-tab"
> >
<div class="table-responsive hidden-md-down"> <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" %} {% include "symposion/schedule/_grid.html" %}
{% endcache %} {% endcache %}
</div> </div>
<div class="mobile-schedule"> <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" %} {% include "symposion/schedule/_mobile.html" %}
{% endcache %} {% endcache %}
</div> </div>

View file

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