: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(alerts): copy snooze when backfilling platform alerts (#105827)
Jon McCallumb48eeecabd1b
diff --git a/docs/internal/alerts-platform-temporal.md b/docs/internal/alerts-platform-temporal.mdindex 27fee4c0cb0b..a0898bd88e8c 100644--- a/docs/internal/alerts-platform-temporal.md+++ b/docs/internal/alerts-platform-temporal.md@@ -305,6 +305,7 @@ python manage.py backfill_platform_alert_configurationsPass `--team-id` to copy one team's configurations only.It is a seed, not a sync: the logs product keeps the control plane, and a later change to a logs alert reaches these tables only on the next run.A second run updates rather than duplicates, because `legacy_configuration_id` carries the row each copy came from.+Each run also copies the logs alert's snooze onto its platform alert row, so a snoozed alert stays silent, and an alert unsnoozed since the last run is unsnoozed here too.## Postgres connectivity probediff --git a/products/alerts/backend/facade/contracts.py b/products/alerts/backend/facade/contracts.pyindex ff288df52b3d..a06a3a6a7ae4 100644--- a/products/alerts/backend/facade/contracts.py+++ b/products/alerts/backend/facade/contracts.py@@ -135,6 +135,7 @@ class PlatformAlertUpsert:cooldown_minutes: intschedule_restriction: dict[str, Any] | Nonenext_check_at: datetime | None+ snooze_until: datetime | None@frozendiff --git a/products/alerts/backend/logic/platform_lifecycle.py b/products/alerts/backend/logic/platform_lifecycle.pyindex 3b49bf5f0e6b..39f2d125e929 100644--- a/products/alerts/backend/logic/platform_lifecycle.py+++ b/products/alerts/backend/logic/platform_lifecycle.py@@ -9,6 +9,7 @@ from datetime import datetimefrom django.db import transactionfrom django.db.models import Q+from django.utils import timezonefrom products.alerts.backend.facade.contracts import PlatformAlertCheck, PlatformAlertOutcome, PlatformAlertUpsertfrom products.alerts.backend.facade.scheduling import (@@ -168,23 +169,32 @@ def upsert_configuration(upsert: PlatformAlertUpsert) -> bool:Keyed on the row it came from, so a second run updates rather than duplicates."""- _, created = PlatformAlertConfiguration.objects.unscoped().update_or_create(- legacy_configuration_id=upsert.legacy_configuration_id,- defaults={- "team_id": upsert.team_id,- "name": upsert.name,- "enabled": upsert.enabled,- "source_kind": upsert.source_kind.value,- "source_config": upsert.source_config,- "threshold_count": upsert.threshold_count,- "threshold_operator": upsert.threshold_operator,- "window_minutes": upsert.window_minutes,- "check_interval_minutes": upsert.check_interval_minutes,- "evaluation_periods": upsert.evaluation_periods,- "datapoints_to_alarm": upsert.datapoints_to_alarm,- "cooldown_minutes": upsert.cooldown_minutes,- "schedule_restriction": upsert.schedule_restriction,- "next_check_at": upsert.next_check_at,- },- )+ with transaction.atomic():+ configuration, created = PlatformAlertConfiguration.objects.unscoped().update_or_create(+ legacy_configuration_id=upsert.legacy_configuration_id,+ defaults={+ "team_id": upsert.team_id,+ "name": upsert.name,+ "enabled": upsert.enabled,+ "source_kind": upsert.source_kind.value,+ "source_config": upsert.source_config,+ "threshold_count": upsert.threshold_count,+ "threshold_operator": upsert.threshold_operator,+ "window_minutes": upsert.window_minutes,+ "check_interval_minutes": upsert.check_interval_minutes,+ "evaluation_periods": upsert.evaluation_periods,+ "datapoints_to_alarm": upsert.datapoints_to_alarm,+ "cooldown_minutes": upsert.cooldown_minutes,+ "schedule_restriction": upsert.schedule_restriction,+ "next_check_at": upsert.next_check_at,+ },+ )+ alert = _alerts_for_write(upsert.team_id, [configuration])[str(configuration.id)]+ # Logs-style evaluation honors `snooze_until` only while the state is SNOOZED.+ if upsert.snooze_until is not None and upsert.snooze_until > timezone.now():+ alert.state = PlatformAlert.State.SNOOZED+ elif alert.state == PlatformAlert.State.SNOOZED:+ alert.state = PlatformAlert.State.NOT_FIRING+ alert.snooze_until = upsert.snooze_until+ alert.save(update_fields=["state", "snooze_until"])return createddiff --git a/products/alerts/backend/test/test_platform_lifecycle.py b/products/alerts/backend/test/test_platform_lifecycle.pyindex a9e76ed594b8..d4fc7685b3e3 100644--- a/products/alerts/backend/test/test_platform_lifecycle.py+++ b/products/alerts/backend/test/test_platform_lifecycle.py@@ -1,12 +1,14 @@from datetime import UTC, datetime, timedelta+from uuid import uuid4+import time_machinefrom posthog.test.base import APIBaseTestfrom posthog.models.scoping import team_scope-from products.alerts.backend.facade.contracts import PlatformAlertOutcome, SourceKind-from products.alerts.backend.facade.platform_alerts import due_checks, record_outcomes-from products.alerts.backend.models import PlatformAlertConfiguration+from products.alerts.backend.facade.contracts import PlatformAlertOutcome, PlatformAlertUpsert, SourceKind+from products.alerts.backend.facade.platform_alerts import due_checks, record_outcomes, upsert_configuration+from products.alerts.backend.models import PlatformAlert, PlatformAlertConfigurationclass TestPlatformAlertLifecycle(APIBaseTest):@@ -57,3 +59,50 @@ class TestPlatformAlertLifecycle(APIBaseTest):with team_scope(self.team.id):self.configuration.refresh_from_db()assert self.configuration.next_check_at == after_first++ def test_a_copied_snooze_reaches_the_check_and_a_later_unsnooze_clears_it(self) -> None:+ legacy_id = uuid4()+ snoozed_until = self.cutoff + timedelta(hours=2)++ def copy(snooze_until: datetime | None) -> None:+ upsert_configuration(+ PlatformAlertUpsert(+ legacy_configuration_id=legacy_id,+ team_id=self.team.id,+ name="Snoozed alert",+ enabled=True,+ source_kind=SourceKind.LOGS,+ source_config={},+ threshold_count=1,+ threshold_operator="above",+ window_minutes=5,+ check_interval_minutes=5,+ evaluation_periods=1,+ datapoints_to_alarm=1,+ cooldown_minutes=0,+ schedule_restriction=None,+ next_check_at=self.cutoff - timedelta(minutes=1),+ snooze_until=snooze_until,+ )+ )++ def snooze_seen_by_check() -> tuple[str, datetime | None]:+ (check,) = [+ c+ for c in due_checks(self.team.id, SourceKind.LOGS.value, self.slot, self.cutoff)+ if c.legacy_configuration_id == legacy_id+ ]+ return check.state, check.snooze_until++ with time_machine.travel(self.cutoff, tick=False):+ copy(snoozed_until)+ assert snooze_seen_by_check() == ("snoozed", snoozed_until)++ copy(None)+ assert snooze_seen_by_check() == ("not_firing", None)++ with team_scope(self.team.id):+ PlatformAlert.objects.filter(configuration__legacy_configuration_id=legacy_id).update(state="firing")+ expired = self.cutoff - timedelta(hours=1)+ copy(expired)+ assert snooze_seen_by_check() == ("firing", expired)diff --git a/products/logs/backend/platform_alert_backfill.py b/products/logs/backend/platform_alert_backfill.pyindex d5db9792b615..a2e0fcbea136 100644--- a/products/logs/backend/platform_alert_backfill.py+++ b/products/logs/backend/platform_alert_backfill.py@@ -50,6 +50,7 @@ def backfill_platform_alert_configurations(*, team_id: int | None = None) -> Baccooldown_minutes=configuration.cooldown_minutes,schedule_restriction=configuration.schedule_restriction,next_check_at=configuration.next_check_at,+ snooze_until=configuration.snooze_until,))if was_created: