Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 6e81e13

Browse files
authored
Merge pull request #162 from open-craft/jill/es_agg_search_size
Make ElasticSearch aggregate search results size configurable
2 parents 54a8016 + 87d555b commit 6e81e13

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

analytics_data_api/v0/models.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,14 @@ def get_course_metadata(cls, course_id):
407407
}
408408
}
409409
"""
410+
# Use the configured default page size to set the number of aggregate search results.
411+
page_size = getattr(settings, 'AGGREGATE_PAGE_SIZE', 10)
412+
410413
search = cls.search()
411414
search.query = Q('bool', must=[Q('term', course_id=course_id)])
412-
search.aggs.bucket('enrollment_modes', 'terms', field='enrollment_mode')
413-
search.aggs.bucket('segments', 'terms', field='segments')
414-
search.aggs.bucket('cohorts', 'terms', field='cohort')
415+
search.aggs.bucket('enrollment_modes', 'terms', field='enrollment_mode', size=page_size)
416+
search.aggs.bucket('segments', 'terms', field='segments', size=page_size)
417+
search.aggs.bucket('cohorts', 'terms', field='cohort', size=page_size)
415418
response = search.execute()
416419
# Build up the map of aggregation name to count
417420
aggregations = {

analytics_data_api/v0/tests/views/test_learners.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from django.conf import settings
1616
from django.core import management
17+
from django.test import override_settings
1718

1819
from analyticsdataserver.tests import TestCaseWithAuthentication
1920
from analytics_data_api.constants import engagement_events
@@ -769,6 +770,29 @@ def test_cohorts(self, course_id, cohorts):
769770
)
770771
self.assert_response_matches(self._get(course_id), 200, expected)
771772

773+
@ddt.data(
774+
(CourseSamples.course_ids[0], [], {}),
775+
(CourseSamples.course_ids[1], ['Yellow'], {'Yellow': 1}),
776+
(CourseSamples.course_ids[1], ['Yellow', 'Green'], {'Yellow': 1, 'Green': 1}),
777+
(CourseSamples.course_ids[0], ['Red', 'Red', 'yellow team', 'green'], {'Red': 2, 'green': 1}),
778+
)
779+
@ddt.unpack
780+
@override_settings(AGGREGATE_PAGE_SIZE=2)
781+
def test_cohorts_page_size(self, course_id, cohorts, expected_cohorts):
782+
""" Ensure that the AGGREGATE_PAGE_SIZE sets the max number of cohorts returned."""
783+
784+
self.create_learners([
785+
{'username': 'user_{}'.format(i), 'course_id': course_id, 'cohort': cohort}
786+
for i, cohort in enumerate(cohorts)
787+
])
788+
expected = self.get_expected_json(
789+
course_id=course_id,
790+
segments={'disengaging': 0, 'struggling': 0, 'highly_engaged': 0, 'inactive': 0, 'unenrolled': 0},
791+
enrollment_modes={'honor': len(cohorts)} if cohorts else {},
792+
cohorts=expected_cohorts,
793+
)
794+
self.assert_response_matches(self._get(course_id), 200, expected)
795+
772796
@property
773797
def empty_engagement_ranges(self):
774798
""" Returns the engagement ranges where all fields are set to None. """

analyticsdataserver/settings/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@
310310
# Warning: using 0 or None for these can alter the structure of the REST response.
311311
DEFAULT_PAGE_SIZE = 25
312312
MAX_PAGE_SIZE = 100
313+
AGGREGATE_PAGE_SIZE = 10
313314

314315
########## END ANALYTICS DATA API CONFIGURATION
315316

0 commit comments

Comments
 (0)