: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
feat(alerts): pair real-time checks in the insight comparison (#123643)
Jimmy Zhu93e5bdf0c9d2
diff --git a/products/alerts/backend/alert_comparison.py b/products/alerts/backend/alert_comparison.pyindex 1076ec0f6659..e2d8b10d4f9d 100644--- a/products/alerts/backend/alert_comparison.py+++ b/products/alerts/backend/alert_comparison.py@@ -7,8 +7,9 @@ A platform check names its due slot, and the two stacks do not check at the samespreads its alerts over an offset within the period, keyed on its own id for the alert, soproduction's check of a period can come before the platform's slot or after it. A platform checkpairs with production's first check in the same local calendar period: the quarter hour, hour,-day, ISO week or month the slot falls in. A production stack that made no check in that period is-behind.+day, ISO week or month the slot falls in. A real-time check has no such period, and both stacks+step from the same due time, so it pairs with production's first check in the cadence after its+slot. A production stack that made no check in that period is behind.`AlertCheck` rows are deleted after `AlertCheck.RETENTION`, so an older slot cannot be answered."""@@ -48,7 +49,12 @@ from products.alerts_platform.backend.facade.contracts import (SuppressionReason,)from products.alerts_platform.backend.facade.lifecycle import AlertState-from products.alerts_platform.backend.facade.scheduling import is_weekend, local_period_start, next_local_period_start+from products.alerts_platform.backend.facade.scheduling import (+ REAL_TIME_CADENCE_MINUTES,+ is_weekend,+ local_period_start,+ next_local_period_start,+)logger = structlog.get_logger(__name__)@@ -63,6 +69,7 @@ _SUB_DAY_PERIOD_MINUTES: dict[str, int] = {AlertCalculationInterval.EVERY_15_MINUTES: 15,AlertCalculationInterval.HOURLY: 60,}+_REAL_TIME_PERIOD = timedelta(minutes=REAL_TIME_CADENCE_MINUTES)# Calendar periods follow the team's local calendar: a day across a daylight-saving change is 23 or# 25 hours, and a month is as long as it is._CALENDAR_PERIOD: dict[str, relativedelta] = {@@ -276,9 +283,13 @@ def _period_of(slot: datetime, alert: AlertConfiguration) -> _Period | None:"""The local calendar period the slot falls in: its quarter hour, hour, day, ISO week or month.Both schedulers check once per such period, each at an offset of its own, so production's check- of the same period can come before the platform's slot as well as after it.+ of the same period can come before the platform's slot as well as after it. A real-time check is+ due at the same instant on both stacks, so production's check of it comes after the slot, before+ the next one is due."""interval = alert.calculation_interval or ""+ if interval == AlertCalculationInterval.REAL_TIME:+ return _Period(start=slot, end=slot + _REAL_TIME_PERIOD)if interval in _SUB_DAY_PERIOD_MINUTES:minutes = _SUB_DAY_PERIOD_MINUTES[interval]team_timezone = pytz.timezone(alert.team.timezone)diff --git a/products/alerts/backend/test/test_alert_comparison.py b/products/alerts/backend/test/test_alert_comparison.pyindex 43fd6d914526..513b99ff7329 100644--- a/products/alerts/backend/test/test_alert_comparison.py+++ b/products/alerts/backend/test/test_alert_comparison.py@@ -209,6 +209,23 @@ class TestInsightCorrespondence(BaseTest):datetime(2026, 10, 3, 16, 5, tzinfo=UTC),SourceCoverage.BEHIND,),+ (+ "a_real_time_check_of_the_slot",+ AlertCalculationInterval.REAL_TIME,+ "UTC",+ datetime(2026, 3, 4, 9, 52, tzinfo=UTC),+ datetime(2026, 3, 4, 9, 53, 50, tzinfo=UTC),+ SourceCoverage.EVALUATED,+ ),+ # Due at the same instant on both stacks, so a check before the slot belonged to the one before.+ (+ "a_real_time_check_of_the_previous_slot",+ AlertCalculationInterval.REAL_TIME,+ "UTC",+ datetime(2026, 3, 4, 9, 52, tzinfo=UTC),+ datetime(2026, 3, 4, 9, 51, 40, tzinfo=UTC),+ SourceCoverage.BEHIND,+ ),])def test_a_check_pairs_within_the_local_calendar_period_of_its_slot(