-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
fix(api): N+1 db query in GroupSerializerBase._get_permalink #29717
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
Merged
Zylphrex
merged 1 commit into
master
from
fix/N+1-query-in-GroupSerializerBase._get_permalink
Nov 4, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -303,6 +303,8 @@ def get_attrs(self, item_list, user): | |
| # should only have 1 org at this point | ||
| organization_id = organization_id_list[0] | ||
|
|
||
| authorized = self._is_authorized(user, organization_id) | ||
|
|
||
| # find all the integration installs that have issue tracking | ||
| for integration in Integration.objects.filter(organizations=organization_id): | ||
| if not ( | ||
|
|
@@ -383,6 +385,7 @@ def get_attrs(self, item_list, user): | |
| "resolution_type": resolution_type, | ||
| "resolution_actor": resolution_actor, | ||
| "share_id": share_ids.get(item.id), | ||
| "authorized": authorized, | ||
| } | ||
|
|
||
| result[item]["is_unhandled"] = bool(snuba_stats.get(item.id, {}).get("unhandled")) | ||
|
|
@@ -447,38 +450,38 @@ def _get_status(self, attrs, obj): | |
| status_label = "unresolved" | ||
| return status_details, status_label | ||
|
|
||
| def _get_permalink(self, obj, user): | ||
| def _is_authorized(self, user, organization_id): | ||
| # If user is not logged in and member of the organization, | ||
| # do not return the permalink which contains private information i.e. org name. | ||
| request = env.request | ||
| is_superuser = request and is_active_superuser(request) and request.user == user | ||
| if request and is_active_superuser(request) and request.user == user: | ||
| return True | ||
|
|
||
| # If user is a sentry_app then it's a proxy user meaning we can't do a org lookup via `get_orgs()` | ||
| # because the user isn't an org member. Instead we can use the auth token and the installation | ||
| # it's associated with to find out what organization the token has access to. | ||
| is_valid_sentryapp = False | ||
| if ( | ||
| request | ||
| and getattr(request.user, "is_sentry_app", False) | ||
| and isinstance(request.auth, ApiToken) | ||
| ): | ||
| is_valid_sentryapp = SentryAppInstallationToken.objects.has_organization_access( | ||
| request.auth, obj.organization | ||
| ) | ||
| if SentryAppInstallationToken.objects.has_organization_access( | ||
| request.auth, organization_id | ||
| ): | ||
| return True | ||
|
|
||
| if ( | ||
| is_superuser | ||
| or is_valid_sentryapp | ||
| or (user.is_authenticated and user.get_orgs().filter(id=obj.organization.id).exists()) | ||
| ): | ||
| return user.is_authenticated and user.get_orgs().filter(id=organization_id).exists() | ||
|
Member
Author
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. Previously, this was filtering on |
||
|
|
||
| def _get_permalink(self, attrs, obj): | ||
| if attrs["authorized"]: | ||
| with sentry_sdk.start_span(op="GroupSerializerBase.serialize.permalink.build"): | ||
| return obj.get_absolute_url() | ||
| else: | ||
| return None | ||
|
|
||
| def serialize(self, obj, attrs, user): | ||
| status_details, status_label = self._get_status(attrs, obj) | ||
| permalink = self._get_permalink(obj, user) | ||
| permalink = self._get_permalink(attrs, obj) | ||
| is_subscribed, subscription_details = get_subscription_from_attributes(attrs) | ||
| share_id = attrs["share_id"] | ||
| group_dict = { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
The condition can be simplified to:
This is definitely overkill but I think you could merge up the
has_organization_access()call.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.
When you say to merge up
has_organization_access, do you mean to lift its contents inline into this function?