Skip to content

Commit

Permalink
[RHCLOUD-36102] - Slow loading of data from roles endpoint performanc…
Browse files Browse the repository at this point in the history
…e fixes (#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.
  • Loading branch information
EvanCasey13 authored Jan 30, 2025
1 parent 7c70108 commit edf766e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions rbac/management/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ def get_role_queryset(request) -> QuerySet:
"""Obtain the queryset for roles."""
scope = validate_and_get_key(request.query_params, SCOPE_KEY, VALID_SCOPES, ORG_ID_SCOPE)
public_tenant = Tenant.objects.get(tenant_name="public")
base_query = annotate_roles_with_counts(Role.objects.prefetch_related("access")).filter(
tenant__in=[request.tenant, public_tenant]
)
base_query = annotate_roles_with_counts(
Role.objects.prefetch_related("access", "ext_relation", "access__permission")
).filter(tenant__in=[request.tenant, public_tenant])

if scope == PRINCIPAL_SCOPE:
queryset = get_object_principal_queryset(
Expand Down
1 change: 0 additions & 1 deletion rbac/rbac/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@

ALLOWED_HOSTS = ["*"]


# Application definition

INSTALLED_APPS = [
Expand Down
1 change: 1 addition & 0 deletions tests/internal/integration/test_integration_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ def test_roles_for_org(self, mock_request):
**self.request.META,
follow=True,
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data.get("meta").get("count"), 1)

Expand Down

0 comments on commit edf766e

Please sign in to comment.