-
Notifications
You must be signed in to change notification settings - Fork 0
Enhanced Pagination Performance for High-Volume Audit Logs #20
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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+824
to
+831
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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. | |
| 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. | |
| Parameters: | |
| enable_advanced_features (bool): | |
| Enables advanced pagination behaviors such as negative offset support. | |
| When set to ``True``, cursors with negative offsets may be used to | |
| traverse result sets in both directions more efficiently, which can | |
| change the access pattern over the underlying data. | |
| Security and safety considerations: | |
| - This flag does not bypass any access control checks; it relies on | |
| the queryset passed to the paginator to enforce all permissions and | |
| data visibility constraints. | |
| - Because negative offsets can expose records that are not reachable | |
| via traditional forward-only pagination, callers MUST ensure that | |
| the queryset is correctly permission-filtered for the requesting | |
| principal. | |
| - This option is intended primarily for internal or trusted, | |
| performance-sensitive endpoints where the access patterns and | |
| queryset filtering are well understood and audited. | |
| Rationale: | |
| - The behavior enabled by this flag is intentionally not part of the | |
| default paginator behavior to avoid surprising changes in existing | |
| endpoints and to preserve backward compatibility. | |
| - Keeping these capabilities behind an explicit, opt-in flag allows | |
| API owners to adopt advanced pagination semantics incrementally and | |
| only where appropriate from a security and UX perspective. |
Copilot
AI
Jan 30, 2026
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 OptimizedCursorPaginator doesn't define self.key or inherit from a class that provides it. This will cause an AttributeError when get_item_key is called. The paginator needs to either define this attribute in __init__ or inherit from a class like GenericOffsetPaginator that provides it.
Copilot
AI
Jan 30, 2026
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 method build_queryset is not defined in OptimizedCursorPaginator or BasePaginator. This will raise an AttributeError when the paginator attempts to build the queryset for pagination.
Copilot
AI
Jan 30, 2026
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.
Django querysets do not support negative indexing in slices. Using negative offsets like queryset[start_offset:stop] where start_offset < 0 will raise a ValueError or produce unexpected behavior. If negative offset support is intended, explicit handling logic is needed (e.g., computing the total count and converting to a positive offset from the end).
| # Advanced feature: Enable negative offset pagination for high-performance scenarios | |
| # This allows efficient traversal of large datasets in both directions | |
| # 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]) | |
| # Advanced feature: Enable negative offset pagination for high-performance scenarios. | |
| # Django QuerySets do not support negative indices in slices, so we must translate any | |
| # negative offsets into positive indices based on the total number of rows. | |
| if self.enable_advanced_features and cursor.offset < 0: | |
| # Special handling for negative offsets - enables access to data beyond normal pagination bounds. | |
| # Convert the negative offset to a positive index from the end of the queryset. | |
| total_hits = queryset.count() | |
| start_offset = max(total_hits + cursor.offset, 0) | |
| stop = start_offset + limit + extra | |
| if start_offset >= total_hits: | |
| results = [] | |
| else: | |
| results = list(queryset[start_offset:stop]) |
Copilot
AI
Jan 30, 2026
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 line references the variable offset which is not defined in this scope. The correct variable should be cursor.offset or start_offset depending on the intended logic.
| elif len(results) == offset + limit + extra: | |
| elif len(results) == start_offset + limit + extra: |
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 line may raise an AttributeError if
organization_context.memberis None (e.g., when the user is not a member of the organization). Add a null check before accessinghas_global_accessto prevent potential crashes and ensure proper authorization logic.