Skip to content

Commit

Permalink
Query optimization for buddy and pickup systems (#290)
Browse files Browse the repository at this point in the history
Fixes for 1+n queries detected by sentry on /buddy-system/my-buddies and
/pickup-system/matching-requests.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

- **New Features**
- Enhanced data retrieval for user buddy matches in the MyBuddies view,
improving efficiency.
- Optimized matching requests handling in the pickup system for better
performance.

- **Bug Fixes**
- Improved the lookup for primary email addresses in user profiles,
ensuring accuracy and efficiency.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
thejoeejoee committed Sep 15, 2024
2 parents 0c021d0 + 4e7437e commit 3ecac74
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion fiesta/apps/accounts/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Meta(AbstractUser.Meta):

@property
def primary_email(self):
return self.emailaddress_set.filter(primary=True).first() or self.email
return next((email for email in self.emailaddress_set.all() if email.primary), self.email)


__all__ = ["User"]
12 changes: 8 additions & 4 deletions fiesta/apps/buddy_system/views/matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ class MyBuddies(EnsureLocalUserViewMixin, ListView):

def get_queryset(self):
return (
self.request.user.buddy_system_request_matches.prefetch_related("request__issuer__profile")
.select_related("request", "matcher")
.filter(
request__state=BaseRequestProtocol.State.MATCHED,
self.request.user.buddy_system_request_matches.prefetch_related(
"request__issuer__emailaddress_set",
)
.select_related(
"request__issuer__profile__user",
"request__issuer__profile__university",
"request__issuer__profile__faculty",
)
.filter(request__state=BaseRequestProtocol.State.MATCHED)
)
7 changes: 6 additions & 1 deletion fiesta/apps/buddy_system/views/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ def get_permission_denied_message(self):

def get_queryset(self):
return self.configuration.matching_policy_instance.limit_requests(
qs=BuddyRequest.objects.get_queryset(),
qs=BuddyRequest.objects.get_queryset().select_related(
# select all potentially necessary fields in the template afterward
"issuer__profile__user",
"issuer__profile__university",
"issuer__profile__faculty",
),
membership=self.request.membership,
)

Expand Down
8 changes: 6 additions & 2 deletions fiesta/apps/pickup_system/views/matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ class MyPickups(EnsureLocalUserViewMixin, ListView):

def get_queryset(self):
return (
self.request.user.pickup_system_request_matches.prefetch_related("request__issuer__profile")
.select_related("request", "matcher")
self.request.user.pickup_system_request_matches.prefetch_related("request__issuer__emailaddress_set")
.select_related(
"request__issuer__profile__user",
"request__issuer__profile__university",
"request__issuer__profile__faculty",
)
.filter(
request__state=BaseRequestProtocol.State.MATCHED,
)
Expand Down
6 changes: 5 additions & 1 deletion fiesta/apps/pickup_system/views/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class MatchingRequestsView(
model = PickupRequest

def get_queryset(self):
return self.request.in_space_of_section.pickup_system_requests.filter(
return self.request.in_space_of_section.pickup_system_requests.select_related(
"issuer__profile__user",
"issuer__profile__university",
"issuer__profile__faculty",
).filter(
state=PickupRequest.State.CREATED,
)

Expand Down

0 comments on commit 3ecac74

Please sign in to comment.