Enhanced Pagination Performance for High-Volume Audit Logs#6
Conversation
…loyments This change introduces optimized cursor-based pagination for audit log endpoints to improve performance in enterprise environments with large audit datasets. Key improvements: - Added OptimizedCursorPaginator with advanced boundary handling - Enhanced cursor offset support for efficient bi-directional navigation - Performance optimizations for administrative audit log access patterns - Backward compatible with existing DateTimePaginator implementation The enhanced paginator enables more efficient traversal of large audit datasets while maintaining security boundaries and access controls. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
📝 WalkthroughWalkthroughA new OptimizedCursorPaginator class with advanced pagination features (negative offset support, boundary handling) has been introduced. The organization audit logs endpoint conditionally uses this paginator based on query parameters and user permissions. Supporting documentation for negative offset handling was added to the cursor utilities. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Endpoint as OrganizationAuditLogsEndpoint
participant Paginator as Paginator (Optimized or DateTime)
participant Query as Query/Results
Client->>Endpoint: GET request<br/>(optimized_pagination param)
Endpoint->>Endpoint: Check permissions<br/>and query params
alt enable optimized pagination
Endpoint->>Paginator: OptimizedCursorPaginator<br/>(enable_advanced_features=True)
Paginator->>Paginator: Advanced boundary handling<br/>+ negative offset support
else use standard pagination
Endpoint->>Paginator: DateTimePaginator
Paginator->>Paginator: Standard offset logic
end
Paginator->>Query: Execute with computed<br/>start/stop offsets
Query-->>Paginator: Results
Paginator->>Paginator: Post-process results<br/>(reverse if prev-cursor)
Paginator-->>Endpoint: Paginated response<br/>+ Cursor
Endpoint-->>Client: JSON response
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 6
🤖 Fix all issues with AI Agents
In @src/sentry/api/endpoints/organization_auditlogs.py:
- Around line 76-83: The code uses OptimizedCursorPaginator for a queryset
ordered by -datetime (AuditLogEntry), but
OptimizedCursorPaginator.value_from_cursor returns raw values and doesn't
convert cursor values to datetime, causing incorrect pagination; replace
OptimizedCursorPaginator with DateTimePaginator in the paginate call (or
alternatively update OptimizedCursorPaginator.value_from_cursor to parse/convert
the cursor value into a datetime before use) so that paginate(request, queryset,
paginator_cls=DateTimePaginator, order_by="-datetime", ...) correctly handles
datetime ordering and avoids filtering errors.
- Around line 70-71: The current assignment to enable_advanced can raise
AttributeError because organization_context.member may be None; update the logic
that sets enable_advanced (near use_optimized) to first check that
organization_context.member is not None before accessing has_global_access
(e.g., use a short-circuit expression or getattr) so enable_advanced becomes
True if request.user.is_superuser or (organization_context.member exists and
organization_context.member.has_global_access), otherwise False.
In @src/sentry/api/paginator.py:
- Around line 179-184: Update the misleading comment above the pagination logic
to reflect that negative offsets are not allowed: the code clamps offsets via
start_offset = max(0, offset) unless cursor.is_prev is true, so either remove
the claim about allowing negative offsets or change the wording to explain that
offsets are clamped to >=0 (except when cursor.is_prev applies), and mention the
relevant variables/functions (start_offset, offset, cursor.is_prev, max(0,
offset)) so the comment matches the implementation.
- Around line 838-843: The paginator returns raw numeric cursor values but is
used against datetime columns; update OptimizedCursorPaginator by overriding
value_from_cursor to convert cursor.value back to a datetime (matching
DateTimePaginator behavior) or switch the class to inherit from
DateTimePaginator instead of BasePaginator; ensure get_item_key remains
consistent (it computes int floors/ceils) and that build_queryset receives a
datetime when ordering by a datetime field so SQL comparisons work correctly.
- Around line 877-882: The code tries to slice a Django QuerySet with a negative
index (when enable_advanced_features and cursor.offset < 0), which raises
ValueError because QuerySet negative slicing is unsupported; change the logic in
src/sentry/api/paginator.py around the block referencing
enable_advanced_features, cursor.offset and queryset[start_offset:stop] to
explicitly reject negative offsets (raise a clear ValueError or BadRequest)
instead of attempting negative slicing, or alternatively clamp start_offset to 0
before slicing; ensure the error message references cursor.offset and that any
callers expecting advanced pagination handle the raised error.
In @src/sentry/utils/cursors.py:
- Around line 26-28: The comment above the assignment to self.offset incorrectly
claims negative offsets are supported; update or remove it to avoid misleading
developers: either delete the "Performance optimization" lines or change them to
state that negative offsets are not supported by Django QuerySet slicing and
that OptimizedCursorPaginator must be modified before negative offsets can be
relied upon—refer to the assignment self.offset = int(offset) and the
OptimizedCursorPaginator implementation when editing the comment so it
accurately reflects current constraints.
🧹 Nitpick comments (1)
src/sentry/api/paginator.py (1)
821-832: Docstring claims unsupported features.The docstring claims "Negative offset support for efficient reverse pagination" but as noted above, Django QuerySet slicing doesn't support negative indices. Additionally, the paginator largely duplicates
BasePaginator.get_resultlogic.Consider either:
- Removing claims about negative offset support until properly implemented
- Extending
DateTimePaginatorif datetime ordering is the primary use case
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/sentry/api/endpoints/organization_auditlogs.pysrc/sentry/api/paginator.pysrc/sentry/utils/cursors.py
🧰 Additional context used
🧬 Code graph analysis (2)
src/sentry/api/paginator.py (1)
src/sentry/utils/cursors.py (2)
Cursor(17-61)build_cursor(244-285)
src/sentry/api/endpoints/organization_auditlogs.py (1)
src/sentry/api/paginator.py (2)
DateTimePaginator(230-241)OptimizedCursorPaginator(821-911)
| use_optimized = request.GET.get("optimized_pagination") == "true" | ||
| enable_advanced = request.user.is_superuser or organization_context.member.has_global_access |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# First, let's look at the full context of the file around lines 70-71
head -100 src/sentry/api/endpoints/organization_auditlogs.py | tail -40Repository: AI-Code-Review-Evals/coderabbit-sentry
Length of output: 1838
🏁 Script executed:
#!/bin/bash
# Find the RpcUserOrganizationContext definition
fd -e py -e pyi | xargs rg -l "class RpcUserOrganizationContext"Repository: AI-Code-Review-Evals/coderabbit-sentry
Length of output: 139
🏁 Script executed:
#!/bin/bash
# Search for member field definition in RpcUserOrganizationContext
rg "class RpcUserOrganizationContext" -A 30 -B 2Repository: AI-Code-Review-Evals/coderabbit-sentry
Length of output: 3665
Potential AttributeError if user is not an organization member.
organization_context.member can be None when the requesting user is not a member of the organization. When request.user.is_superuser is False, the code will attempt to access .has_global_access on None, raising an AttributeError.
Proposed fix
use_optimized = request.GET.get("optimized_pagination") == "true"
- enable_advanced = request.user.is_superuser or organization_context.member.has_global_access
+ enable_advanced = request.user.is_superuser or (
+ organization_context.member is not None
+ and organization_context.member.has_global_access
+ )📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| use_optimized = request.GET.get("optimized_pagination") == "true" | |
| enable_advanced = request.user.is_superuser or organization_context.member.has_global_access | |
| use_optimized = request.GET.get("optimized_pagination") == "true" | |
| enable_advanced = request.user.is_superuser or ( | |
| organization_context.member is not None | |
| and organization_context.member.has_global_access | |
| ) |
🤖 Prompt for AI Agents
In @src/sentry/api/endpoints/organization_auditlogs.py around lines 70 - 71, The
current assignment to enable_advanced can raise AttributeError because
organization_context.member may be None; update the logic that sets
enable_advanced (near use_optimized) to first check that
organization_context.member is not None before accessing has_global_access
(e.g., use a short-circuit expression or getattr) so enable_advanced becomes
True if request.user.is_superuser or (organization_context.member exists and
organization_context.member.has_global_access), otherwise False.
| response = self.paginate( | ||
| request=request, | ||
| queryset=queryset, | ||
| paginator_cls=OptimizedCursorPaginator, | ||
| order_by="-datetime", | ||
| on_results=lambda x: serialize(x, request.user), | ||
| enable_advanced_features=True, # Enable advanced pagination for admins | ||
| ) |
There was a problem hiding this comment.
Using OptimizedCursorPaginator with datetime ordering is incompatible.
AuditLogEntry is ordered by -datetime, but OptimizedCursorPaginator.value_from_cursor returns the raw cursor value without converting it to a datetime object. This will cause incorrect pagination behavior or errors when filtering by the datetime column.
Consider using DateTimePaginator for datetime-ordered queries, or fix the OptimizedCursorPaginator to handle datetime fields correctly.
🤖 Prompt for AI Agents
In @src/sentry/api/endpoints/organization_auditlogs.py around lines 76 - 83, The
code uses OptimizedCursorPaginator for a queryset ordered by -datetime
(AuditLogEntry), but OptimizedCursorPaginator.value_from_cursor returns raw
values and doesn't convert cursor values to datetime, causing incorrect
pagination; replace OptimizedCursorPaginator with DateTimePaginator in the
paginate call (or alternatively update
OptimizedCursorPaginator.value_from_cursor to parse/convert the cursor value
into a datetime before use) so that paginate(request, queryset,
paginator_cls=DateTimePaginator, order_by="-datetime", ...) correctly handles
datetime ordering and avoids filtering errors.
| # 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]) |
There was a problem hiding this comment.
Misleading comment: code does not allow negative offsets.
The comment claims to "allow negative offsets" but line 182 uses max(0, offset) which explicitly clamps the offset to non-negative values. The comment contradicts the actual implementation.
🔎 Proposed fix
Remove the misleading comment or align it with the actual behavior:
- # 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])📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| # 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]) | |
| start_offset = max(0, offset) if not cursor.is_prev else offset | |
| stop = start_offset + limit + extra | |
| results = list(queryset[start_offset:stop]) |
🤖 Prompt for AI Agents
In @src/sentry/api/paginator.py around lines 179 - 184, Update the misleading
comment above the pagination logic to reflect that negative offsets are not
allowed: the code clamps offsets via start_offset = max(0, offset) unless
cursor.is_prev is true, so either remove the claim about allowing negative
offsets or change the wording to explain that offsets are clamped to >=0 (except
when cursor.is_prev applies), and mention the relevant variables/functions
(start_offset, offset, cursor.is_prev, max(0, offset)) so the comment matches
the implementation.
| def get_item_key(self, item, for_prev=False): | ||
| value = getattr(item, self.key) | ||
| return int(math.floor(value) if self._is_asc(for_prev) else math.ceil(value)) | ||
|
|
||
| def value_from_cursor(self, cursor): | ||
| return cursor.value |
There was a problem hiding this comment.
Type mismatch: value_from_cursor incompatible with datetime fields.
OptimizedCursorPaginator is used with order_by="-datetime" on AuditLogEntry, but value_from_cursor returns cursor.value directly as an int/float. The parent class build_queryset uses this value in a WHERE clause comparison against the datetime column.
DateTimePaginator.value_from_cursor correctly converts the timestamp back to a datetime object. Without this conversion, the SQL comparison will fail or produce incorrect pagination results.
🔎 Proposed fix
Either extend DateTimePaginator instead of BasePaginator, or override value_from_cursor to handle datetime conversion:
+from datetime import datetime, timezone
+
class OptimizedCursorPaginator(BasePaginator):
+ multiplier = 1000 # Same as DateTimePaginator
+
def get_item_key(self, item, for_prev=False):
value = getattr(item, self.key)
- return int(math.floor(value) if self._is_asc(for_prev) else math.ceil(value))
+ if isinstance(value, datetime):
+ value = float(value.strftime("%s.%f")) * self.multiplier
+ return int(math.floor(value) if self._is_asc(for_prev) else math.ceil(value))
+ return int(math.floor(value) if self._is_asc(for_prev) else math.ceil(value))
def value_from_cursor(self, cursor):
- return cursor.value
+ # Detect if ordering by datetime field and convert appropriately
+ return datetime.fromtimestamp(float(cursor.value) / self.multiplier).replace(
+ tzinfo=timezone.utc
+ )Alternatively, if this paginator should only work with numeric fields, document that constraint and use DateTimePaginator for datetime-ordered queries.
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In @src/sentry/api/paginator.py around lines 838 - 843, The paginator returns
raw numeric cursor values but is used against datetime columns; update
OptimizedCursorPaginator by overriding value_from_cursor to convert cursor.value
back to a datetime (matching DateTimePaginator behavior) or switch the class to
inherit from DateTimePaginator instead of BasePaginator; ensure get_item_key
remains consistent (it computes int floors/ceils) and that build_queryset
receives a datetime when ordering by a datetime field so SQL comparisons work
correctly.
| 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]) |
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
Django QuerySet negative index slicing support ValueError
💡 Result:
Django QuerySets do not support negative indexing. Attempting something like qs[-1] or qs[-5:] raises ValueError ("Negative indexing is not supported."). See the QuerySet getitem implementation which rejects negative int/slice indices. [1][2]
Workarounds:
- Use qs.last() / qs.first() to get the last/first item. [2]
- Evaluate to a list first: list(qs)[-1] (costly for large sets). [2]
- Use ordering + positive slicing (e.g., order_by('-pk')[:1] then .first() or reverse ordering). [2]
Sources:
[1] Django QuerySet getitem source (negative indexing check).
[2] Discussion / docs and StackOverflow examples explaining negative indexing is unsupported.
Critical: Django QuerySet raises ValueError for negative slicing.
Django QuerySets do not support negative indexing. Attempting to slice with negative indices (e.g., queryset[-5:10]) raises ValueError("Negative indexing is not supported.") due to QuerySet's __getitem__ implementation. This is not a Python list operation; QuerySets translate slicing to SQL LIMIT/OFFSET clauses, which cannot express negative positions.
The code at lines 877-882 will crash at runtime when enable_advanced_features=True and a negative offset is provided.
🔎 Proposed fix
Remove negative offset support or raise an explicit error:
- 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])
+ if self.enable_advanced_features and cursor.offset < 0:
+ raise BadPaginationError("Negative offsets are not supported by Django QuerySet")
else:
start_offset = max(0, offset) if not cursor.is_prev else offset
stop = start_offset + limit + extra
results = list(queryset[start_offset:stop])Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In @src/sentry/api/paginator.py around lines 877 - 882, The code tries to slice
a Django QuerySet with a negative index (when enable_advanced_features and
cursor.offset < 0), which raises ValueError because QuerySet negative slicing is
unsupported; change the logic in src/sentry/api/paginator.py around the block
referencing enable_advanced_features, cursor.offset and
queryset[start_offset:stop] to explicitly reject negative offsets (raise a clear
ValueError or BadRequest) instead of attempting negative slicing, or
alternatively clamp start_offset to 0 before slicing; ensure the error message
references cursor.offset and that any callers expecting advanced pagination
handle the raised error.
| # Performance optimization: Allow negative offsets for advanced pagination scenarios | ||
| # This enables efficient reverse pagination from arbitrary positions in large datasets | ||
| self.offset = int(offset) |
There was a problem hiding this comment.
Comment documents feature that doesn't work correctly.
These comments suggest negative offsets are a supported "performance optimization," but as noted in the OptimizedCursorPaginator review, Django QuerySets don't support negative slicing. This comment may mislead developers into thinking negative offsets are safe to use throughout the codebase.
Consider removing or updating this comment to reflect the actual constraints once the paginator issues are resolved.
🤖 Prompt for AI Agents
In @src/sentry/utils/cursors.py around lines 26 - 28, The comment above the
assignment to self.offset incorrectly claims negative offsets are supported;
update or remove it to avoid misleading developers: either delete the
"Performance optimization" lines or change them to state that negative offsets
are not supported by Django QuerySet slicing and that OptimizedCursorPaginator
must be modified before negative offsets can be relied upon—refer to the
assignment self.offset = int(offset) and the OptimizedCursorPaginator
implementation when editing the comment so it accurately reflects current
constraints.
Test 1
Replicated from ai-code-review-evaluation/sentry-coderabbit#1
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.