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

perf: instance cache and select-related for license serialization #599

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions license_manager/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class MinimalCustomerAgreementSerializer(serializers.ModelSerializer):
Minimal serializer for the `CustomerAgreement` model that does not
include information about related subscription plan records.
"""
net_days_until_expiration = serializers.SerializerMethodField()

class Meta:
model = CustomerAgreement
Expand All @@ -118,6 +119,19 @@ class Meta:
'net_days_until_expiration',
]

def get_net_days_until_expiration(self, obj):
Copy link
Contributor

@pwnage101 pwnage101 Feb 21, 2024

Choose a reason for hiding this comment

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

Does this class-var-based memoization approach work even if many=True is passed to this serializer? I'm not suggesting we actually use many=True anywhere currently, but it does feel like one of those hidden time bombs if it doesn't work.

"""
Cache the net_days_until_expiration of the agreement
to serializer for the lifetime of this serializer instance.
"""
# pylint: disable=attribute-defined-outside-init,access-member-before-definition
if hasattr(self, '_cached_net_days_until_expiration'):
return self._cached_net_days_until_expiration

value = obj.net_days_until_expiration
self._cached_net_days_until_expiration = value
return value


class CustomerAgreementSerializer(serializers.ModelSerializer):
"""
Expand Down
20 changes: 18 additions & 2 deletions license_manager/apps/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ def base_queryset(self):
return License.objects.filter(
subscription_plan=self._get_subscription_plan(),
user_email=self.request.user.email,
).select_related(
'subscription_plan',
'subscription_plan__customer_agreement',
).exclude(
status=constants.REVOKED
).order_by('status', '-subscription_plan__expiration_date')
Expand All @@ -562,14 +565,22 @@ def _get_subscription_plan(self):
"""
Helper that returns the subscription plan specified by `subscription_uuid` in the request.
"""
# pylint: disable=attribute-defined-outside-init,access-member-before-definition
if hasattr(self, '_cached_subscription_plan'):
return self._cached_subscription_plan

subscription_uuid = self.kwargs.get('subscription_uuid')
if not subscription_uuid:
return None

value = None
try:
return SubscriptionPlan.objects.get(uuid=subscription_uuid)
value = SubscriptionPlan.objects.get(uuid=subscription_uuid)
except SubscriptionPlan.DoesNotExist:
return None
pass

self._cached_subscription_plan = value
return value


class LicenseAdminViewSet(BaseLicenseViewSet):
Expand Down Expand Up @@ -640,6 +651,11 @@ def base_queryset(self):
"""
queryset = License.objects.filter(
subscription_plan=self._get_subscription_plan()
).select_related(
'subscription_plan',
'subscription_plan__customer_agreement',
).prefetch_related(
'subscription_plan__renewal',
).order_by(
'status', 'user_email'
)
Expand Down
2 changes: 1 addition & 1 deletion license_manager/apps/subscriptions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class CustomerAgreement(TimeStampedModel):

history = HistoricalRecords()

@property
@cached_property
def net_days_until_expiration(self):
"""
Returns the max number of days until expiration
Expand Down
Loading