Skip to content

Commit

Permalink
fix: performance improvments main page
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsCalebJones committed Aug 12, 2024
1 parent 02dd6d1 commit c3e337b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
8 changes: 4 additions & 4 deletions src/spacelaunchnow/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from django.contrib.sitemaps import views as sitemaps_views
from django.http import HttpResponse
from django.urls import include, path, re_path
from django.views.decorators.cache import cache_page
from django.views.generic import TemplateView

import web
Expand Down Expand Up @@ -52,12 +51,12 @@
re_path(r"^robots\.txt", include("robots.urls")),
path(
"sitemap.xml/",
cache_page(86400)(sitemaps_views.index),
sitemaps_views.index,
{"sitemaps": sitemaps, "sitemap_url_name": "sitemaps"},
),
path(
"sitemap-<section>.xml",
cache_page(86400)(sitemaps_views.sitemap),
sitemaps_views.sitemap,
{"sitemaps": sitemaps},
name="sitemaps",
),
Expand Down Expand Up @@ -121,7 +120,8 @@ def health(request):
path("launch/upcoming/vandenberg", landing_views.launches_vandenberg, name="launches_vandenberg"),
path("spacex/", landing_views.launches_spacex, name="direct_launches_spacex"),
path("florida/", landing_views.launches_florida, name="direct_launches_florida"),
path("launch/<int:id>/", landing_views.launch_by_id, name="launch_by_id"),
path("launch/schedule/", LaunchListView.as_view()),
path("launch/<uuid:uuid>/", landing_views.launch_by_uuid, name="launch_by_uuid"),
re_path(r"^launch/(?P<slug>[-\w]+)/$", landing_views.launch_by_slug, name="launch_by_slug"),
path("starship/", landing_views.starship_page, name="starship_page"),
path("event/", landing_views.events_list, name="events_list"),
Expand Down
10 changes: 5 additions & 5 deletions src/web/templates/web/launches/launch_detail_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ <h1 class="title ">Watch the Launch</h1>
{% endif %}
</div>
{% endvideo %}
{% if launch.vid_urls.all|length > 1 or launch.info_urls.all|length > 0 %}
{% if vids|length > 1 or launch.info_urls.all|length > 0 %}
<h2 class="title">Additional Media</h2>
{% for item in launch.vid_urls.all|slice:"1:" %}
{% for item in vids|slice:"1:" %}
<div class="card card-plain card-blog">
<div class="row">
<div class="col-md-5 mx-auto">
Expand Down Expand Up @@ -158,7 +158,7 @@ <h3 class="card-title">{{ item.title }}</h3>
</div>
</div>
{% endfor %}
{% for info in launch.info_urls.all %}
{% for info in infos %}
{% if info.title and info.description %}
<div class="card card-plain card-blog">
<div class="row">
Expand Down Expand Up @@ -263,7 +263,7 @@ <h2 class="title">Telemetry</h2>
</div>
</div>
{% if updates|length > 0 %}
{% with list_updates=updates|slice:"3" %}
{% with list_updates=updates|slice:"5" %}
<div class="row">
<div class="card">
<div class="card-header card-header-info text-center">
Expand All @@ -272,7 +272,7 @@ <h1 class="title">Updates</h1>
<div class="card-body mt-3">
<div id="updates" class="col-md-10 ml-auto mr-auto">
{% include 'web/views/small_update.html' %}
{% if updates|length > 3 %}
{% if updates|length > 5 %}
<div class="text-center">
<a class="btn btn-primary btn" id="lazyLoadLink"
href="javascript:void(0);" data-page="2">Load More</a>
Expand Down
75 changes: 47 additions & 28 deletions src/web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
from datetime import timedelta
from itertools import chain
from uuid import UUID

from api.models import (
Agency,
Expand Down Expand Up @@ -200,12 +199,13 @@ def app(request):
@cache_page(60)
# Create your views here.
def next_launch(request):
in_flight_launch = get_prefetched_launch_queryset(Launch.objects.filter(status__id=6)).order_by("-net").first()
in_flight_launch = Launch.objects.filter(status__id=6).order_by("-net").fields("slug").first()
if in_flight_launch:
return redirect("launch_by_slug", slug=in_flight_launch.slug)
recently_launched = (
get_prefetched_launch_queryset(Launch.objects.filter(net__gte=UTC_NOW - timedelta(hours=6), net__lte=UTC_NOW))
Launch.objects.filter(net__gte=UTC_NOW - timedelta(hours=6), net__lte=UTC_NOW)
.order_by("-net")
.fields("slug")
.first()
)
if recently_launched:
Expand All @@ -215,41 +215,51 @@ def next_launch(request):
return redirect("launch_by_slug", slug=_next_launch.slug)


@cache_page(120)
# Create your views here.
def launch_by_slug(request, slug):
if slug == "schedule":
redirect("launch_schedule")
def launch_by_uuid(request, uuid):
try:
UUID(slug, version=4)
try:
launch = Launch.objects.get(id=slug)
if str(launch.id) == launch.slug:
return create_launch_view(request, Launch.objects.get(slug=slug))
return redirect("launch_by_slug", slug=launch.slug)
except ObjectDoesNotExist:
return redirect("launches")
except ValueError:
# If it's a value error, then the string
# is not a valid hex code for a UUID.
try:
return create_launch_view(request, Launch.objects.get(slug=slug))
except ObjectDoesNotExist:
return redirect("launches")
launch = (
Launch.objects.select_related("mission")
.select_related("image")
.select_related("status")
.select_related("changed_by")
.select_related("net_precision")
.select_related("rocket")
.prefetch_related("changed_by__tsdstaff")
.prefetch_related("rocket__firststage")
.prefetch_related("vid_urls")
.prefetch_related("info_urls")
.get(id=uuid)
)
return create_launch_view(request, launch)
except Launch.DoesNotExist as e:
raise Http404("Launch with the specified UUID does not exist.") from e


# Create your views here.
def launch_by_id(request, id):
def launch_by_slug(request, slug):
try:
return redirect("launch_by_slug", slug=Launch.objects.get(launch_library_id=id).slug)
except ObjectDoesNotExist:
return redirect("launches")
launch = (
Launch.objects.select_related("mission")
.select_related("image")
.select_related("status")
.select_related("changed_by")
.select_related("net_precision")
.select_related("rocket")
.prefetch_related("changed_by__tsdstaff")
.prefetch_related("rocket__firststage")
.prefetch_related("vid_urls")
.prefetch_related("info_urls")
.get(slug=slug)
)
return create_launch_view(request, launch)
except Launch.DoesNotExist as e:
raise Http404("Launch with the specified UUID does not exist.") from e


@cache_page(120)
def create_launch_view(request, launch):
youtube_urls = []
vids = launch.vid_urls.all()
infos = launch.info_urls.all()
status = launch.status.full_name
agency = launch.rocket.configuration.manufacturer
launches_good = get_prefetched_launch_queryset(
Expand Down Expand Up @@ -294,6 +304,8 @@ def create_launch_view(request, launch):
"launches": launches,
"previous_launches": previous_launches,
"updates": launch.updates.all(),
"vids": vids,
"infos": infos,
},
)

Expand Down Expand Up @@ -1152,6 +1164,11 @@ def lazy_load_updates(request, id):
launch = Launch.objects.get(id=id)
page = request.POST.get("page")
updates = launch.updates.all()
if page is None:
return HttpResponse(status=204)

if len(updates) == 0:
return HttpResponse(status=204)

# use Django's pagination
# https://docs.djangoproject.com/en/dev/topics/pagination/
Expand All @@ -1163,6 +1180,8 @@ def lazy_load_updates(request, id):
updates = paginator.page(2)
except EmptyPage:
updates = paginator.page(paginator.num_pages)
except Exception:
return HttpResponse(status=204)

# build a html posts list with the paginated posts
updates_html = loader.render_to_string("web/views/small_update.html", {"list_updates": updates})
Expand Down

0 comments on commit c3e337b

Please sign in to comment.