Skip to content

Commit

Permalink
code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
lukavdplas committed Nov 22, 2023
1 parent 8eb433d commit 13bc846
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
13 changes: 5 additions & 8 deletions backend/api/save_query.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from datetime import datetime, timedelta

def save_query(user, es_query):
IGNORE_KEYS = ['size', 'scroll', 'from', 'aggs']
'Keys that should be ignored when comparing if two queries are identical'

def should_save_query(user, es_query):
'''
Whether to save a query in the search history for a user
Whether a query should be saved in the search history for a user
'''

if not user.profile.enable_search_history:
Expand All @@ -25,12 +28,6 @@ def recent_queries(user):
def has_aggregations(es_query):
return 'aggs' in es_query

IGNORE_KEYS = [
'size',
'scroll',
'from',
'aggs',
]

def _filter_relevant_keys(es_query):
return {
Expand Down
12 changes: 6 additions & 6 deletions backend/es/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from addcorpus.permissions import CorpusAccessPermission
from tag.filter import handle_tags_in_request
from tag.permissions import CanSearchTags
from api.save_query import save_query
from api.save_query import should_save_query
from addcorpus.models import Corpus
from api.models import Query

Expand Down Expand Up @@ -56,7 +56,7 @@ def post(self, request, *args, **kwargs):
**get_query_parameters(request)
}

history_obj = self._log_query_started(request, corpus_name, query)
history_obj = self._save_query_started(request, corpus_name, query)

try:
results = client.search(
Expand All @@ -69,20 +69,20 @@ def post(self, request, *args, **kwargs):
raise APIException('Search failed')

if history_obj and results:
self._log_query_done(history_obj, results)
self._save_query_done(history_obj, results)

return Response(results)

def _log_query_started(self, request, corpus_name, es_query):
if save_query(request.user, es_query):
def _save_query_started(self, request, corpus_name, es_query):
if should_save_query(request.user, es_query):
corpus = Corpus.objects.get(name=corpus_name)
return Query.objects.create(
user=request.user,
corpus=corpus,
query_json=es_query,
)

def _log_query_done(self, query, results):
def _save_query_done(self, query, results):
query.completed = timezone.now()
query.total_results = total_hits(results)
query.transferred = len(hits(results))
Expand Down

0 comments on commit 13bc846

Please sign in to comment.