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(dashboards): page the template list on a stable order (#103076)

posthog[bot]fa35fec49428
diff --git a/products/dashboards/backend/api/dashboard_templates.py b/products/dashboards/backend/api/dashboard_templates.py
index eab7a82502d7..a0987d2905a1 100644
--- a/products/dashboards/backend/api/dashboard_templates.py
+++ b/products/dashboards/backend/api/dashboard_templates.py
@@ -73,13 +73,15 @@ def enforce_organization_dashboard_template_limit(*, organization_id: UUID) -> N
def _dashboard_template_list_order_by(ordering: str | None) -> list[Any]:
"""Featured rows first, then order by `template_name` or `created_at` (when `ordering` requests it)."""
+ # Neither template_name nor created_at is unique across the visible templates, so each order needs `id`
+ # to page reliably. The `id` direction mirrors the column above it.
if ordering == "-template_name":
- return ["-is_featured", OrderBy(Lower("template_name"), descending=True)]
+ return ["-is_featured", OrderBy(Lower("template_name"), descending=True), "-id"]
if ordering == "created_at":
- return ["-is_featured", "created_at"]
+ return ["-is_featured", "created_at", "id"]
if ordering == "-created_at":
- return ["-is_featured", "-created_at"]
- return ["-is_featured", Lower("template_name")]
+ return ["-is_featured", "-created_at", "-id"]
+ return ["-is_featured", Lower("template_name"), "id"]
def _collect_warehouse_table_names(node: Any) -> set[str]:
@@ -661,7 +663,7 @@ class DashboardTemplateViewSet(TeamAndOrgViewSetMixin, ForbidDestroyModel, views
rank=build_rank({"template_name": "A", "dashboard_description": "C", "tags": "B"}, search),
)
qs = qs.filter(rank__gt=0.05)
- qs = qs.order_by("-is_featured", "-rank", Lower("template_name"))
+ qs = qs.order_by("-is_featured", "-rank", Lower("template_name"), "id")
else:
qs = qs.order_by(*_dashboard_template_list_order_by(ordering))
diff --git a/products/dashboards/backend/api/test/test_dashboard_templates.py b/products/dashboards/backend/api/test/test_dashboard_templates.py
index 0c43af3a7927..8b8ebf61e49c 100644
--- a/products/dashboards/backend/api/test/test_dashboard_templates.py
+++ b/products/dashboards/backend/api/test/test_dashboard_templates.py
@@ -746,6 +746,49 @@ class TestDashboardTemplates(APIBaseTest):
assert len(default_results) == 2
assert [r["template_name"] for r in default_results] == ["Alpha", "Zebra"]
+ def create_templates_sharing_name_and_created_at(self) -> list[str]:
+ """Six org-scoped templates that tie on every sort column, one per team so the shared name is allowed."""
+ DashboardTemplate.objects.all().delete()
+ templates = [
+ DashboardTemplate.objects.create(
+ team=Team.objects.create(organization=self.organization, name=f"Tied team {index}"),
+ template_name="Tied name",
+ scope=DashboardTemplate.Scope.ORGANIZATION,
+ is_featured=False,
+ )
+ for index in range(6)
+ ]
+ DashboardTemplate.objects.all().update(created_at=datetime(2020, 6, 1, 12, 0, 0, tzinfo=UTC))
+ return [str(template.id) for template in templates]
+
+ def walk_template_list_pages(self, query: str) -> list[str]:
+ url: Optional[str] = f"/api/projects/{self.team.pk}/dashboard_templates/{query}"
+ paged_ids: list[str] = []
+ while url:
+ page = self.client.get(url)
+ assert page.status_code == status.HTTP_200_OK
+ body = page.json()
+ paged_ids.extend(result["id"] for result in body["results"])
+ url = body["next"]
+ return paged_ids
+
+ @parameterized.expand(
+ [
+ ("ordering=", True),
+ ("ordering=template_name", True),
+ ("ordering=-template_name", False),
+ ("ordering=created_at", True),
+ ("ordering=-created_at", False),
+ ("search=Tied", True),
+ ]
+ )
+ def test_list_pages_templates_with_equal_sort_values(self, query: str, ascending_ids: bool) -> None:
+ template_ids = self.create_templates_sharing_name_and_created_at()
+
+ paged_ids = self.walk_template_list_pages(f"?{query}&limit=1")
+
+ assert paged_ids == sorted(template_ids, reverse=not ascending_ids)
+
def test_featured_templates_list_before_non_featured_when_listing_without_search(self) -> None:
DashboardTemplate.objects.all().delete()
self.create_template(