Skip to content
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

Search: track total results #7080

Merged
merged 3 commits into from
May 14, 2020
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
8 changes: 4 additions & 4 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ def get_context_data(self, **kwargs):
qs.values('query')
.annotate(count=Count('id'))
.order_by('-count', 'query')
.values_list('query', 'count')
.values_list('query', 'count', 'total_results')
)

# only show top 100 queries
Expand All @@ -1040,7 +1040,7 @@ def _search_analytics_csv_data(self):
created__date__lte=now,
)
.order_by('-created')
.values_list('created', 'query')
.values_list('created', 'query', 'total_results')
stsewd marked this conversation as resolved.
Show resolved Hide resolved
)

file_name = '{project_slug}_from_{start}_to_{end}.csv'.format(
Expand All @@ -1052,8 +1052,8 @@ def _search_analytics_csv_data(self):
file_name = '-'.join([text for text in file_name.split() if text])

csv_data = (
[timezone.datetime.strftime(time, '%Y-%m-%d %H:%M:%S'), query]
for time, query in data
[timezone.datetime.strftime(time, '%Y-%m-%d %H:%M:%S'), query, total_results]
for time, query, total_results in data
)
pseudo_buffer = Echo()
writer = csv.writer(pseudo_buffer)
Expand Down
18 changes: 9 additions & 9 deletions readthedocs/rtd_tests/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,15 @@ def test_top_queries(self):
test_time.return_value = self.test_time

expected_result = [
('hello world', 5),
('documentation', 4),
('read the docs', 4),
('advertising', 3),
('elasticsearch', 2),
('sphinx', 2),
('github', 1),
('hello', 1),
('search', 1),
('hello world', 5, 0),
('documentation', 4, 0),
('read the docs', 4, 0),
('advertising', 3, 0),
('elasticsearch', 2, 0),
('sphinx', 2, 0),
('github', 1, 0),
('hello', 1, 0),
('search', 1, 0),
]

resp = self.client.get(self.analyics_page)
Expand Down
18 changes: 18 additions & 0 deletions readthedocs/search/migrations/0002_add_total_results_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.12 on 2020-05-14 17:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('search', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='searchquery',
name='total_results',
field=models.IntegerField(default=0, null=True, verbose_name='Total results'),
),
]
6 changes: 6 additions & 0 deletions readthedocs/search/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class SearchQuery(TimeStampedModel):
_('Query'),
max_length=4092,
)
total_results = models.IntegerField(
_('Total results'),
default=0,
# TODO: to avoid downtime, remove later.
null=True,
Comment on lines +38 to +39
Copy link
Member Author

Choose a reason for hiding this comment

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

If it's ok to not save some search queries during deploy I can just remove this.

)
objects = RelatedProjectQuerySet.as_manager()

class Meta:
Expand Down
16 changes: 8 additions & 8 deletions readthedocs/search/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def delete_old_search_queries_from_db():

@app.task(queue='web')
def record_search_query(project_slug, version_slug, query, total_results, time_string):
"""Record/update search query in database."""
"""Record/update a search query for analytics."""
if not project_slug or not version_slug or not query:
log.debug(
'Not recording the search query. Passed arguments: '
Expand All @@ -162,11 +162,12 @@ def record_search_query(project_slug, version_slug, query, total_results, time_s
modified__gte=before_10_sec,
).order_by('-modified')

# check if partial query exists,
# if yes, then just update the object.
# If a partial query exists,
# then just update that object.
for partial_query in partial_query_qs.iterator():
if query.startswith(partial_query.query):
partial_query.query = query
partial_query.total_results = total_results
partial_query.save()
return

Expand All @@ -190,9 +191,9 @@ def record_search_query(project_slug, version_slug, query, total_results, time_s
)
return

version_qs = Version.objects.filter(project=project, slug=version_slug)
version = Version.objects.filter(project=project, slug=version_slug).first()

if not version_qs.exists():
if not version:
log.debug(
'Not recording the search query because version does not exist. '
'project_slug: %s, version_slug: %s' % (
Expand All @@ -201,11 +202,10 @@ def record_search_query(project_slug, version_slug, query, total_results, time_s
)
return

version = version_qs.first()

# make a new SearchQuery object.
# Create a new SearchQuery object.
SearchQuery.objects.create(
project=project,
version=version,
query=query,
total_results=total_results,
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ <h3>{% trans "Top queries" %}</h3>
<div class="module-list">
<div class="module-list-wrapper">
<ul class="long-list-overflow">
{% for query, count in queries %}
{% for query, count, total_results in queries %}
<li class="module-item">
{{ query }}
({{ total_results }} result{{ total_results|pluralize:"s" }})
<span class="right quiet">
{{ count }} search{{ count|pluralize:"es" }}
</span>
Expand Down