From 02e8bac4d273a1f8a74fe032bf5470234f54eaed Mon Sep 17 00:00:00 2001 From: saadmk11 Date: Sat, 16 Mar 2019 06:24:31 +0600 Subject: [PATCH 1/2] Dashboard screen performance fix --- readthedocs/projects/views/private.py | 9 +++++++-- .../templates/projects/project_dashboard_base.html | 14 ++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/readthedocs/projects/views/private.py b/readthedocs/projects/views/private.py index 04d6d681b..88427c1ee 100644 --- a/readthedocs/projects/views/private.py +++ b/readthedocs/projects/views/private.py @@ -7,6 +7,7 @@ from celery import chain from django.contrib import messages from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User +from django.db.models import Count, OuterRef, Subquery from django.http import ( Http404, HttpResponseBadRequest, @@ -23,7 +24,7 @@ from formtools.wizard.views import SessionWizardView from vanilla import CreateView, DeleteView, DetailView, GenericView, UpdateView from readthedocs.builds.forms import VersionForm -from readthedocs.builds.models import Version +from readthedocs.builds.models import Build, Version from readthedocs.core.mixins import ListViewWithForm, LoginRequiredMixin from readthedocs.core.utils import broadcast, prepare_build, trigger_build from readthedocs.integrations.models import HttpExchange, Integration @@ -92,7 +93,11 @@ class ProjectDashboard(PrivateViewMixin, ListView): notification.send() def get_queryset(self): - return Project.objects.dashboard(self.request.user) + builds = Build.objects.filter( + project=OuterRef('pk'), type='html', state='finished') + sub_query = Subquery(builds.values('success')[:1]) + return Project.objects.dashboard(self.request.user).annotate( + Count('builds'), build_success=sub_query) def get(self, request, *args, **kwargs): self.validate_primary_email(request.user) diff --git a/readthedocs/templates/projects/project_dashboard_base.html b/readthedocs/templates/projects/project_dashboard_base.html index 7394fd85d..6b4ad8d6a 100644 --- a/readthedocs/templates/projects/project_dashboard_base.html +++ b/readthedocs/templates/projects/project_dashboard_base.html @@ -66,7 +66,7 @@ {% block project-name %} {{ project.name }} {% endblock %} - {% with builds=project.builds.count %} + {% with builds=project.builds__count %} {% if builds == 0 %} {% trans "No builds yet" %} @@ -80,13 +80,11 @@ {{ builds }} builds {% endblocktrans %} - {% with build=project.get_latest_build %} - {% if build.success %} - {% trans "passing" %} - {% else %} - {% trans "failing" %} - {% endif %} - {% endwith %} + {% if project.build_success %} + {% trans "passing" %} + {% else %} + {% trans "failing" %} + {% endif %} {% endif %} {% endwith %} From 5bc9e98f0779cdc13a22704a0465e453594467c4 Mon Sep 17 00:00:00 2001 From: saadmk11 Date: Sun, 17 Mar 2019 02:22:42 +0600 Subject: [PATCH 2/2] Added Comments & minor fix. --- readthedocs/projects/views/private.py | 5 ++++- readthedocs/templates/projects/project_dashboard_base.html | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/readthedocs/projects/views/private.py b/readthedocs/projects/views/private.py index 88427c1ee..57e0d0761 100644 --- a/readthedocs/projects/views/private.py +++ b/readthedocs/projects/views/private.py @@ -93,11 +93,14 @@ class ProjectDashboard(PrivateViewMixin, ListView): notification.send() def get_queryset(self): + # Filters the builds for a perticular project. builds = Build.objects.filter( project=OuterRef('pk'), type='html', state='finished') + # Creates a Subquery object which returns + # the value of Build.success of the latest build. sub_query = Subquery(builds.values('success')[:1]) return Project.objects.dashboard(self.request.user).annotate( - Count('builds'), build_success=sub_query) + build_count=Count('builds'), latest_build_success=sub_query) def get(self, request, *args, **kwargs): self.validate_primary_email(request.user) diff --git a/readthedocs/templates/projects/project_dashboard_base.html b/readthedocs/templates/projects/project_dashboard_base.html index 6b4ad8d6a..7a18d58bb 100644 --- a/readthedocs/templates/projects/project_dashboard_base.html +++ b/readthedocs/templates/projects/project_dashboard_base.html @@ -66,7 +66,7 @@ {% block project-name %} {{ project.name }} {% endblock %} - {% with builds=project.builds__count %} + {% with builds=project.build_count %} {% if builds == 0 %} {% trans "No builds yet" %} @@ -80,7 +80,7 @@ {{ builds }} builds {% endblocktrans %} - {% if project.build_success %} + {% if project.latest_build_success %} {% trans "passing" %} {% else %} {% trans "failing" %}