Search: little optimization when saving search queries
Check max 30 queries, in case there is a flood of queries.
Fetch project and version in one query.
@@ -167,35 +167,32 @@ def record_search_query(project_slug, version_slug, query, total_results, time_s
modified__gte=before_10_sec,
).order_by('-modified')
- # If a partial query exists,
- # then just update that object.
- for partial_query in partial_query_qs.iterator():
+ # If a partial query exists, then just update that object.
+ # Check max 30 queries, in case there is a flood of queries.
+ max_queries = 30
+ for partial_query in partial_query_qs[:max_queries]:
if query.startswith(partial_query.query):
partial_query.query = query
partial_query.total_results = total_results
partial_query.save()
return
- project = Project.objects.filter(slug=project_slug).first()
- if not project:
- log.debug(
- 'Not recording the search query because project does not exist. '
- 'project_slug: %s', project_slug
+ version = (
+ Version.objects
+ .filter(slug=version_slug, project__slug=project_slug)
+ .prefetch_related('project')
+ .first()
)
- return
-
- version = Version.objects.filter(project=project, slug=version_slug).first()
-
if not version:
log.debug(
- 'Not recording the search query because version does not exist. '
- 'project_slug: %s, version_slug: %s', project_slug, version_slug
+ 'Not recording the search query because project does not exist. '
+ 'project=%s version=%s',
+ project_slug, version_slug,
)
return
- # Create a new SearchQuery object.
SearchQuery.objects.create(
- project=project,
+ project=version.project,
version=version,
query=query,
total_results=total_results,
