Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(health): break ordering ties in the health issues list (#102203)

posthog[bot]5b782d78025f
diff --git a/posthog/api/health_issue.py b/posthog/api/health_issue.py
index 85442c0ce1ef..aeab9a9d8a1c 100644
--- a/posthog/api/health_issue.py
+++ b/posthog/api/health_issue.py
@@ -441,7 +441,9 @@ class HealthIssueViewSet(TeamAndOrgViewSetMixin, ListModelMixin, RetrieveModelMi
queryset = (
queryset.filter(team_id=self.team_id)
.annotate(severity_order=SEVERITY_ORDERING)
- .order_by("severity_order", "-created_at")
+ # `bulk_upsert` stamps one `created_at` on a whole batch, so severity and
+ # `created_at` leave ties. The id breaks them, so limit-offset pages stay stable.
+ .order_by("severity_order", "-created_at", "-id")
)
if status_filter := self.request.query_params.get("status"):
diff --git a/posthog/api/test/test_health_issue.py b/posthog/api/test/test_health_issue.py
index f88ecb4bc82f..3de2e7e25991 100644
--- a/posthog/api/test/test_health_issue.py
+++ b/posthog/api/test/test_health_issue.py
@@ -55,6 +55,26 @@ class TestHealthIssueAPI(APIBaseTest):
self.assertEqual(results[1]["id"], str(warning.id))
self.assertEqual(results[2]["id"], str(info.id))
+ def test_list_pages_tied_issues_without_skipping_or_repeating(self):
+ shared_created_at = datetime.now(UTC)
+ issues = [
+ self._create_issue(
+ severity=HealthIssue.Severity.WARNING,
+ unique_hash=f"h{index}",
+ created_at=shared_created_at,
+ )
+ for index in range(6)
+ ]
+
+ paged_ids: list[str] = []
+ for offset in range(0, len(issues), 2):
+ response = self.client.get(self._url(), {"limit": 2, "offset": offset})
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ paged_ids.extend(result["id"] for result in response.json()["results"])
+
+ expected_ids = [str(issue.id) for issue in sorted(issues, key=lambda issue: issue.id, reverse=True)]
+ self.assertEqual(paged_ids, expected_ids)
+
@parameterized.expand(
[
("status", "active", 2),