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

Performance and test improvement for LearnerMetricsViewSet #260

Merged
merged 12 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions figures/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ class EnrollmentMetricsSerializerV2(serializers.ModelSerializer):
course_id = serializers.CharField()
date_enrolled = serializers.DateTimeField(source='created',
format="%Y-%m-%d")
is_enrolled = serializers.BooleanField()
is_enrolled = serializers.BooleanField(source='is_active')
progress_percent = serializers.SerializerMethodField()
progress_details = serializers.SerializerMethodField()

Expand All @@ -821,14 +821,6 @@ def to_representation(self, instance):
user=instance.user, course_id=str(instance.course_id))
return super(EnrollmentMetricsSerializerV2, self).to_representation(instance)

def get_is_enrolled(self, obj):
"""
CourseEnrollment has to do some work to get this value
TODO: inspect CourseEnrollment._get_enrollment_state to see how we
can speed this up, avoiding construction of `CourseEnrollmentState`
"""
return CourseEnrollment.is_enrolled(obj.user, obj.course_id)

def get_progress_percent(self, obj): # pylint: disable=unused-argument
value = self._lcgm.progress_percent if self._lcgm else 0
return float(Decimal(value).quantize(Decimal('.00')))
Expand Down Expand Up @@ -866,7 +858,7 @@ class LearnerMetricsSerializer(serializers.ModelSerializer):

class Meta:
model = get_user_model()
# list_serializer_class = LearnerMetricsListSerializer
list_serializer_class = LearnerMetricsListSerializer
fields = ('id', 'username', 'email', 'fullname', 'is_active',
'date_joined', 'enrollments')
read_only_fields = fields
Expand All @@ -876,14 +868,14 @@ def get_enrollments(self, user):
Rely on the caller (the view) to filter users and prefetch related
"""

user_enrollments = user.courseenrollment_set.all()
# user_enrollments = user.courseenrollment_set.all()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# user_enrollments = user.courseenrollment_set.all()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll remove the dead code. Thanks


# Still in testing, and remarked out to get the first pass PR through:
# This is where the ListSerializer helps, by doing the database hit in
# one set of queries at the top, then using the results for each. But
# it still needs work

# user_enrollments = user.courseenrollment_set.filter(
# course_id__in=self.parent.course_keys)
user_enrollments = user.courseenrollment_set.filter(
course_id__in=self.parent.course_keys)

return EnrollmentMetricsSerializerV2(user_enrollments, many=True).data
20 changes: 20 additions & 0 deletions tests/views/test_learner_metrics_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ def make_request(self, monkeypatch, request_path, site, caller, action):
view = self.view_class.as_view({'get': action})
return view(request)

def matching_enrollment_set_to_course_ids(self, enrollments, course_ids):
"""
enrollment course ids need to be a subset of course_ids
It is ok if there are none or fewer enrollments than course_ids because
a learner might not be enrolled in all the courses on which we are
filtering
"""
enroll_course_ids = set([rec['course_id'] for rec in enrollments])
return enroll_course_ids.issubset(set([str(rec) for rec in course_ids]))

def test_list_method_all(self, monkeypatch, lm_test_data):
"""Partial test coverage to check we get all site users

Expand Down Expand Up @@ -150,6 +160,10 @@ def test_course_param_single(self, monkeypatch, lm_test_data):
expected_user_ids = [obj.user.id for obj in course_enrollments]
assert set(result_ids) == set(expected_user_ids)

for rec in results:
assert self.matching_enrollment_set_to_course_ids(
rec['enrollments'], [our_courses[0].id])

def test_course_param_multiple(self, monkeypatch, lm_test_data):
"""Test that the 'course' query parameter works

Expand Down Expand Up @@ -185,8 +199,14 @@ def test_course_param_multiple(self, monkeypatch, lm_test_data):
courses=filtered_courses)
expected_user_ids = [obj.user.id for obj in expected_enrollments]
assert set(result_ids) == set(expected_user_ids)
for rec in results:
assert self.matching_enrollment_set_to_course_ids(
rec['enrollments'], [rec.id for rec in filtered_courses])

def invalid_course_ids_raise_404(self, monkeypatch, lm_test_data, query_params):
"""
Helper method to test expected 404 calls
"""
us = lm_test_data['us']
them = lm_test_data['them']

Expand Down