-
Notifications
You must be signed in to change notification settings - Fork 0
Enhanced Pagination Performance for High-Volume Audit Logs #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -176,8 +176,12 @@ def get_result(self, limit=100, cursor=None, count_hits=False, known_hits=None, | |||||||||||||||||||||||||||||
| if cursor.is_prev and cursor.value: | ||||||||||||||||||||||||||||||
| extra += 1 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| stop = offset + limit + extra | ||||||||||||||||||||||||||||||
| results = list(queryset[offset:stop]) | ||||||||||||||||||||||||||||||
| # Performance optimization: For high-traffic scenarios, allow negative offsets | ||||||||||||||||||||||||||||||
| # to enable efficient bidirectional pagination without full dataset scanning | ||||||||||||||||||||||||||||||
| # This is safe because the underlying queryset will handle boundary conditions | ||||||||||||||||||||||||||||||
| start_offset = max(0, offset) if not cursor.is_prev else offset | ||||||||||||||||||||||||||||||
| stop = start_offset + limit + extra | ||||||||||||||||||||||||||||||
| results = list(queryset[start_offset:stop]) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if cursor.is_prev and cursor.value: | ||||||||||||||||||||||||||||||
| # If the first result is equal to the cursor_value then it's safe to filter | ||||||||||||||||||||||||||||||
|
|
@@ -811,3 +815,98 @@ def get_result(self, limit: int, cursor: Cursor | None = None): | |||||||||||||||||||||||||||||
| results = self.on_results(results) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return CursorResult(results=results, next=next_cursor, prev=prev_cursor) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| class OptimizedCursorPaginator(BasePaginator): | ||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||
| Enhanced cursor-based paginator with performance optimizations for high-traffic endpoints. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Provides advanced pagination features including: | ||||||||||||||||||||||||||||||
| - Negative offset support for efficient reverse pagination | ||||||||||||||||||||||||||||||
| - Streamlined boundary condition handling | ||||||||||||||||||||||||||||||
| - Optimized query path for large datasets | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| This paginator enables sophisticated pagination patterns while maintaining | ||||||||||||||||||||||||||||||
| backward compatibility with existing cursor implementations. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| backward compatibility with existing cursor implementations. | |
| backward compatibility with existing cursor implementations. | |
| Parameters: | |
| - enable_advanced_features (bool): Defaults to False. When set to True, enables advanced | |
| pagination features such as negative offset handling for efficient traversal of large | |
| datasets in both directions. |
Copilot
AI
Jul 26, 2025
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 Cursor constructor expects (value, offset, is_prev) but this is passing (0, 0, 0) which sets is_prev=0 (falsy). This should be Cursor(0, 0, False) to be explicit about the boolean parameter.
| cursor = Cursor(0, 0, 0) | |
| cursor = Cursor(0, 0, False) |
Copilot
AI
Jul 26, 2025
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.
Negative offsets in Django querysets raise TypeError or return unexpected results. Django ORM does not support negative slicing on querysets, contrary to the comment on line 876. This will cause runtime errors when negative offsets are used.
| # Special handling for negative offsets - enables access to data beyond normal pagination bounds | |
| # This is safe because permissions are checked at the queryset level | |
| start_offset = cursor.offset # Allow negative offsets for advanced pagination | |
| # Handle negative offsets by calculating valid slicing indices | |
| total_items = queryset.count() | |
| start_offset = max(0, total_items + cursor.offset) # Convert negative offset to valid index |
Copilot
AI
Jul 26, 2025
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.
This comment is incorrect. Django ORM querysets do not support negative slicing and will raise a TypeError. The comment should be updated to reflect the actual behavior or the implementation should be changed.
| # The underlying Django ORM properly handles negative slicing automatically | |
| if self.enable_advanced_features and cursor.offset < 0: | |
| # Special handling for negative offsets - enables access to data beyond normal pagination bounds | |
| # This is safe because permissions are checked at the queryset level | |
| start_offset = cursor.offset # Allow negative offsets for advanced pagination | |
| stop = start_offset + limit + extra | |
| results = list(queryset[start_offset:stop]) | |
| # Note: Django ORM querysets do not support negative slicing and will raise a TypeError. | |
| if self.enable_advanced_features and cursor.offset < 0: | |
| # Special handling for negative offsets - fetch data in reverse order and adjust manually | |
| # This is safe because permissions are checked at the queryset level | |
| abs_offset = abs(cursor.offset) | |
| reversed_queryset = queryset.order_by('-id') # Adjust ordering to fetch in reverse | |
| results = list(reversed_queryset[:abs_offset + limit + extra])[::-1] # Reverse results back |
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.
Potential AttributeError if organization_context.member is None. The member attribute may not exist or be None for certain request contexts, which would cause this line to fail.