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

feat: add timing for results filtering #142

Merged
merged 11 commits into from
Jan 4, 2024
27 changes: 27 additions & 0 deletions search/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
""" search business logic implementations """

from datetime import datetime
import time

from django.conf import settings

from eventtracking import tracker as track
from .filter_generator import SearchFilterGenerator
from .search_engine_base import SearchEngine
from .result_processor import SearchResultProcessor
Expand Down Expand Up @@ -52,10 +54,14 @@ def perform_search(
"""
# field_, filter_ and exclude_dictionary(s) can be overridden by calling application
# field_dictionary includes course if course_id provided
filter_generation_time = {
"start": time.time()
}
(field_dictionary, filter_dictionary, exclude_dictionary) = SearchFilterGenerator.generate_field_filters(
user=user,
course_id=course_id
)
filter_generation_time['end'] = time.time()

searcher = SearchEngine.get_search_engine(
getattr(settings, "COURSEWARE_CONTENT_INDEX_NAME", "courseware_content")
Expand All @@ -64,6 +70,10 @@ def perform_search(
raise NoSearchEngineError("No search engine specified in settings.SEARCH_ENGINE")
log_search_params = getattr(settings, "SEARCH_COURSEWARE_CONTENT_LOG_PARAMS", False)

search_time = {
"start": time.time()
}

results = searcher.search(
query_string=search_term,
field_dictionary=field_dictionary,
Expand All @@ -74,13 +84,30 @@ def perform_search(
log_search_params=log_search_params,
)

processing_time = {
"start": time.time()
}

# post-process the result
for result in results["results"]:
result["data"] = SearchResultProcessor.process_result(result["data"], search_term, user)

results["access_denied_count"] = len([r for r in results["results"] if r["data"] is None])
results["results"] = [r for r in results["results"] if r["data"] is not None]

processing_time['end'] = time.time()
search_time['end'] = time.time()

processing_time_in_seconds = processing_time['end'] - processing_time['start']

Choose a reason for hiding this comment

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

Can we please wrap all these into a separate function? That way, we can add a set of unit test to ensure the calculation and event sending

search_time_in_seconds = search_time['end'] - search_time['start']
filter_generation_time_in_seconds = filter_generation_time['end'] - filter_generation_time['start']

track.emit("edx.course.search.executed", {
davidnuon marked this conversation as resolved.
Show resolved Hide resolved
"search_term": search_term,
"processing_time_in_seconds": processing_time_in_seconds,
davidnuon marked this conversation as resolved.
Show resolved Hide resolved
"search_time_in_seconds": search_time_in_seconds,
"filter_generation_time_in_seconds": filter_generation_time_in_seconds,
})
return results


Expand Down
Loading