Skip to content

Commit 6b7d865

Browse files
committed
[RHCLOUD-36102] - Slow loading of data from roles endpoint performance fixes (RedHatInsights#1354)
Using prefetch_related for "access", "ext_relation" and "access__permission" reduced the number of queries from 33 to 6 for a request to the roles endpoint for the get_role_queryset method.
1 parent 666eb6c commit 6b7d865

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

rbac/management/querysets.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ def get_role_queryset(request) -> QuerySet:
155155
"""Obtain the queryset for roles."""
156156
scope = validate_and_get_key(request.query_params, SCOPE_KEY, VALID_SCOPES, ORG_ID_SCOPE)
157157
public_tenant = Tenant.objects.get(tenant_name="public")
158-
base_query = annotate_roles_with_counts(Role.objects.prefetch_related("access")).filter(
159-
tenant__in=[request.tenant, public_tenant]
160-
)
158+
base_query = annotate_roles_with_counts(
159+
Role.objects.prefetch_related("access", "ext_relation", "access__permission")
160+
).filter(tenant__in=[request.tenant, public_tenant])
161161

162162
if scope == PRINCIPAL_SCOPE:
163163
queryset = get_object_principal_queryset(

rbac/rbac/settings.py

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484

8585
ALLOWED_HOSTS = ["*"]
8686

87-
8887
# Application definition
8988

9089
INSTALLED_APPS = [

tests/internal/integration/test_integration_views.py

+1
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ def test_roles_for_org(self, mock_request):
442442
**self.request.META,
443443
follow=True,
444444
)
445+
445446
self.assertEqual(response.status_code, status.HTTP_200_OK)
446447
self.assertEqual(response.data.get("meta").get("count"), 1)
447448

0 commit comments

Comments
 (0)