Skip to content

Optimizations and UX improvements to the dashboard screen #5637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions readthedocs/core/templatetags/privacy_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,11 @@ def is_admin(user, project):

@register.simple_tag(takes_context=True)
def get_public_projects(context, user):
# Creates a Subquery object which returns the latest builds 'id'.
# Used for optimization purpose.
subquery = Subquery(
Build.objects.filter(
project=OuterRef('project_id')).values_list('id', flat=True)[:1]
)
# Filters the latest builds of projects.
latest_build = Prefetch('builds', Build.objects.filter(
pk__in=subquery), to_attr='_latest_build'
)
# 'Exists()' checks if the project has any good builds.
projects = Project.objects.for_user_and_viewer(
user=user,
viewer=context['request'].user,
).prefetch_related('users', latest_build).annotate(
).prefetch_latest_build().annotate(
_good_build=Exists(
Build.objects.filter(success=True, project=OuterRef('pk')))
)
Expand Down
5 changes: 4 additions & 1 deletion readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ class Project(models.Model):
objects = ProjectQuerySet.as_manager()
all_objects = models.Manager()

# Property used for storing the latest build for a project when prefetching
LATEST_BUILD_CACHE = '_latest_build'

class Meta:
ordering = ('slug',)
permissions = (
Expand Down Expand Up @@ -857,7 +860,7 @@ def get_latest_build(self, finished=True):
"""
# Check if there is `_latest_build` attribute in the Queryset.
# Used for Database optimization.
if hasattr(self, '_latest_build'):
if hasattr(self, self.LATEST_BUILD_CACHE):
if self._latest_build:
return self._latest_build[0]
return None
Expand Down
26 changes: 24 additions & 2 deletions readthedocs/projects/querysets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Project model QuerySet classes."""

from django.db import models
from django.db.models import Q
from django.db.models import Q, OuterRef, Subquery, Prefetch
from guardian.shortcuts import get_objects_for_user

from readthedocs.core.utils.extend import SettingsOverrideObject
Expand Down Expand Up @@ -75,10 +75,32 @@ def is_active(self, project):

return True

def prefetch_latest_build(self):
"""
For a given queryset of projects, prefetch the latest build for each project

This should come after any filtering.
"""
from readthedocs.builds.models import Build

# Prefetch the latest build for each project.
subquery = Subquery(
Build.objects.filter(
project=OuterRef('project_id')
).order_by('-date').values_list('id', flat=True)[:1]
)
latest_build = Prefetch(
'builds',
Build.objects.filter(pk__in=subquery),
to_attr=self.model.LATEST_BUILD_CACHE,
)
return self.prefetch_related(latest_build)

# Aliases

def dashboard(self, user=None):
return self.for_admin_user(user)
"""Get the projects for this user including the latest build"""
return self.for_admin_user(user).prefetch_latest_build()

def api(self, user=None, detail=True):
if detail:
Expand Down
10 changes: 1 addition & 9 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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,
Expand Down Expand Up @@ -92,14 +91,7 @@ def validate_primary_email(self, user):
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(
build_count=Count('builds'), latest_build_success=sub_query)
return Project.objects.dashboard(self.request.user)

def get(self, request, *args, **kwargs):
self.validate_primary_email(request.user)
Expand Down
29 changes: 12 additions & 17 deletions readthedocs/templates/projects/project_dashboard_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,22 @@ <h3>{% trans "Projects" %}</h3>
{% block project-name %}
{{ project.name }}
{% endblock %}
{% with builds=project.build_count %}
{% if builds == 0 %}
<span class="right quiet">
{% trans "No builds yet" %}
</span>
{% else %}
<span class="right quiet">
<span class="build-count">
{% blocktrans trimmed count counter=builds %}
1 build
{% plural %}
{{ builds }} builds
{% endblocktrans %}
</span>
{% if project.latest_build_success %}
{% with build=project.get_latest_build %}
<span class="right quiet">

{% if build %}
<time class="build-count" datetime="{{ build.date|date:"c" }}" title="{{ build.date|date:"DATETIME_FORMAT" }}">
<small>{% blocktrans with date=build.date|timesince %}{{ date }} ago{% endblocktrans %}</small>
</time>
{% if build.success %}
<span class="build-state build-state-passing">{% trans "passing" %}</span>
{% else %}
<span class="build-state build-state-failing">{% trans "failing" %}</span>
{% endif %}
</span>
{% endif %}
{% else %}
<span>{% trans "No builds yet" %}</span>
{% endif %}
</span>
{% endwith %}
</a>
</li>
Expand Down