diff --git a/src/sentry/api/paginator.py b/src/sentry/api/paginator.py index 6e01f307b95496..4c48b1fe8c4d25 100644 --- a/src/sentry/api/paginator.py +++ b/src/sentry/api/paginator.py @@ -295,8 +295,12 @@ def get_result( if self.on_results: results = self.on_results(results) - if count_hits: - hits = self.count_hits(max_hits=MAX_HITS_LIMIT) + if known_hits is not None: + hits = known_hits + elif count_hits: + if max_hits is None: + max_hits = MAX_HITS_LIMIT + hits = self.count_hits(max_hits) else: hits = None diff --git a/src/sentry/workflow_engine/endpoints/serializers/workflow_group_history_serializer.py b/src/sentry/workflow_engine/endpoints/serializers/workflow_group_history_serializer.py index 832399e70a5759..23ed916b565600 100644 --- a/src/sentry/workflow_engine/endpoints/serializers/workflow_group_history_serializer.py +++ b/src/sentry/workflow_engine/endpoints/serializers/workflow_group_history_serializer.py @@ -128,11 +128,14 @@ def fetch_workflow_groups_paginated( .annotate(detector_id=Subquery(group_max_dates.values("detector_id"))) ) + # Count distinct groups for pagination + group_count = qs.count() + return cast( CursorResult[Group], OffsetPaginator( qs, order_by=("-count", "-last_triggered"), on_results=convert_results, - ).get_result(per_page, cursor, count_hits=True), + ).get_result(per_page, cursor, known_hits=group_count), ) diff --git a/tests/sentry/workflow_engine/endpoints/test_organization_workflow_group_history.py b/tests/sentry/workflow_engine/endpoints/test_organization_workflow_group_history.py index 9e927aa7f82b95..b706dc7dc39707 100644 --- a/tests/sentry/workflow_engine/endpoints/test_organization_workflow_group_history.py +++ b/tests/sentry/workflow_engine/endpoints/test_organization_workflow_group_history.py @@ -96,7 +96,7 @@ def test_pagination(self) -> None: self.user, WorkflowGroupHistorySerializer(), ) - assert resp["X-Hits"] == "4" + assert resp["X-Hits"] == "2" # 2 unique groups, not 4 total history records resp = self.get_success_response( self.organization.slug, @@ -119,7 +119,7 @@ def test_pagination(self) -> None: self.user, WorkflowGroupHistorySerializer(), ) - assert resp["X-Hits"] == "4" + assert resp["X-Hits"] == "2" # 2 unique groups, not 4 total history records def test_invalid_dates_error(self) -> None: self.get_error_response(