From 598ab3c71f4966bc7446dc7a5c725e374f715589 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Tue, 19 May 2020 13:04:11 -0500 Subject: [PATCH 1/2] Search: show total_results from last query --- readthedocs/projects/views/private.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/readthedocs/projects/views/private.py b/readthedocs/projects/views/private.py index c27d0ec7cae..e5c28de0300 100644 --- a/readthedocs/projects/views/private.py +++ b/readthedocs/projects/views/private.py @@ -7,7 +7,7 @@ from django.conf import settings from django.contrib import messages from django.contrib.auth.decorators import login_required -from django.db.models import Count +from django.db.models import Count, OuterRef, Subquery from django.http import ( Http404, HttpResponseBadRequest, @@ -1006,18 +1006,21 @@ def get_context_data(self, **kwargs): project.slug, ) - queries = [] - qs = SearchQuery.objects.filter(project=project) - if qs.exists(): - qs = ( - qs.values('query') - .annotate(count=Count('id')) - .order_by('-count', 'query') - .values_list('query', 'count', 'total_results') + project_queries = SearchQuery.objects.filter(project=project) + last_total_results = ( + project_queries.filter(query=OuterRef('query')) + .order_by('-modified') + .values('total_results') + ) + queries = ( + project_queries.values('query') + .annotate( + count=Count('id'), + total_results=Subquery(last_total_results[:1]) ) - - # only show top 100 queries - queries = qs[:100] + .order_by('-count', 'query') + .values_list('query', 'count', 'total_results') + )[:100] context.update( { From 2b67e53c59b591206da5e7701cce9c32c9cb2b76 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Tue, 19 May 2020 13:09:01 -0500 Subject: [PATCH 2/2] Remove unused import --- readthedocs/projects/views/private.py | 1 - 1 file changed, 1 deletion(-) diff --git a/readthedocs/projects/views/private.py b/readthedocs/projects/views/private.py index 44bceeeda23..8ef7570ebfe 100644 --- a/readthedocs/projects/views/private.py +++ b/readthedocs/projects/views/private.py @@ -6,7 +6,6 @@ from allauth.socialaccount.models import SocialAccount from django.conf import settings from django.contrib import messages -from django.contrib.auth.decorators import login_required from django.db.models import Count, OuterRef, Subquery from django.http import ( Http404,