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: query set optimization #1491

Merged
merged 33 commits into from
Feb 12, 2025
Merged

perf: query set optimization #1491

merged 33 commits into from
Feb 12, 2025

Conversation

eric-intuitem
Copy link
Collaborator

@eric-intuitem eric-intuitem commented Feb 10, 2025

  • optimize get_accessible_object_ids by first calculating the folders that match, and only then retrieving (optimally) the objects from these folders. This should be way more scalable.
  • use scope_folder_id parameter to scope the retrieved objects

Summary by CodeRabbit

  • New Features

    • Improved data filtering by allowing users to refine results based on folder context.
    • Enhanced selection components with additional URL parameters for more precise data loading in risk scenario and requirement assessment screens.
  • Refactor

    • Streamlined permission processing to simplify and optimize access control for various object types.
    • Enhanced flexibility of data retrieval methods for better control over object access.
    • Updated documentation for clarity on new query parameters.

Mohamed-Hacene and others added 30 commits January 29, 2025 17:58
- optimize get_accessible_object_ids by first calculating the folders that match, and only then retrieving (optimally) the objects from these folders. This should be way more scalable.
- use scope_folder_id parameter to scope the retrieved objects
Copy link
Contributor

coderabbitai bot commented Feb 10, 2025

Walkthrough

The pull request enhances both backend and frontend components to support folder-based scoping. In the backend, the BaseModelViewSet.get_queryset method is updated to accept a new scope_folder_id query parameter—with proper folder retrieval via get_object_or_404—and improved documentation. Additionally, the RoleAssignment.get_accessible_object_ids method refines permission handling by restructuring the logic for identifying accessible object IDs. On the frontend, AutocompleteSelect components in risk scenario and requirement assessment pages now pass a scope_folder_id parameter, ensuring that folder context is included in their data requests.

Changes

File(s) Change Summary
backend/core/views.py Added get_object_or_404 import; updated BaseModelViewSet.get_queryset to accept scope_folder_id, retrieve the corresponding Folder, filter the queryset accordingly, and include an explanatory docstring.
backend/iam/models.py Refactored RoleAssignment.get_accessible_object_ids by replacing list-based permission storage with individual variables, initializing distinct sets for view/change/delete permissions, streamlining role assignment processing, and returning a tuple of lists of object IDs.
frontend/src/routes/(app)/.../+page.svelte Updated AutocompleteSelect components in both risk scenarios and requirement assessments pages to include an optionsDetailedUrlParameters property with scope_folder_id derived respectively from scenario perimeter and requirement assessment folder data.

Suggested reviewers

  • ab-smith
  • Mohamed-Hacene

Poem

I'm a rabbit, hopping through the code,
Skipping through changes on my digital road.
Folders and permissions now in clear view,
Each parameter added makes the logic more true.
With joyful hops and ASCII cheer,
I celebrate our code improvements here! 🐰

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
backend/iam/models.py (3)

692-692: Be mindful of performance when collecting large folder trees.
Calling folder.get_sub_folders() and instantly converting it to a set is straightforward, but for heavily nested structures, repeated conversions may become expensive. You might consider caching or a more efficient traversal if scale becomes a concern.


694-698: Optimize role assignment lookups.
Repeatedly calling ra.role.permissions.all() in this list comprehension can result in multiple database queries. Consider using prefetching (e.g., prefetch_related('role__permissions')) to minimize query overhead.


700-711: Avoid repeated subtree expansions in recursive folder lookups.
Within the loop, calling folder.get_sub_folders() for each folder in ra_perimeter may cause repeated traversals if the same folders appear in multiple role assignments. You could unify the list of all required subfolders once per assignment to improve performance.

backend/core/views.py (1)

135-161: Add validation for scope_folder_id and user's access to the folder.

While the implementation of folder-based scoping is good, consider adding:

  1. Validation that the user has access to the specified scope folder
  2. Error handling for invalid scope_folder_id values (non-UUID format)
 def get_queryset(self):
     """the scope_folder_id query_param allows scoping the objects to retrieve"""
     if not self.model:
         return None
     object_ids_view = None
     if self.request.method == "GET":
         if q := re.match(
             r"/api/[\w-]+/([\w-]+/)?([0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}(,[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})+)",
             self.request.path,
         ):
             """"get_queryset is called by Django even for an individual object via get_object
             https://stackoverflow.com/questions/74048193/why-does-a-retrieve-request-end-up-calling-get-queryset"""
             id = UUID(q.group(1))
             if RoleAssignment.is_object_readable(self.request.user, self.model, id):
                 object_ids_view = [id]
     if not object_ids_view:
         scope_folder_id = self.request.query_params.get("scope_folder_id")
+        if scope_folder_id:
+            try:
+                scope_folder_id = UUID(scope_folder_id)
+            except ValueError:
+                raise ValidationError({"scope_folder_id": "Invalid UUID format"})
         scope_folder = (
             get_object_or_404(Folder, id=scope_folder_id)
             if scope_folder_id
             else Folder.get_root_folder()
         )
+        if scope_folder_id and not RoleAssignment.is_access_allowed(
+            user=self.request.user,
+            perm=Permission.objects.get(codename=f"view_{self.model._meta.model_name}"),
+            folder=scope_folder
+        ):
+            raise PermissionDenied("User does not have access to the specified folder")
         object_ids_view = RoleAssignment.get_accessible_object_ids(
             scope_folder, self.request.user, self.model
         )[0]
     queryset = self.model.objects.filter(id__in=object_ids_view)
     return queryset
🧰 Tools
🪛 Ruff (0.8.2)

140-144: Use a single if statement instead of nested if statements

(SIM102)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2c309e0 and 40cabaf.

📒 Files selected for processing (4)
  • backend/core/views.py (3 hunks)
  • backend/iam/models.py (1 hunks)
  • frontend/src/routes/(app)/(internal)/risk-scenarios/[id=uuid]/edit/+page.svelte (4 hunks)
  • frontend/src/routes/(app)/(third-party)/requirement-assessments/[id=uuid]/edit/+page.svelte (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: startup-docker-compose-test
  • GitHub Check: startup-functional-test (3.12)
  • GitHub Check: enterprise-startup-docker-compose-test
  • GitHub Check: functional-tests (3.12, chromium)
  • GitHub Check: build (3.12)
  • GitHub Check: enterprise-functional-tests (3.12, chromium)
  • GitHub Check: ruff (3.12)
  • GitHub Check: enterprise-startup-functional-test (3.12)
  • GitHub Check: test (3.12)
  • GitHub Check: Analyze (python)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (11)
backend/iam/models.py (3)

683-687: Consider handling missing permissions gracefully.
If a corresponding permission record doesn't exist in the database, Permission.objects.get() will raise a Permission.DoesNotExist error. Handling or logging this case might prevent unexpected runtime errors.


688-689: Initialization of result sets looks good.
No issues with creating separate sets for each permission level.


741-745: Confirm the publishing logic for folder-scoped objects.
This publishing logic only executes if the object has both is_published and folder attributes. Ensure that objects requiring limited visibility are properly handled, and consider unit tests for corner cases where objects may or may not have these attributes.

frontend/src/routes/(app)/(internal)/risk-scenarios/[id=uuid]/edit/+page.svelte (5)

175-177: Parameter passing for scope_folder_id looks valid.
Ensure that this parameter is handled and verified on the backend side to prevent potential injection or empty-value issues.


185-187: Repeated logic for scope_folder_id parameter.
The addition closely mirrors the pattern on lines 175–177. Maintaining consistency is good; just verify that the folder ID is present and correct in every usage scenario.


197-199: Consistent approach with scope_folder_id for vulnerabilities.
Same note about verifying the backend acceptance of this parameter.


219-221: Extended parameter usage for existing applied controls.
Again, ensure the backend properly interprets 'scope_folder_id' in this context.


295-297: Ensuring param for extra applied controls as well.
No functional concerns; just confirm that any new logic fully supports scoping in every relevant endpoint.

frontend/src/routes/(app)/(third-party)/requirement-assessments/[id=uuid]/edit/+page.svelte (2)

413-415: Ensure backend alignment with scope_folder_id for applied controls.
Passing the folder ID here is consistent with the pattern in other files. Verify that the backend is ready to handle this parameter for partial scoping if needed.


449-451: Include folder ID scoping for evidence retrieval.
As above, confirm that the backend receives and utilizes this parameter correctly for evidence queries.

backend/core/views.py (1)

50-50: LGTM!

The addition of get_object_or_404 is appropriate for handling folder retrieval in the updated get_queryset method.

Comment on lines 712 to 740
if hasattr(object_type, "folder"):
objects_ids = object_type.objects.filter(folder=f).values_list(
"id", flat=True
)
elif hasattr(object_type, "risk_assessment"):
objects_ids = object_type.objects.filter(
risk_assessment__folder=f
).values_list("id", flat=True)
elif hasattr(object_type, "entity"):
objects_ids = object_type.objects.filter(entity__folder=f).values_list(
"id", flat=True
)
for p in [p for p in permissions if p in ra_permissions]:
if p == permissions[0]:
folders_with_local_view.add(my_folder)
for object in [
x for x in all_objects if folder_for_object[x] in target_folders
]:
# builtins objects cannot be edited or deleted
if not (
hasattr(object, "builtin")
and object.builtin
and p != permissions[0]
):
permissions_per_object_id[object.id].add(p)

if hasattr(object_type, "is_published"):
elif hasattr(object_type, "provider_entity"):
objects_ids = object_type.objects.filter(
provider_entity__folder=f
).values_list("id", flat=True)
elif hasattr(object_type, "parent_folder"):
objects_ids = object_type.objects.filter(parent_folder=f).values_list(
"id", flat=True
)
else:
raise NotImplementedError("type not supported")
if permission_view in result_folders[f]:
result_view.update(objects_ids)
if permission_change in result_folders[f]:
result_change.update(objects_ids)
if permission_delete in result_folders[f]:
result_delete.update(objects_ids)

Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor the conditional blocks for folder-based object filtering.
These cascading hasattr checks can become unwieldy and error-prone, especially as new object types with different folder relationships emerge. Consider centralizing the mapping in a dictionary or leveraging a shared helper function to reduce repetition and improve maintainability.

special case of folder for get_accessible_object_ids
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
backend/iam/models.py (1)

712-731: Refactor object type handling to reduce complexity.

The cascading attribute checks make the code harder to maintain. Consider using a mapping or registry pattern.

-            if hasattr(object_type, "folder"):
-                objects_ids = object_type.objects.filter(folder=f).values_list(
-                    "id", flat=True
-                )
-            elif hasattr(object_type, "risk_assessment"):
-                objects_ids = object_type.objects.filter(
-                    risk_assessment__folder=f
-                ).values_list("id", flat=True)
-            elif hasattr(object_type, "entity"):
-                objects_ids = object_type.objects.filter(entity__folder=f).values_list(
-                    "id", flat=True
-                )
-            elif hasattr(object_type, "provider_entity"):
-                objects_ids = object_type.objects.filter(
-                    provider_entity__folder=f
-                ).values_list("id", flat=True)
-            elif hasattr(object_type, "parent_folder"):
-                objects_ids = [f.id]
-            else:
-                raise NotImplementedError("type not supported")
+            FOLDER_PATHS = {
+                "folder": lambda obj, f: obj.objects.filter(folder=f),
+                "risk_assessment": lambda obj, f: obj.objects.filter(risk_assessment__folder=f),
+                "entity": lambda obj, f: obj.objects.filter(entity__folder=f),
+                "provider_entity": lambda obj, f: obj.objects.filter(provider_entity__folder=f),
+                "parent_folder": lambda obj, f: [f.id]
+            }
+            
+            for attr, query_func in FOLDER_PATHS.items():
+                if hasattr(object_type, attr):
+                    objects_ids = query_func(object_type, f).values_list("id", flat=True) if attr != "parent_folder" else query_func(object_type, f)
+                    break
+            else:
+                raise NotImplementedError(f"Object type {object_type.__name__} does not support any known folder relationship")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 40cabaf and 5ba1453.

📒 Files selected for processing (1)
  • backend/iam/models.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: startup-functional-test (3.12)
  • GitHub Check: startup-docker-compose-test
  • GitHub Check: enterprise-startup-functional-test (3.12)
  • GitHub Check: functional-tests (3.12, chromium)
  • GitHub Check: build (3.12)
  • GitHub Check: test (3.12)
  • GitHub Check: migrations-check (3.12)
  • GitHub Check: enterprise-startup-docker-compose-test
  • GitHub Check: enterprise-functional-tests (3.12, chromium)
  • GitHub Check: Analyze (python)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (3)
backend/iam/models.py (3)

683-689: LGTM! Efficient permission handling using sets.

The change from list-based to set-based permission handling improves performance for membership testing operations.


692-711: LGTM! Efficient folder permission calculation.

The new implementation optimizes performance by:

  • Pre-calculating the folder perimeter
  • Using defaultdict to group folders by permissions
  • Reducing database queries through set operations

739-755: LGTM! Clear handling of published objects.

The implementation correctly manages published objects visibility while maintaining good readability.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
backend/iam/models.py (2)

704-706: Consider optimizing recursive folder updates.

The current implementation might be expensive for deep folder hierarchies. Consider using a generator-based approach to reduce memory usage.

-                ra_perimeter.update(
-                    *[folder.get_sub_folders() for folder in ra_perimeter]
-                )
+                for folder in ra_perimeter.copy():
+                    ra_perimeter.update(folder.get_sub_folders())

730-731: Enhance error handling for unsupported types.

The current error message could be more informative. Consider including the actual type that's not supported.

-                raise NotImplementedError("type not supported")
+                raise NotImplementedError(
+                    f"Object type '{object_type.__name__}' is not supported. "
+                    "The type must have one of these attributes: folder, risk_assessment, "
+                    "entity, provider_entity, or parent_folder"
+                )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5ba1453 and 0343faf.

📒 Files selected for processing (1)
  • backend/iam/models.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: functional-tests (3.12, chromium)
  • GitHub Check: test (3.12)
  • GitHub Check: enterprise-functional-tests (3.12, chromium)
🔇 Additional comments (3)
backend/iam/models.py (3)

683-689: LGTM! Improved permission handling initialization.

The refactored code improves clarity and efficiency by:

  • Using individual permission variables for better readability
  • Using sets for efficient permission lookups and result storage

692-711: LGTM! Efficient folder permission calculation.

The refactored code significantly improves performance by:

  • Pre-calculating the perimeter once
  • Using defaultdict to efficiently track permissions per folder
  • Processing role assignments in a more streamlined manner

739-755: LGTM! Improved published objects handling.

The refactored code efficiently handles published objects by:

  • Checking for required attributes before processing
  • Using list comprehension for better performance
  • Properly scoping the published objects retrieval

Copy link
Collaborator

@Mohamed-Hacene Mohamed-Hacene left a comment

Choose a reason for hiding this comment

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

0k

@Mohamed-Hacene Mohamed-Hacene merged commit 87ffbea into main Feb 12, 2025
20 checks passed
@Mohamed-Hacene Mohamed-Hacene deleted the perf/query_set branch February 12, 2025 10:54
@github-actions github-actions bot locked and limited conversation to collaborators Feb 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants