Skip to content

Commit

Permalink
Merge pull request #1111 from Metro-Records/feature/1109-optimize-hom…
Browse files Browse the repository at this point in the history
…epage

Optimize homepage loadtime
  • Loading branch information
antidipyramid authored May 28, 2024
2 parents b94bae4 + fbef1a2 commit b8d5840
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
43 changes: 29 additions & 14 deletions lametro/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.utils.text import slugify
from django.utils import timezone
from django.utils.functional import cached_property
from django.core.cache import cache

from django.db.models import Prefetch, Case, When, Value, Q, F
from django.db.models.functions import Now, Cast
Expand Down Expand Up @@ -606,6 +607,7 @@ def most_recent_past_meetings(cls):

meetings_in_past_two_weeks = (
cls.objects.with_media()
.prefetch_related("broadcast")
.filter(start_time__gte=two_weeks_ago)
.order_by("-start_time")
)
Expand All @@ -625,11 +627,11 @@ def upcoming_board_meetings(cls):
homepage by returning all upcoming board meetings scheduled for the
month of the next upcoming meeting.
"""
board_meetings = cls.objects.filter(
name__icontains="Board Meeting", start_time__gt=timezone.now()
).order_by("start_time")

next_meeting = board_meetings.first()
board_meetings = (
cls.objects.select_related("location")
.filter(name__icontains="Board Meeting", start_time__gt=timezone.now())
.order_by("start_time")
)

if board_meetings.exists():
next_meeting = board_meetings.first()
Expand Down Expand Up @@ -675,9 +677,13 @@ def _potentially_current_meetings(cls):
pk__in=cls._streaming_meeting().values_list("pk")
)

return cls.objects.filter(
start_time__gte=six_hours_ago, start_time__lte=five_minutes_from_now
).exclude(was_cancelled | has_passed)
return (
cls.objects.prefetch_related("broadcast")
.filter(
start_time__gte=six_hours_ago, start_time__lte=five_minutes_from_now
)
.exclude(was_cancelled | has_passed)
)

@classmethod
def _streaming_meeting(cls):
Expand All @@ -691,12 +697,20 @@ def _streaming_meeting(cls):
queryset.
"""

try:
running_events = requests.get(
"http://metro.granicus.com/running_events.php", timeout=5
)
except (requests.exceptions.ConnectTimeout, requests.exceptions.ReadTimeout):
return cls.objects.none()
running_events = cache.get("running_events")
if not running_events:
try:
running_events = requests.get(
"http://metro.granicus.com/running_events.php", timeout=5
)
except (
requests.exceptions.ConnectTimeout,
requests.exceptions.ReadTimeout,
):
return cls.objects.none()

# Cache running events for one minute
cache.set("running_events", running_events, 60)

if running_events.status_code == 200:
for guid in running_events.json():
Expand Down Expand Up @@ -780,6 +794,7 @@ def upcoming_committee_meetings(cls):
# board meeting. Show the board meeting last.
meetings = (
cls.objects.filter(start_time__gt=timezone.now())
.select_related("location")
.annotate(
is_board_meeting=RawSQL(
"opencivicdata_event.name like %s", ("%Board Meeting%",)
Expand Down
23 changes: 12 additions & 11 deletions lametro/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,27 @@ class LAMetroIndexView(IndexView):

event_model = LAMetroEvent

@property
def extra_context(self):
extra = {}
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

extra["upcoming_board_meetings"] = self.event_model.upcoming_board_meetings()[
context["upcoming_board_meetings"] = self.event_model.upcoming_board_meetings()[
:2
]
extra["most_recent_past_meetings"] = (
context["most_recent_past_meetings"] = (
self.event_model.most_recent_past_meetings()
)
extra["current_meeting"] = self.event_model.current_meeting()
extra["bilingual"] = bool([e for e in extra["current_meeting"] if e.bilingual])
extra["USING_ECOMMENT"] = settings.USING_ECOMMENT
context["current_meeting"] = self.event_model.current_meeting()
context["bilingual"] = bool(
[e for e in context["current_meeting"] if e.bilingual]
)
context["USING_ECOMMENT"] = settings.USING_ECOMMENT

extra["todays_meetings"] = self.event_model.todays_meetings().order_by(
context["todays_meetings"] = self.event_model.todays_meetings().order_by(
"start_date"
)
extra["form"] = LAMetroCouncilmaticSearchForm()
context["form"] = LAMetroCouncilmaticSearchForm()

return extra
return context


class LABillDetail(BillDetailView):
Expand Down

0 comments on commit b8d5840

Please sign in to comment.