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 1 commit
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
6 changes: 6 additions & 0 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,12 @@ def get_latest_build(self, finished=True):

:param finished: Return only builds that are in a finished state
"""
if hasattr(self, '_latest_build'):
# Cached latest build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be useful to extend this comment indicating where this comes from (dashboard queryset), why it's needed or similar since the source it's in another file and I think we will not find this quickly when reading the code later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. How about after #5472 is merged I'll refactor this slightly to have a class variable for caching it.

if self._latest_build:
return self._latest_build[0]
return None

kwargs = {'type': 'html'}
if finished:
kwargs['state'] = 'finished'
Expand Down
19 changes: 17 additions & 2 deletions readthedocs/projects/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,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 @@ -80,7 +80,22 @@ def is_active(self, project):
# Aliases

def dashboard(self, user=None):
return self.for_admin_user(user)
"""Get the projects for this user including the latest build"""
from readthedocs.builds.models import Build

projects = self.for_admin_user(user)

# Prefetch the latest build for each project.
subquery = Subquery(
Build.objects.filter(project=OuterRef('project_id')).values_list('id', flat=True)[:1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not needed because ordering = ['-date'] in the model, but just in case: don't we need to order_by here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't necessary due to the default ordering but maybe it is best to be explicit.

)
latest_build = Prefetch(
'builds',
Build.objects.filter(pk__in=subquery),
to_attr='_latest_build',
)

return projects.prefetch_related(latest_build)

def api(self, user=None):
return self.public(user)
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