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.

feat(alerts): filter platform alerts by legacy configuration id (#120015)

Jimmy Zhu4288562d7f72
diff --git a/products/alerts_platform/backend/facade/api.py b/products/alerts_platform/backend/facade/api.py
index 4fe419a2ce65..671fbc098bf2 100644
--- a/products/alerts_platform/backend/facade/api.py
+++ b/products/alerts_platform/backend/facade/api.py
@@ -107,10 +107,22 @@ def disable_configurations(
def list_configurations(
- *, team_id: int, source_kinds: Sequence[str], limit: int, offset: int
+ *,
+ team_id: int,
+ source_kinds: Sequence[str],
+ limit: int,
+ offset: int,
+ legacy_configuration_id: UUID | None = None,
) -> PlatformAlertConfigurationPage:
- """One page of configurations a caller may read, with the total behind it."""
- return platform_reads.list_configurations(team_id=team_id, source_kinds=source_kinds, limit=limit, offset=offset)
+ """One page of configurations a caller may read, with the total behind it. With a
+ `legacy_configuration_id`, only the copy of that source row."""
+ return platform_reads.list_configurations(
+ team_id=team_id,
+ source_kinds=source_kinds,
+ limit=limit,
+ offset=offset,
+ legacy_configuration_id=legacy_configuration_id,
+ )
def get_configuration(
diff --git a/products/alerts_platform/backend/logic/platform_reads.py b/products/alerts_platform/backend/logic/platform_reads.py
index be0de0f70563..b4884e4862bc 100644
--- a/products/alerts_platform/backend/logic/platform_reads.py
+++ b/products/alerts_platform/backend/logic/platform_reads.py
@@ -92,15 +92,23 @@ def _readable(team_id: int, source_kinds: Sequence[str]) -> QuerySet[PlatformAle
def list_configurations(
- *, team_id: int, source_kinds: Sequence[str], limit: int, offset: int
+ *,
+ team_id: int,
+ source_kinds: Sequence[str],
+ limit: int,
+ offset: int,
+ legacy_configuration_id: UUID | None = None,
) -> PlatformAlertConfigurationPage:
- """One page of configurations, newest first, with the total behind it.
+ """One page of configurations, newest first, with the total behind it. With a
+ `legacy_configuration_id`, only the copy of that source row.
The page is taken in the database rather than in the caller, so a team with many alerts
costs one page rather than all of them. Slicing keeps the prefetch, which resolves over the
page's rows alone.
"""
readable = _readable(team_id, source_kinds)
+ if legacy_configuration_id is not None:
+ readable = readable.filter(legacy_configuration_id=legacy_configuration_id)
return PlatformAlertConfigurationPage(
configurations=tuple(_configuration_view(row) for row in readable[offset : offset + limit]),
total=readable.count(),
diff --git a/products/alerts_platform/backend/presentation/views/platform_alert.py b/products/alerts_platform/backend/presentation/views/platform_alert.py
index 35b479571046..d5fa683e7ede 100644
--- a/products/alerts_platform/backend/presentation/views/platform_alert.py
+++ b/products/alerts_platform/backend/presentation/views/platform_alert.py
@@ -205,6 +205,13 @@ class PlatformAlertConfigurationSerializer(serializers.Serializer):
)
+class PlatformAlertConfigurationListQuerySerializer(serializers.Serializer):
+ legacy_configuration_id = serializers.UUIDField(
+ required=False,
+ help_text="Only the alert copied from, or created with, this source configuration, such as a logs alert's id.",
+ )
+
+
# A configuration shows the source product's data (its filters and firing state), so a reader
# also needs read access to that product. `alert:read` alone must not reveal it.
SOURCE_KIND_RESOURCE: dict[str, APIScopeObject] = {
@@ -605,7 +612,10 @@ class PlatformAlertConfigurationViewSet(TeamAndOrgViewSetMixin, viewsets.Generic
raise NotFound()
return Response(PlatformAlertSerializer(group).data)
+ @extend_schema(parameters=[PlatformAlertConfigurationListQuerySerializer])
def list(self, request: Request, **kwargs) -> Response:
+ query = PlatformAlertConfigurationListQuerySerializer(data=request.query_params)
+ query.is_valid(raise_exception=True)
paginator = cast(PrecountedLimitOffsetPagination, self.paginator)
limit = paginator.get_limit(request)
assert limit is not None
@@ -614,6 +624,7 @@ class PlatformAlertConfigurationViewSet(TeamAndOrgViewSetMixin, viewsets.Generic
source_kinds=self._readable_source_kinds(),
limit=limit,
offset=paginator.get_offset(request),
+ legacy_configuration_id=query.validated_data.get("legacy_configuration_id"),
)
paginator.set_count(page.total)
return self.get_paginated_response(
diff --git a/products/alerts_platform/backend/tests/api/test_platform_alert.py b/products/alerts_platform/backend/tests/api/test_platform_alert.py
index b5c527380612..d7bf826b78fb 100644
--- a/products/alerts_platform/backend/tests/api/test_platform_alert.py
+++ b/products/alerts_platform/backend/tests/api/test_platform_alert.py
@@ -212,10 +212,11 @@ class TestPlatformAlertAPI(APIBaseTest):
assert [r["id"] for r in list_response.json()["results"]] == []
assert retrieve_response.status_code == status.HTTP_404_NOT_FOUND, retrieve_response.json()
- def test_a_malformed_id_is_refused_rather_than_raising(self) -> None:
+ @parameterized.expand([("retrieve", "not-a-uuid/"), ("legacy_id_filter", "?legacy_configuration_id=not-a-uuid")])
+ def test_a_malformed_id_is_refused_rather_than_raising(self, _name: str, path: str) -> None:
self._set_flag(True)
- response = self.client.get(f"/api/projects/{self.team.id}/platform_alerts/not-a-uuid/")
+ response = self.client.get(f"/api/projects/{self.team.id}/platform_alerts/{path}")
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()
@@ -237,6 +238,19 @@ class TestPlatformAlertAPI(APIBaseTest):
assert set(first_ids).isdisjoint(second_ids)
assert set(first_ids) | set(second_ids) == created
+ def test_the_legacy_id_filter_finds_the_copy_of_one_source_alert(self) -> None:
+ self._set_flag(True)
+ legacy_configuration_id = uuid4()
+ wanted = self._create_configuration(self.team, "Copied", legacy_configuration_id=legacy_configuration_id)
+ self._create_configuration(self.team, "Another copy", legacy_configuration_id=uuid4())
+
+ response = self.client.get(
+ f"/api/projects/{self.team.id}/platform_alerts/?legacy_configuration_id={legacy_configuration_id}"
+ )
+
+ assert response.status_code == status.HTTP_200_OK, response.json()
+ assert [(row["id"], response.json()["count"]) for row in response.json()["results"]] == [(str(wanted.id), 1)]
+
def test_a_page_costs_the_same_queries_however_many_rows_it_holds(self) -> None:
# A filter added to the prefetched relation would bypass the cache and cost a query per
# configuration, which no assertion on the response body would notice.
diff --git a/products/alerts_platform/frontend/generated/api.schemas.ts b/products/alerts_platform/frontend/generated/api.schemas.ts
index ca0b7e0ca402..8e3bf886158f 100644
--- a/products/alerts_platform/frontend/generated/api.schemas.ts
+++ b/products/alerts_platform/frontend/generated/api.schemas.ts
@@ -834,6 +834,10 @@ export interface PlatformAlertGroupSnoozeApi {
}
export type PlatformAlertsListParams = {
+ /**
+ * Only the alert copied from, or created with, this source configuration, such as a logs alert's id.
+ */
+ legacy_configuration_id?: string
/**
* Number of results to return per page.
*/
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 13cc7c033c10..3b990d4d6302 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -131732,6 +131732,10 @@ export namespace Schemas {
} as const;
export type PlatformAlertsListParams = {
+ /**
+ * Only the alert copied from, or created with, this source configuration, such as a logs alert's id.
+ */
+ legacy_configuration_id?: string;
/**
* Number of results to return per page.
*/