: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): skip missed alert intervals (#97702)
Matt Puaa144f3fe9e16
diff --git a/products/alerts/backend/scheduling.py b/products/alerts/backend/scheduling.pyindex 56ea97b085b2..cee8146943b5 100644--- a/products/alerts/backend/scheduling.py+++ b/products/alerts/backend/scheduling.py@@ -1,8 +1,9 @@"""Scheduling math for alert checks.-Sub-daily checks preserve their existing cadence. Daily, weekly, and monthly-checks anchor to calendar instants in the team's local timezone. Quiet hours-and weekend skipping layer local-time restrictions on top of those schedules.+Sub-daily checks preserve their existing cadence and skip missed intervals.+Daily, weekly, and monthly checks anchor to calendar instants in the team's+local timezone. Quiet hours and weekend skipping layer local-time restrictions+on top of those schedules.Pure Python with no Django or model imports. Timezones are passed as IANAnames, quiet-hours windows as parsed tuples.@@ -17,7 +18,6 @@ from typing import Any, castfrom uuid import UUIDimport pytz-from dateutil.relativedelta import relativedeltafrom pytz.exceptions import AmbiguousTimeError, NonExistentTimeErrorfrom pytz.tzinfo import BaseTzInfo@@ -155,21 +155,26 @@ def next_calendar_check_time() -> datetime:"""Nominal next check instant, before quiet-hours snapping.- Sub-daily intervals advance from the previous next_check_at (falling back to- now) so per-alert spread from creation time is preserved. Daily/weekly/monthly- anchor to fixed local instants: 1am tomorrow, 3am next Monday, 4am on the 1st- of next month. Hour-only replacement keeps the minute/second spread.+ Sub-daily intervals keep their cadence from the previous next_check_at. If+ a check is late, the next check skips missed intervals and is after now.+ Daily/weekly/monthly anchor to fixed local instants: 1am tomorrow, 3am next+ Monday, 4am on the 1st of next month. Hour-only replacement keeps the+ minute/second spread."""team_timezone = pytz.timezone(tz_name)local_now = now.astimezone(team_timezone)match interval:- case CalendarInterval.REAL_TIME:- return (next_check_at or now) + relativedelta(minutes=REAL_TIME_CADENCE_MINUTES)- case CalendarInterval.EVERY_15_MINUTES:- return (next_check_at or now) + relativedelta(minutes=EVERY_15_MINUTES_CADENCE_MINUTES)- case CalendarInterval.HOURLY:- return (next_check_at or now) + relativedelta(hours=1)+ case CalendarInterval.REAL_TIME | CalendarInterval.EVERY_15_MINUTES | CalendarInterval.HOURLY:+ interval_delta = {+ CalendarInterval.REAL_TIME: timedelta(minutes=REAL_TIME_CADENCE_MINUTES),+ CalendarInterval.EVERY_15_MINUTES: timedelta(minutes=EVERY_15_MINUTES_CADENCE_MINUTES),+ CalendarInterval.HOURLY: timedelta(hours=1),+ }[interval]+ candidate = (next_check_at or now) + interval_delta+ if candidate <= now:+ candidate += interval_delta * (int((now - candidate) // interval_delta) + 1)+ return candidatecase CalendarInterval.DAILY:return _calendar_anchor_utc(local_now,diff --git a/products/alerts/backend/test/test_scheduling.py b/products/alerts/backend/test/test_scheduling.pyindex 82e78ee2fe3f..de608d419c68 100644--- a/products/alerts/backend/test/test_scheduling.py+++ b/products/alerts/backend/test/test_scheduling.py@@ -126,8 +126,8 @@ class TestValidateAndNormalizeScheduleRestriction:class TestNextCalendarCheckTime:@parameterized.expand([- # Sub-daily intervals advance from the prior next_check_at, preserving per-alert spread- ("real_time_from_prev", CalendarInterval.REAL_TIME, PREV_CHECK, datetime(2026, 3, 18, 11, 49, tzinfo=UTC)),+ # Sub-daily intervals preserve their schedule phase and skip missed evaluations.+ ("real_time_from_prev", CalendarInterval.REAL_TIME, PREV_CHECK, datetime(2026, 3, 18, 12, 1, tzinfo=UTC)),("real_time_first_check", CalendarInterval.REAL_TIME, None, datetime(2026, 3, 18, 12, 2, tzinfo=UTC)),("15min_from_prev",@@ -136,6 +136,12 @@ class TestNextCalendarCheckTime:datetime(2026, 3, 18, 12, 2, tzinfo=UTC),),("hourly_from_prev", CalendarInterval.HOURLY, PREV_CHECK, datetime(2026, 3, 18, 12, 47, tzinfo=UTC)),+ (+ "hourly_skips_backlog",+ CalendarInterval.HOURLY,+ datetime(2026, 3, 18, 5, 47, tzinfo=UTC),+ datetime(2026, 3, 18, 12, 47, tzinfo=UTC),+ ),])def test_sub_daily_advances_from_previous(