Skip to content

Commit

Permalink
Merge pull request #7101 from readthedocs/show-last-total
Browse files Browse the repository at this point in the history
Search: show total_results from last query
  • Loading branch information
stsewd authored May 21, 2020
2 parents 0d7901e + 2b67e53 commit 204bc43
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from allauth.socialaccount.models import SocialAccount
from django.conf import settings
from django.contrib import messages
from django.db.models import Count
from django.db.models import Count, OuterRef, Subquery
from django.http import (
Http404,
HttpResponseBadRequest,
Expand Down Expand Up @@ -39,10 +39,7 @@
Version,
VersionAutomationRule,
)
from readthedocs.core.mixins import (
ListViewWithForm,
PrivateViewMixin,
)
from readthedocs.core.mixins import ListViewWithForm, PrivateViewMixin
from readthedocs.core.utils import broadcast, trigger_build
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.integrations.models import HttpExchange, Integration
Expand Down Expand Up @@ -996,18 +993,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(
{
Expand Down

0 comments on commit 204bc43

Please sign in to comment.