-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1306 from UUDigitalHumanitieslab/feature/save-sea…
…rch-history-from-backend Feature/save search history from backend
- Loading branch information
Showing
7 changed files
with
135 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from datetime import datetime, timedelta | ||
|
||
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 a query should be saved in the search history for a user | ||
''' | ||
|
||
if not user.profile.enable_search_history: | ||
return False | ||
|
||
if has_aggregations(es_query): | ||
return False | ||
|
||
if any(same_query(es_query, q.query_json) for q in recent_queries(user)): | ||
return False | ||
|
||
return True | ||
|
||
def recent_queries(user): | ||
one_hour_ago = datetime.today() - timedelta(hours = 1) | ||
return user.queries.filter( | ||
completed__gte=one_hour_ago | ||
) | ||
|
||
def has_aggregations(es_query): | ||
return 'aggs' in es_query | ||
|
||
|
||
def _filter_relevant_keys(es_query): | ||
return { | ||
key: value | ||
for (key, value) in es_query.items() | ||
if key not in IGNORE_KEYS | ||
} | ||
|
||
def same_query(es_query_1, es_query_2): | ||
return _filter_relevant_keys(es_query_1) == _filter_relevant_keys(es_query_2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from django.utils import timezone | ||
import pytest | ||
from copy import deepcopy | ||
|
||
from visualization.query import MATCH_ALL, set_query_text | ||
from addcorpus.models import Corpus | ||
from api.models import Query | ||
from api.save_query import recent_queries, same_query | ||
|
||
|
||
@pytest.fixture() | ||
def saved_query(auth_user, db): | ||
corpus = Corpus.objects.get(name='small-mock-corpus') | ||
return Query.objects.create( | ||
query_json=MATCH_ALL, | ||
user=auth_user, | ||
corpus=corpus, | ||
completed=timezone.now(), | ||
transferred=10, | ||
total_results=10, | ||
) | ||
|
||
|
||
def test_recent_queries(auth_user, saved_query): | ||
assert saved_query in recent_queries(auth_user) | ||
|
||
saved_query.started = timezone.datetime(2000, 1, 1, 0, 0, tzinfo=timezone.get_current_timezone()) | ||
saved_query.completed = timezone.datetime(2000, 1, 1, 0, 0, tzinfo=timezone.get_current_timezone()) | ||
saved_query.save() | ||
|
||
assert saved_query not in recent_queries(auth_user) | ||
|
||
def test_same_query(): | ||
assert same_query(MATCH_ALL, MATCH_ALL) | ||
|
||
q1 = deepcopy(MATCH_ALL) | ||
q1.update({ | ||
'size': 20, | ||
'from': 21, | ||
}) | ||
|
||
assert same_query(q1, MATCH_ALL) | ||
|
||
q2 = set_query_text(MATCH_ALL, 'test') | ||
assert not same_query(q2, MATCH_ALL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters