Skip to content

Commit

Permalink
dev mode (#1333)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeglova authored Jul 30, 2024
1 parent fafe842 commit c3c345d
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 16 deletions.
96 changes: 96 additions & 0 deletions frontends/api/src/generated/v1/api.ts

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions learning_resources_search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
DEPARTMENT_QUERY_FIELDS,
LEARNING_RESOURCE,
LEARNING_RESOURCE_QUERY_FIELDS,
LEARNING_RESOURCE_SEARCH_FILTERS,
LEARNING_RESOURCE_TYPES,
RESOURCEFILE_QUERY_FIELDS,
RUN_INSTRUCTORS_QUERY_FIELDS,
RUNS_QUERY_FIELDS,
SEARCH_FILTERS,
SOURCE_EXCLUDED_FIELDS,
TOPICS_QUERY_FIELDS,
)
Expand Down Expand Up @@ -340,7 +340,7 @@ def generate_filter_clauses(search_params):
"""
all_filter_clauses = {}

for filter_name, filter_config in LEARNING_RESOURCE_SEARCH_FILTERS.items():
for filter_name, filter_config in SEARCH_FILTERS.items():
if search_params.get(filter_name):
clauses_for_filter = [
generate_filter_clause(
Expand Down Expand Up @@ -447,7 +447,7 @@ def generate_aggregation_clauses(search_params, filter_clauses):
for aggregation in search_params.get("aggregations"):
# Each aggregation clause contains a filter which includes all the filters
# except it's own
path = LEARNING_RESOURCE_SEARCH_FILTERS[aggregation].path
path = SEARCH_FILTERS[aggregation].path
unfiltered_aggs = generate_aggregation_clause(aggregation, path)
other_filters = [
filter_clauses[key] for key in filter_clauses if key != aggregation
Expand Down Expand Up @@ -486,7 +486,7 @@ def adjust_original_query_for_percolate(query):
Remove keys that are irrelevent when storing original queries
for percolate uniqueness such as "limit" and "offset"
"""
for key in ["limit", "offset", "sortby", "yearly_decay_percent"]:
for key in ["limit", "offset", "sortby", "yearly_decay_percent", "dev_mode"]:
query.pop(key, None)
return order_params(query)

Expand Down Expand Up @@ -622,6 +622,9 @@ def construct_search(search_params):
)
search = search.extra(aggs=aggregation_clauses)

if search_params.get("dev_mode"):
search = search.extra(explain=True)

return search


Expand Down
20 changes: 20 additions & 0 deletions learning_resources_search/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2214,3 +2214,23 @@ def test_default_sort(sortby, q, result):
}

assert construct_search(search_params).to_dict().get("sort") == result


@pytest.mark.parametrize(
"dev_mode",
[True, False],
)
def test_dev_mode(dev_mode):
search_params = {
"aggregations": [],
"q": "text",
"limit": 1,
"offset": 1,
"dev_mode": dev_mode,
"endpoint": LEARNING_RESOURCE,
}

if dev_mode:
assert construct_search(search_params).to_dict().get("explain")
else:
assert construct_search(search_params).to_dict().get("explain") is None
4 changes: 2 additions & 2 deletions learning_resources_search/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class FilterConfig:
case_sensitive: bool = False


LEARNING_RESOURCE_SEARCH_FILTERS = {
SEARCH_FILTERS = {
"resource_type": FilterConfig("resource_type"),
"certification": FilterConfig("certification"),
"certification_type": FilterConfig("certification_type.code"),
Expand All @@ -67,7 +67,7 @@ class FilterConfig:
"course_feature": FilterConfig("course_feature"),
"content_feature_type": FilterConfig("content_feature_type"),
"run_id": FilterConfig("run_id", case_sensitive=True),
"resource_id": FilterConfig("resource_id"),
"resource_id": FilterConfig("resource_id", case_sensitive=True),
"topic": FilterConfig("topics.name"),
"level": FilterConfig("runs.level.code"),
"department": FilterConfig("departments.department_id"),
Expand Down
6 changes: 6 additions & 0 deletions learning_resources_search/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ class SearchRequestSerializer(serializers.Serializer):
child=serializers.CharField(),
help_text="The topic name. To see a list of options go to api/v1/topics/",
)
dev_mode = serializers.BooleanField(
required=False,
allow_null=True,
default=False,
help_text="If true return raw open search results with score explanations",
)

def validate(self, attrs):
unknown = set(self.initial_data) - set(self.fields)
Expand Down
2 changes: 2 additions & 0 deletions learning_resources_search/serializers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ def test_learning_resources_search_request_serializer():
"course_feature": ["Lecture Videos"],
"aggregations": ["resource_type", "platform", "level", "resource_category"],
"yearly_decay_percent": 0.25,
"dev_mode": False,
}

serialized = LearningResourcesSearchRequestSerializer(data=data)
Expand Down Expand Up @@ -867,6 +868,7 @@ def test_content_file_search_request_serializer():
"resource_id": [1, 2, 3],
"offered_by": ["xpro", "ocw"],
"platform": ["xpro", "edx", "ocw"],
"dev_mode": False,
}

serialized = ContentFileSearchRequestSerializer(data=data)
Expand Down
27 changes: 17 additions & 10 deletions learning_resources_search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ def get(self, request):
response = execute_learn_search(
request_data.data | {"endpoint": LEARNING_RESOURCE}
)
return Response(
LearningResourcesSearchResponseSerializer(
response, context={"request": request}
).data
)

if request_data.data.get("dev_mode"):
return Response(response)
else:
return Response(
LearningResourcesSearchResponseSerializer(
response, context={"request": request}
).data
)
else:
errors = {}
for key, errors_obj in request_data.errors.items():
Expand Down Expand Up @@ -237,11 +241,14 @@ def get(self, request):
response = execute_learn_search(
request_data.data | {"endpoint": CONTENT_FILE_TYPE}
)
return Response(
ContentFileSearchResponseSerializer(
response, context={"request": request}
).data
)
if request_data.data.get("dev_mode"):
return Response(response)
else:
return Response(
LearningResourcesSearchResponseSerializer(
response, context={"request": request}
).data
)
else:
errors = {}
for key, errors_obj in request_data.errors.items():
Expand Down
40 changes: 40 additions & 0 deletions openapi/specs/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ paths:
minLength: 1
description: The feature type of the content file. Possible options are at
api/v1/course_features/
- in: query
name: dev_mode
schema:
type: boolean
nullable: true
default: false
description: If true return raw open search results with score explanations
- in: query
name: id
schema:
Expand Down Expand Up @@ -2298,6 +2305,13 @@ paths:
\ Society\n* `MAS` - Media Arts and Sciences\n* `PE` - Athletics, Physical\
\ Education and Recreation\n* `RES` - Supplemental Resources\n* `STS` -\
\ Science, Technology, and Society\n* `WGS` - Women's and Gender Studies"
- in: query
name: dev_mode
schema:
type: boolean
nullable: true
default: false
description: If true return raw open search results with score explanations
- in: query
name: free
schema:
Expand Down Expand Up @@ -2728,6 +2742,13 @@ paths:
\ Society\n* `MAS` - Media Arts and Sciences\n* `PE` - Athletics, Physical\
\ Education and Recreation\n* `RES` - Supplemental Resources\n* `STS` -\
\ Science, Technology, and Society\n* `WGS` - Women's and Gender Studies"
- in: query
name: dev_mode
schema:
type: boolean
nullable: true
default: false
description: If true return raw open search results with score explanations
- in: query
name: free
schema:
Expand Down Expand Up @@ -3183,6 +3204,13 @@ paths:
\ Society\n* `MAS` - Media Arts and Sciences\n* `PE` - Athletics, Physical\
\ Education and Recreation\n* `RES` - Supplemental Resources\n* `STS` -\
\ Science, Technology, and Society\n* `WGS` - Women's and Gender Studies"
- in: query
name: dev_mode
schema:
type: boolean
nullable: true
default: false
description: If true return raw open search results with score explanations
- in: query
name: free
schema:
Expand Down Expand Up @@ -3629,6 +3657,13 @@ paths:
\ Society\n* `MAS` - Media Arts and Sciences\n* `PE` - Athletics, Physical\
\ Education and Recreation\n* `RES` - Supplemental Resources\n* `STS` -\
\ Science, Technology, and Society\n* `WGS` - Women's and Gender Studies"
- in: query
name: dev_mode
schema:
type: boolean
nullable: true
default: false
description: If true return raw open search results with score explanations
- in: query
name: free
schema:
Expand Down Expand Up @@ -9500,6 +9535,11 @@ components:
type: string
minLength: 1
description: The topic name. To see a list of options go to api/v1/topics/
dev_mode:
type: boolean
nullable: true
default: false
description: If true return raw open search results with score explanations
id:
type: array
items:
Expand Down

0 comments on commit c3c345d

Please sign in to comment.