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): show a moved insight alert's state and checks from the platform (#120703)

Jimmy Zhu2600187003eb
diff --git a/products/alerts/backend/facade/contracts.py b/products/alerts/backend/facade/contracts.py
index fbf6a3f5a08d..8facb47d32c8 100644
--- a/products/alerts/backend/facade/contracts.py
+++ b/products/alerts/backend/facade/contracts.py
@@ -13,6 +13,7 @@ from typing import Any, Final
from uuid import UUID
from posthog.dataclasses import frozen
+from posthog.schema_enums import AlertState
from products.alerts_platform.backend.facade.contracts import DeliveryPath, DestinationType
from products.cdp.backend.internal_events import LEGACY_INSIGHT_ALERT_EVENT
@@ -136,3 +137,47 @@ class AlertInternalEvent:
class AlertInternalEventsNotDelivered(Exception):
"""The broker did not take every event, so the caller retries."""
+
+
+@frozen
+class MovedAlertRuntime:
+ """Where an alert the platform owns stands, in the insight alert's terms."""
+
+ state: AlertState
+ next_check_at: datetime | None
+ last_notified_at: datetime | None
+
+
+@frozen
+class MovedAlertLastCheck:
+ checked_at: datetime
+ value: float | None
+
+
+@frozen
+class MovedAlertStatus:
+ """What the insight alert API shows for an alert the platform owns, in place of its own row."""
+
+ # None when the platform holds no runtime for the alert.
+ runtime: MovedAlertRuntime | None
+ # None when the alert has no recent check, or the platform's history could not be read.
+ last_check: MovedAlertLastCheck | None
+
+
+@frozen
+class MovedAlertCheck:
+ """One check of an alert the platform owns, in the insight alert's terms."""
+
+ # The same on every read, so a client can key on it.
+ id: UUID
+ checked_at: datetime
+ value: float | None
+ state: AlertState
+ error_message: str | None
+
+
+@frozen
+class MovedAlertCheckPage:
+ # How many checks the requested window holds, past this page too.
+ total: int
+ checks: tuple[MovedAlertCheck, ...]
diff --git a/products/alerts/backend/facade/platform.py b/products/alerts/backend/facade/platform.py
index 3587efba5ad9..7eda1452b028 100644
--- a/products/alerts/backend/facade/platform.py
+++ b/products/alerts/backend/facade/platform.py
@@ -8,6 +8,7 @@ import uuid
from products.alerts.backend.models.alert import AlertConfiguration
from products.alerts.backend.platform_alert_backfill import follow_insight_alert as follow_alert
+from products.alerts.backend.platform_alert_reads import moved_alert_check_page, moved_alert_statuses
def follow_insight_alert(*, team_id: int, alert_id: uuid.UUID) -> None:
@@ -18,4 +19,4 @@ def follow_insight_alert(*, team_id: int, alert_id: uuid.UUID) -> None:
follow_alert(alert)
-__all__ = ["follow_insight_alert"]
+__all__ = ["follow_insight_alert", "moved_alert_check_page", "moved_alert_statuses"]
diff --git a/products/alerts/backend/platform_alert_reads.py b/products/alerts/backend/platform_alert_reads.py
new file mode 100644
index 000000000000..b39022e572a5
--- /dev/null
+++ b/products/alerts/backend/platform_alert_reads.py
@@ -0,0 +1,122 @@
+"""What the insight alert API shows for an alert the platform owns.
+
+After the shadow the insight stack stops checking a moved alert, so its row and its checks stop
+moving. The platform's runtime rows and check history say where the alert stands instead.
+"""
+
+from __future__ import annotations
+
+from collections.abc import Sequence
+from datetime import UTC, datetime, timedelta
+from typing import Final
+from uuid import NAMESPACE_URL, UUID, uuid5
+
+from posthog.exceptions_capture import capture_exception
+from posthog.schema_enums import AlertState
+from posthog.utils import ensure_utc
+
+from products.alerts.backend.facade.contracts import (
+ MovedAlertCheck,
+ MovedAlertCheckPage,
+ MovedAlertLastCheck,
+ MovedAlertRuntime,
+ MovedAlertStatus,
+)
+from products.alerts.backend.models.alert import AlertConfiguration
+from products.alerts.backend.platform_cutover import insight_state_of_platform
+from products.alerts_platform.backend.facade.api import (
+ check_page_by_legacy_id,
+ last_check_by_legacy_id,
+ runtime_by_legacy_id,
+)
+from products.alerts_platform.backend.facade.contracts import PlatformCheckEntry
+from products.alerts_platform.backend.facade.enums import PlatformAlertOwner, PlatformAlertState
+
+_EARLIEST: Final = datetime(1970, 1, 1, tzinfo=UTC)
+_LATEST: Final = datetime(2100, 1, 1, tzinfo=UTC)
+
+
+def moved_alert_statuses(*, team_id: int, alert_ids: Sequence[UUID], now: datetime) -> dict[UUID, MovedAlertStatus]:
+ """Each moved alert's state, schedule and last check as the platform has them.
+
+ Alerts the platform does not own are left out. Up to three Postgres queries and one ClickHouse
+ query. A ClickHouse failure leaves out the last check, and the response still goes out.
+ """
+ snoozed_until = dict(
+ AlertConfiguration.objects.filter(
+ team_id=team_id, id__in=alert_ids, owner=PlatformAlertOwner.PLATFORM
+ ).values_list("id", "snoozed_until")
+ )
+ if not snoozed_until:
+ return {}
+ moved = list(snoozed_until)
+ runtime = runtime_by_legacy_id(team_id=team_id, legacy_configuration_ids=moved)
+ try:
+ last_checks = last_check_by_legacy_id(team_id=team_id, legacy_configuration_ids=moved)
+ except Exception as error:
+ capture_exception(error, additional_properties={"team_id": team_id})
+ last_checks = {}
+ statuses = {}
+ for alert_id in moved:
+ alert_runtime = runtime.get(alert_id)
+ last_check = last_checks.get(alert_id)
+ statuses[alert_id] = MovedAlertStatus(
+ runtime=MovedAlertRuntime(
+ state=insight_state_of_platform(alert_runtime.state, snoozed_until=snoozed_until[alert_id], at=now),
+ next_check_at=alert_runtime.next_check_at,
+ last_notified_at=alert_runtime.last_notified_at,
+ )
+ if alert_runtime is not None
+ else None,
+ last_check=MovedAlertLastCheck(checked_at=ensure_utc(last_check.checked_at), value=last_check.value)
+ if last_check is not None
+ else None,
+ )
+ return statuses
+
+
+def moved_alert_check_page(
+ *,
+ team_id: int,
+ alert_id: UUID,
+ since: datetime | None,
+ until: datetime | None,
+ limit: int,
+ offset: int,
+) -> MovedAlertCheckPage | None:
+ """A page of a moved alert's checks in `[since, until]`, newest first, with how many the window holds.
+
+ None when the alert's own checks are the ones to show: the platform does not own it, or its
+ history could not be read.
+ """
+ if not AlertConfiguration.objects.filter(team_id=team_id, id=alert_id, owner=PlatformAlertOwner.PLATFORM).exists():
+ return None
+ try:
+ page = check_page_by_legacy_id(
+ team_id=team_id,
+ legacy_configuration_id=alert_id,
+ since=since or _EARLIEST,
+ # The platform's window leaves out its end, and the API's keeps a check made at that moment.
+ until=until + timedelta(microseconds=1) if until is not None else _LATEST,
+ limit=limit,
+ offset=offset,
+ )
+ except Exception as error:
+ capture_exception(error, additional_properties={"alert_configuration_id": str(alert_id)})
+ return None
+ if page is None:
+ return None
+ return MovedAlertCheckPage(total=page.total, checks=tuple(_as_check(alert_id, check) for check in page.checks))
+
+
+def _as_check(alert_id: UUID, check: PlatformCheckEntry) -> MovedAlertCheck:
+ return MovedAlertCheck(
+ id=uuid5(NAMESPACE_URL, f"{alert_id}|{check.evaluation_key}"),
+ checked_at=ensure_utc(check.occurred_at),
+ value=check.value,
+ # A snoozed check evaluated nothing, so it shows as snoozed rather than as a clear outcome.
+ state=AlertState.SNOOZED
+ if check.state == PlatformAlertState.SNOOZED
+ else insight_state_of_platform(check.state, snoozed_until=None, at=check.occurred_at),
+ error_message=check.error_message or None,
+ )
diff --git a/products/alerts/backend/platform_cutover.py b/products/alerts/backend/platform_cutover.py
index 3701cc2edf9b..68b87bd0925e 100644
--- a/products/alerts/backend/platform_cutover.py
+++ b/products/alerts/backend/platform_cutover.py
@@ -42,6 +42,8 @@ PLATFORM_HANDOVER_STATE: Final[dict[str, PlatformAlertState]] = {
# Insight has no broken state. A configuration the platform found broken is one the insight stack
# reports as errored and disables.
_INSIGHT_STATE_OF_PLATFORM: Final[dict[PlatformAlertState, AlertState]] = {
+ # Only a read sees a firing, because the platform keeps a firing alert until it resolves.
+ PlatformAlertState.FIRING: AlertState.FIRING,
PlatformAlertState.NOT_FIRING: AlertState.NOT_FIRING,
PlatformAlertState.SNOOZED: AlertState.NOT_FIRING,
PlatformAlertState.ERRORED: AlertState.ERRORED,
@@ -49,6 +51,18 @@ _INSIGHT_STATE_OF_PLATFORM: Final[dict[PlatformAlertState, AlertState]] = {
}
+def insight_state_of_platform(
+ platform_state: PlatformAlertState, *, snoozed_until: datetime | None, at: datetime
+) -> AlertState:
+ """The insight state for a platform state. A handback and the API's read of a moved alert both use it.
+
+ A snooze lives on the insight alert, so an active one stays its state.
+ """
+ if snoozed_until is not None and snoozed_until > at:
+ return AlertState.SNOOZED
+ return _INSIGHT_STATE_OF_PLATFORM[platform_state]
+
+
class CutoverSkip(StrEnum):
"""Why an alert stays with the insight stack for now."""
@@ -215,10 +229,8 @@ def _roll_back_alert(team_id: int, alert_id: UUID, now: datetime | None) -> Roll
if isinstance(handback, HandbackRefusal):
return _ROLLBACK_SKIP[handback]
- # A snooze lives on this alert, so an active one stays its state.
- snoozed = alert.snoozed_until is not None and alert.snoozed_until > at
update_fields = apply_platform_state(
- alert, AlertState.SNOOZED if snoozed else _INSIGHT_STATE_OF_PLATFORM[handback.state]
+ alert, insight_state_of_platform(handback.state, snoozed_until=alert.snoozed_until, at=at)
)
if not handback.enabled and alert.enabled:
# The platform switches a copy off only for a configuration it cannot run, which the
diff --git a/products/alerts/backend/presentation/views/alert.py b/products/alerts/backend/presentation/views/alert.py
index 02dc52f52496..b699edfa04c9 100644
--- a/products/alerts/backend/presentation/views/alert.py
+++ b/products/alerts/backend/presentation/views/alert.py
@@ -1,5 +1,6 @@
import json
import uuid
+from datetime import datetime
from typing import Annotated, Any, cast
from zoneinfo import ZoneInfo
@@ -90,7 +91,11 @@ from products.alerts.backend.facade.api import (
is_llm_detector_config,
llm_detector_access_error,
)
-from products.alerts.backend.facade.contracts import INSIGHT_ALERT_DESTINATION_TYPES, INSIGHT_ALERT_EVENT_IDS
+from products.alerts.backend.facade.contracts import (
+ INSIGHT_ALERT_DESTINATION_TYPES,
+ INSIGHT_ALERT_EVENT_IDS,
+ MovedAlertCheck,
+)
from products.alerts.backend.facade.destinations import (
MAX_DESTINATION_IDS_PER_DELETE_REQUEST,
MAX_DESTINATIONS_PER_ALERT,
@@ -101,10 +106,11 @@ from products.alerts.backend.facade.destinations import (
validate_destination_data,
)
from products.alerts.backend.facade.evaluation import DelayedEvaluationUnavailable, validate_evaluation_delay
-from products.alerts.backend.facade.platform import follow_insight_alert
+from products.alerts.backend.facade.platform import follow_insight_alert, moved_alert_check_page, moved_alert_statuses
from products.alerts.backend.insight_alert_state_machine import (
apply_disable,
apply_enable,
+ apply_platform_state,
apply_snooze,
apply_threshold_change,
apply_unsnooze,
@@ -1424,6 +1430,39 @@ class AlertListFiltersSerializer(serializers.Serializer):
has_detector = OptionalBooleanField(required=False)
+def _show_platform_status(alerts: list[AlertConfiguration], *, now: datetime) -> None:
+ """Shows each alert the platform owns as the platform has it, in place. Its own row stops
+ moving once the insight stack stops checking it."""
+ moved = {alert.id: alert for alert in alerts if alert.platform_owned}
+ if not moved:
+ return
+ statuses = moved_alert_statuses(team_id=next(iter(moved.values())).team_id, alert_ids=list(moved), now=now)
+ for alert_id, moved_status in statuses.items():
+ alert = moved[alert_id]
+ if moved_status.runtime is not None:
+ apply_platform_state(alert, moved_status.runtime.state)
+ alert.next_check_at = moved_status.runtime.next_check_at
+ alert.last_notified_at = moved_status.runtime.last_notified_at
+ if moved_status.last_check is not None:
+ alert.last_checked_at = moved_status.last_check.checked_at
+ # Annotated from the alert's own checks, which stop once the shadow ends.
+ alert.last_value = moved_status.last_check.value # type: ignore[attr-defined]
+
+
+def _as_alert_check(alert: AlertConfiguration, check: MovedAlertCheck) -> AlertCheck:
+ # Never saved. It carries what the check serializer shows.
+ return AlertCheck(
+ id=check.id,
+ alert_configuration=alert,
+ created_at=check.checked_at,
+ calculated_value=check.value,
+ condition=alert.condition,
+ state=check.state,
+ error={"message": check.error_message} if check.error_message else None,
+ targets_notified={},
+ )
+
+
@extend_schema_view(
list=extend_schema(
parameters=[
@@ -1601,14 +1640,14 @@ class AlertViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
checks_qs = instance.alertcheck_set.select_related("investigation_notebook").order_by("-created_at")
checks_date_from = request.query_params.get("checks_date_from")
- if checks_date_from:
- parsed_date = relative_date_parse(checks_date_from, ZoneInfo("UTC"))
- checks_qs = checks_qs.filter(created_at__gte=parsed_date)
+ checks_since = relative_date_parse(checks_date_from, ZoneInfo("UTC")) if checks_date_from else None
+ if checks_since is not None:
+ checks_qs = checks_qs.filter(created_at__gte=checks_since)
checks_date_to = request.query_params.get("checks_date_to")
- if checks_date_to:
- parsed_date = relative_date_parse(checks_date_to, ZoneInfo("UTC"))
- checks_qs = checks_qs.filter(created_at__lte=parsed_date)
+ checks_until = relative_date_parse(checks_date_to, ZoneInfo("UTC")) if checks_date_to else None
+ if checks_until is not None:
+ checks_qs = checks_qs.filter(created_at__lte=checks_until)
has_date_filter = checks_date_from or checks_date_to
raw_limit = request.query_params.get("checks_limit")
@@ -1629,10 +1668,27 @@ class AlertViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
else:
offset = 0
- checks_total = checks_qs.count()
- instance.checks_total = checks_total
- offset = min(offset, checks_total)
- instance.checks = checks_qs[offset : offset + limit]
+ _show_platform_status([instance], now=timezone.now())
+ platform_page = (
+ moved_alert_check_page(
+ team_id=instance.team_id,
+ alert_id=instance.id,
+ since=checks_since,
+ until=checks_until,
+ limit=limit,
+ offset=offset,
+ )
+ if instance.platform_owned
+ else None
+ )
+ if platform_page is not None:
+ instance.checks_total = platform_page.total
+ instance.checks = [_as_alert_check(instance, check) for check in platform_page.checks]
+ else:
+ checks_total = checks_qs.count()
+ instance.checks_total = checks_total
+ offset = min(offset, checks_total)
+ instance.checks = checks_qs[offset : offset + limit]
serializer = self.get_serializer(instance)
return Response(serializer.data)
@@ -1642,6 +1698,7 @@ class AlertViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
# Paginate first, then prefetch checks only for the page
page = self.paginate_queryset(queryset)
alerts = list(page) if page is not None else list(queryset)
+ _show_platform_status(alerts, now=timezone.now())
# Prefetch firing checks for anomaly point display on chart.
if alerts:
diff --git a/products/alerts/backend/tests/api/test_alert_platform_owned.py b/products/alerts/backend/tests/api/test_alert_platform_owned.py
new file mode 100644
index 000000000000..2afbae949a06
--- /dev/null
+++ b/products/alerts/backend/tests/api/test_alert_platform_owned.py
@@ -0,0 +1,107 @@
+from datetime import UTC, datetime, timedelta
+from typing import Any
+
+from posthog.test.base import APIBaseTest, ClickhouseTestMixin
+from unittest.mock import patch
+
+from parameterized import parameterized
+from rest_framework import status
+
+from posthog.models.scoping import team_scope
+
+from products.alerts.backend.models.alert import AlertCheck
+from products.alerts.backend.platform_cutover import cut_over_team
+from products.alerts.backend.test.insight_alerts import create_insight_alert
+from products.alerts_platform.backend.facade import testing as platform_testing
+
+
+def _iso(moment: datetime) -> str:
+ return moment.isoformat().replace("+00:00", "Z")
+
+
+class TestPlatformOwnedInsightAlert(ClickhouseTestMixin, APIBaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ self.now = datetime.now(UTC)
+ self.alert = create_insight_alert(
+ self.team,
+ next_check_at=self.now + timedelta(minutes=3),
+ last_checked_at=self.now - timedelta(hours=2),
+ )
+ AlertCheck.objects.create(alert_configuration=self.alert, calculated_value=1.0, state="Not firing")
+ [cutover] = cut_over_team(self.team.id, now=self.now)
+ assert cutover.skipped is None
+ with team_scope(self.team.id):
+ self.copy_id = platform_testing.copy_of(self.alert.id).id
+ self.url = f"/api/projects/{self.team.id}/alerts"
+
+ def _at(self, minutes_ago: int) -> datetime:
+ return self.now - timedelta(minutes=minutes_ago)
+
+ def _record(self, minutes_ago: int, state: str, *, value: float | None = None, error: str | None = None) -> None:
+ platform_testing.record_check(
+ self.copy_id,
+ team_id=self.team.id,
+ at=self._at(minutes_ago),
+ previous_state="not_firing",
+ state=state,
+ value=value,
+ error_message=error,
+ )
+
+ def _row(self, action: str, query: str = "") -> dict[str, Any]:
+ if action == "list":
+ response = self.client.get(f"{self.url}/")
+ assert response.status_code == status.HTTP_200_OK
+ [row] = response.json()["results"]
+ return row
+ response = self.client.get(f"{self.url}/{self.alert.id}/{query}")
+ assert response.status_code == status.HTTP_200_OK
+ return response.json()
+
+ @parameterized.expand([("list",), ("retrieve",)])
+ def test_a_moved_alert_shows_the_platform_state_and_last_check(self, action: str) -> None:
+ self._record(30, "firing", value=150.0)
+ with team_scope(self.team.id):
+ platform_testing.start_firing(self.copy_id, team_id=self.team.id, at=self._at(30))
+
+ row = self._row(action)
+
+ assert (row["state"], row["last_checked_at"], row["last_value"]) == ("Firing", _iso(self._at(30)), 150.0)
+
+ def test_a_moved_alert_lists_its_checks_from_the_platform(self) -> None:
+ self._record(90, "not_firing", value=50.0)
+ self._record(60, "firing", value=150.0)
+ self._record(30, "errored", error="Query timed out")
+ # A snoozed check evaluates nothing, so it measures no value either.
+ self._record(15, "snoozed")
+
+ row = self._row("retrieve", "?checks_limit=2")
+
+ assert row["checks_total"] == 4
+ assert [(check["created_at"], check["state"], check["calculated_value"]) for check in row["checks"]] == [
+ (_iso(self._at(15)), "Snoozed", None),
+ (_iso(self._at(30)), "Errored", None),
+ ]
+ # The newest check measured nothing, so an older check's value must not stand in for it.
+ assert row["last_value"] is None
+ # A platform error can carry an internal detail, so the history shows the generic message.
+ assert row["checks"][1]["error"] == {
+ "message": "This alert encountered an error. Check the alert configuration and try again."
+ }
+ past_the_end = self._row("retrieve", "?checks_offset=5")
+ assert (past_the_end["checks_total"], past_the_end["checks"]) == (4, [])
+ # A check made at the window's end, to the microsecond, is inside it.
+ up_to_a_check = self._row("retrieve", f"?checks_date_to={_iso(self._at(60))}")
+ assert [check["created_at"] for check in up_to_a_check["checks"]] == [_iso(self._at(60)), _iso(self._at(90))]
+
+ def test_a_platform_history_failure_falls_back_to_the_alerts_own_checks(self) -> None:
+ self._record(30, "firing", value=150.0)
+
+ with patch(
+ "products.alerts.backend.platform_alert_reads.check_page_by_legacy_id",
+ side_effect=RuntimeError("ClickHouse is down"),
+ ):
+ row = self._row("retrieve")
+
+ assert [(check["state"], check["calculated_value"]) for check in row["checks"]] == [("Not firing", 1.0)]
diff --git a/products/alerts_platform/backend/facade/api.py b/products/alerts_platform/backend/facade/api.py
index f8dd1581a83f..0935bc76db70 100644
--- a/products/alerts_platform/backend/facade/api.py
+++ b/products/alerts_platform/backend/facade/api.py
@@ -25,7 +25,9 @@ from products.alerts_platform.backend.facade.contracts import (
PlatformAlertSnapshot,
PlatformAlertUpsert,
PlatformCheckEventPage,
+ PlatformCheckPage,
PlatformHandback,
+ PlatformLastCheck,
PlatformOwnershipHandover,
SourceKind,
StoredAlertDestination,
@@ -147,6 +149,23 @@ def runtime_by_legacy_id(
return platform_reads.runtime_by_legacy_id(team_id=team_id, legacy_configuration_ids=legacy_configuration_ids)
+def check_page_by_legacy_id(
+ *, team_id: int, legacy_configuration_id: UUID, since: datetime, until: datetime, limit: int, offset: int
+) -> PlatformCheckPage | None:
+ """A page of one owned alert's checks in a window, newest first, or None when the platform does not own it.
+
+ Raises when ClickHouse fails, so a caller can fall back to what it shows without the platform.
+ """
+ return platform_reads.check_page_by_legacy_id(
+ team_id=team_id,
+ legacy_configuration_id=legacy_configuration_id,
+ since=since,
+ until=until,
+ limit=limit,
+ offset=offset,
+ )
+
+
def history_by_legacy_id(
*, team_id: int, legacy_configuration_ids: Collection[UUID], since: datetime, until: datetime
) -> dict[UUID, PlatformAlertHistory]:
@@ -159,6 +178,16 @@ def history_by_legacy_id(
)
+def last_check_by_legacy_id(
+ *, team_id: int, legacy_configuration_ids: Collection[UUID]
+) -> dict[UUID, PlatformLastCheck]:
+ """The newest check of each named alert the platform owns, keyed by the source's id.
+
+ Raises when ClickHouse fails, so a caller can fall back to what it shows without the platform.
+ """
+ return platform_reads.last_check_by_legacy_id(team_id=team_id, legacy_configuration_ids=legacy_configuration_ids)
+
+
def notable_checks_by_legacy_id(
*, team_id: int, legacy_configuration_id: UUID, since: datetime, limit: int
) -> PlatformCheckEventPage | None:
diff --git a/products/alerts_platform/backend/facade/contracts.py b/products/alerts_platform/backend/facade/contracts.py
index 7c2eefb4ac57..3e6607cb1b9e 100644
--- a/products/alerts_platform/backend/facade/contracts.py
+++ b/products/alerts_platform/backend/facade/contracts.py
@@ -437,6 +437,35 @@ class PlatformAlertHistory:
last_error_message: str | None
+@frozen
+class PlatformLastCheck:
+ """The newest check of an alert the platform owns."""
+
+ checked_at: datetime
+ # None when the check measured nothing.
+ value: float | None
+
+
+@frozen
+class PlatformCheckEntry:
+ """One check of an alert the platform owns, as a source lists its checks."""
+
+ occurred_at: datetime
+ evaluation_key: str
+ state: PlatformAlertState
+ value: float | None
+ # None when the check succeeded.
+ error_message: str | None
+
+
+@frozen
+class PlatformCheckPage:
+ """A page of an alert's checks, newest first, and how many checks the window holds."""
+
+ total: int
+ checks: tuple[PlatformCheckEntry, ...]
+
+
@frozen
class PlatformCheckEvent:
"""One check of an alert the platform owns that moved its state or failed."""
diff --git a/products/alerts_platform/backend/logic/platform_alert_events.py b/products/alerts_platform/backend/logic/platform_alert_events.py
index 47e187041f3b..d306db462c60 100644
--- a/products/alerts_platform/backend/logic/platform_alert_events.py
+++ b/products/alerts_platform/backend/logic/platform_alert_events.py
@@ -15,8 +15,8 @@ import json
import hashlib
from collections.abc import Collection, Sequence
from dataclasses import fields
-from datetime import datetime
-from typing import Any
+from datetime import UTC, datetime
+from typing import Any, Final
from uuid import UUID
import structlog
@@ -31,8 +31,11 @@ from products.alerts_platform.backend.facade.contracts import (
AnnouncedTransition,
EvaluationAnnouncement,
PlatformAlertHistory,
+ PlatformCheckEntry,
PlatformCheckEvent,
PlatformCheckEventPage,
+ PlatformCheckPage,
+ PlatformLastCheck,
PlatformStateChange,
SourceKind,
)
@@ -354,6 +357,16 @@ def history(
}
+# A source reads these to fill in a page it can show without them, so a stalled query gives up
+# rather than holding the request.
+_READ_BUDGET: Final = {"max_execution_time": 5}
+
+
+def _microseconds(moment: datetime) -> str:
+ # A datetime parameter is sent to the second, which would move a window's end past checks inside it.
+ return moment.astimezone(UTC).strftime("%Y-%m-%d %H:%M:%S.%f")
+
+
# Deduplicated as `_HISTORY_SQL` is. The quiet checks are dropped before the deduplication, which
# is safe because two copies of one check are identical rows. The total is a window over every
# matching check, so one query answers both the page and the count a paginated list needs.
@@ -407,3 +420,110 @@ def notable_checks(team_id: int, configuration_id: UUID, *, since: datetime, lim
),
total=rows[0][-1] if rows else 0,
)
+
+
+_CHECKS_SQL = f"""
+SELECT occurred_at, evaluation_key, state, value, error_message, count() OVER () AS total
+FROM (
+ SELECT alert_id, evaluation_key, occurred_at, state, value, error_message
+ FROM {PLATFORM_ALERT_EVENTS_TABLE}
+ WHERE team_id = %(team_id)s
+ AND configuration_id = %(configuration_id)s
+ AND grouping_key = ''
+ AND occurred_at >= toDateTime64(%(since)s, 6, 'UTC')
+ AND occurred_at < toDateTime64(%(until)s, 6, 'UTC')
+ ORDER BY alert_id, occurred_at, evaluation_key
+ LIMIT 1 BY alert_id, evaluation_key
+)
+ORDER BY occurred_at DESC, evaluation_key DESC
+LIMIT %(limit)s OFFSET %(offset)s
+"""
+
+_CHECK_COUNT_SQL = f"""
+SELECT count()
+FROM (
+ SELECT alert_id, evaluation_key
+ FROM {PLATFORM_ALERT_EVENTS_TABLE}
+ WHERE team_id = %(team_id)s
+ AND configuration_id = %(configuration_id)s
+ AND grouping_key = ''
+ AND occurred_at >= toDateTime64(%(since)s, 6, 'UTC')
+ AND occurred_at < toDateTime64(%(until)s, 6, 'UTC')
+ LIMIT 1 BY alert_id, evaluation_key
+)
+"""
+
+
+def check_page(
+ team_id: int, configuration_id: UUID, *, since: datetime, until: datetime, limit: int, offset: int
+) -> PlatformCheckPage:
+ """One configuration's checks in `[since, until)`, newest first, with how many the window holds.
+
+ Only the ungrouped instance is read, for the same reason as `history`. Raises on a ClickHouse
+ failure, because only the caller knows what it can show instead.
+ """
+ tag_queries(product=Product.PLATFORM_AND_SUPPORT, feature=Feature.ALERTING)
+ # Not `_execute`, for the same reason as `history`.
+ parameters = {
+ "team_id": team_id,
+ "configuration_id": configuration_id,
+ "since": _microseconds(since),
+ "until": _microseconds(until),
+ "limit": limit,
+ "offset": offset,
+ }
+ rows = sync_execute(_CHECKS_SQL, parameters, team_id=team_id, settings=_READ_BUDGET)
+ if rows:
+ total = rows[0][5]
+ elif offset:
+ # A page past the end has no row to carry the window's count, so it is counted on its own.
+ total = sync_execute(_CHECK_COUNT_SQL, parameters, team_id=team_id, settings=_READ_BUDGET)[0][0]
+ else:
+ total = 0
+ return PlatformCheckPage(
+ total=total,
+ checks=tuple(
+ PlatformCheckEntry(
+ occurred_at=occurred_at,
+ evaluation_key=evaluation_key,
+ state=PlatformAlertState(state),
+ value=value,
+ error_message=error_message or None,
+ )
+ for occurred_at, evaluation_key, state, value, error_message, _ in rows
+ ),
+ )
+
+
+# Deduplication does not matter here, because two copies of one check are identical rows. The tuple
+# keeps a NULL value, which `argMax` would otherwise skip in favor of an older check's.
+_LAST_CHECKS_SQL = f"""
+SELECT configuration_id, max(occurred_at), argMax(tuple(value), (occurred_at, evaluation_key)).1
+FROM {PLATFORM_ALERT_EVENTS_TABLE}
+WHERE team_id = %(team_id)s
+ AND configuration_id IN %(configuration_ids)s
+ AND grouping_key = ''
+GROUP BY configuration_id
+"""
+
+
+def last_checks(team_id: int, configuration_ids: Collection[UUID]) -> dict[UUID, PlatformLastCheck]:
+ """Each configuration's newest check across its whole history, in one query.
+
+ Only the ungrouped instance is read, as in `history`. A configuration with no check is left out.
+ Raises on a ClickHouse failure, because only the caller knows what it can show instead.
+ """
+ if not configuration_ids:
+ return {}
+ tag_queries(product=Product.PLATFORM_AND_SUPPORT, feature=Feature.ALERTING)
+ # Not `_execute`, for the same reason as `history`.
+ rows = sync_execute(
+ _LAST_CHECKS_SQL,
+ {"team_id": team_id, "configuration_ids": list(configuration_ids)},
+ team_id=team_id,
+ settings=_READ_BUDGET,
+ )
+ return {
+ configuration_id: PlatformLastCheck(checked_at=checked_at, value=value)
+ for configuration_id, checked_at, value in rows
+ }
diff --git a/products/alerts_platform/backend/logic/platform_reads.py b/products/alerts_platform/backend/logic/platform_reads.py
index b4884e4862bc..b95bfad77a21 100644
--- a/products/alerts_platform/backend/logic/platform_reads.py
+++ b/products/alerts_platform/backend/logic/platform_reads.py
@@ -19,6 +19,8 @@ from products.alerts_platform.backend.facade.contracts import (
PlatformAlertRuntime,
PlatformAlertSnapshot,
PlatformCheckEventPage,
+ PlatformCheckPage,
+ PlatformLastCheck,
)
from products.alerts_platform.backend.facade.enums import PlatformAlertOwner, PlatformAlertState
from products.alerts_platform.backend.logic import platform_alert_events
@@ -213,6 +215,26 @@ def history_by_legacy_id(
return {legacy_by_platform_id[platform_id]: history for platform_id, history in histories.items()}
+def last_check_by_legacy_id(
+ *, team_id: int, legacy_configuration_ids: Collection[UUID]
+) -> dict[UUID, PlatformLastCheck]:
+ """The newest check of each named alert the platform owns, keyed by the source's id.
+
+ An alert the platform does not own is left out, as in `runtime_by_legacy_id`.
+ """
+ if not legacy_configuration_ids:
+ return {}
+ legacy_by_platform_id = {
+ platform_id: legacy_id
+ for platform_id, legacy_id in PlatformAlertConfiguration.objects.for_team(team_id)
+ .filter(legacy_configuration_id__in=list(legacy_configuration_ids), owner=PlatformAlertOwner.PLATFORM)
+ .values_list("id", "legacy_configuration_id")
+ if legacy_id is not None
+ }
+ checks = platform_alert_events.last_checks(team_id, legacy_by_platform_id)
+ return {legacy_by_platform_id[platform_id]: check for platform_id, check in checks.items()}
+
+
def notable_checks_by_legacy_id(
*, team_id: int, legacy_configuration_id: UUID, since: datetime, limit: int
) -> PlatformCheckEventPage | None:
@@ -227,3 +249,18 @@ def notable_checks_by_legacy_id(
if platform_id is None:
return None
return platform_alert_events.notable_checks(team_id, platform_id, since=since, limit=limit)
+
+
+def check_page_by_legacy_id(
+ *, team_id: int, legacy_configuration_id: UUID, since: datetime, until: datetime, limit: int, offset: int
+) -> PlatformCheckPage | None:
+ """A page of the checks of one alert the platform owns, or None when the platform does not own it."""
+ platform_id = (
+ PlatformAlertConfiguration.objects.for_team(team_id)
+ .filter(legacy_configuration_id=legacy_configuration_id, owner=PlatformAlertOwner.PLATFORM)
+ .values_list("id", flat=True)
+ .first()
+ )
+ if platform_id is None:
+ return None
+ return platform_alert_events.check_page(team_id, platform_id, since=since, until=until, limit=limit, offset=offset)