-
Notifications
You must be signed in to change notification settings - Fork 171
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
Reduce repeated calls to django cache while calculating utm_source for listing endpoints #4462
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from django.core.files.base import ContentFile | ||
from django.db.models.fields.related import ManyToManyField | ||
from django.utils.translation import gettext as _ | ||
from edx_django_utils.cache import RequestCache | ||
from opaque_keys.edx.keys import CourseKey | ||
from requests.exceptions import HTTPError | ||
from sortedm2m.fields import SortedManyToManyField | ||
|
@@ -352,3 +353,20 @@ def push_to_studio(self, course_run, create=False, old_course_run_key=None, user | |
self.create_course_run_in_studio(course_run, user=user) | ||
else: | ||
self.update_course_run_details_in_studio(course_run) | ||
|
||
|
||
def use_request_cache(cache_name, key_func): | ||
def inner(fn): | ||
@functools.wraps(fn) | ||
def wrapped(*args, **kwargs): | ||
cache = RequestCache(cache_name) | ||
cache_key = key_func(*args, **kwargs) | ||
cached_response = cache.get_cached_response(cache_key) | ||
if cached_response.is_found: | ||
return cached_response.value | ||
|
||
ret_val = fn(*args, **kwargs) | ||
cache.set(cache_key, ret_val) | ||
return ret_val | ||
Comment on lines
+359
to
+370
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming and readability can be improved like: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bunch of lines here n there to improve readability. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [curious] Was the first of these comments generated through some AI tool? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wrote this myself although I got the naming conventions from GPT :D |
||
return wrapped | ||
return inner |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
from edx_toggles.toggles.testutils import override_waffle_switch | ||
from rest_framework.reverse import reverse | ||
from testfixtures import LogCapture | ||
from waffle.testutils import override_switch | ||
|
||
from course_discovery.apps.api.v1.exceptions import EditableAndQUnsupported | ||
from course_discovery.apps.api.v1.tests.test_views.mixins import APITestCase, OAuth2Mixin, SerializationMixin | ||
|
@@ -352,6 +353,26 @@ def test_list(self): | |
self.serialize_course(Course.objects.all(), many=True) | ||
) | ||
|
||
def test_no_repeated_cache_calls_for_utm_calculation(self): | ||
""" | ||
Test that utm source calculation is done only once per request, and not per | ||
serialized object on a listing endpoint. Since each utm source calculation | ||
requires two cache calls, this reduces the number of calls to the django cache. | ||
""" | ||
CourseFactory( | ||
partner=self.partner, title='Fake Test 1', key='edX+Fake102', type=self.audit_type | ||
) | ||
self.partner.lms_url = "http://localhost:8000" | ||
self.partner.save() | ||
with mock.patch('course_discovery.apps.core.api_client.lms.LMSAPIClient.get_api_access_request', return_value={ | ||
"company_name": "Example Inc", | ||
"company_address": "Example Address", | ||
}) as mocked_api_access_request: | ||
with override_switch('use_company_name_as_utm_source_value', True): | ||
url = reverse('api:v1:course-list') | ||
self.client.get(url) | ||
mocked_api_access_request.assert_called_once() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: should we not be checking utm call's count here too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
@pytest.mark.skip(reason="https://github.com/openedx/course-discovery/issues/4431") | ||
@responses.activate | ||
def test_list_query(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A docstring explaining it's usage would be nice.