: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(signals): key anomaly buckets on the declared bucket start (#126831)
posthog[bot]7490854c3175
diff --git a/products/signals/backend/anomaly_scores/read.py b/products/signals/backend/anomaly_scores/read.pyindex 3d84dd87edd9..0c62b8902c44 100644--- a/products/signals/backend/anomaly_scores/read.py+++ b/products/signals/backend/anomaly_scores/read.py@@ -1,6 +1,7 @@"""Read the `$anomaly_score` events of a project back, as anomaly rates per series and as scored buckets.A bucket can be scored more than once, so every read keeps the newest result of each series and bucket.+A bucket is its declared `$anomaly_bucket_start`. Ingestion can move the event timestamp by some milliseconds.Warm-up rows have a null score and count nowhere. A bucket that was not scored is unknown, never 0.A raw score ranks buckets within one series only, so a ranking across series uses rates, never scores."""@@ -40,12 +41,14 @@ DEFAULT_PRIOR_RATE = 0.05# The intervals the scorer writes. Any member can capture a row with another interval, such as `second`,# and stepping through its buckets over a long range would take hours.SCORED_INTERVALS = frozenset(DEFAULT_PROFILE.intervals)+# The event scan reaches this far past the range, so a row whose timestamp ingestion moved stays in the scan.+TIMESTAMP_DRIFT = datetime.timedelta(hours=1)_LATEST_BUCKETS = """SELECTtoInt(properties.$anomaly_insight_id) AS insight_id,properties.$anomaly_series_key AS series_key,- timestamp AS bucket_start,+ coalesce(toDateTime(properties.$anomaly_bucket_start), timestamp) AS bucket_start,toFloat(properties.$anomaly_score) AS score,toInt(properties.$anomaly_flag) AS flag,toFloat(properties.$anomaly_value) AS value,@@ -60,8 +63,10 @@ SELECTFROM eventsWHERE event = {event}AND properties.$anomaly_source = {source}- AND timestamp >= {since}- AND timestamp <= {until}+ AND timestamp >= {scan_since}+ AND timestamp <= {scan_until}+ AND bucket_start >= {since}+ AND bucket_start <= {until}AND {scope}ORDER BY scored_at DESCLIMIT 1 BY series_key, bucket_start@@ -262,6 +267,8 @@ def _placeholders("source": ast.Constant(value=SOURCE_INSIGHT),"since": ast.Constant(value=since),"until": ast.Constant(value=until),+ "scan_since": ast.Constant(value=since - TIMESTAMP_DRIFT),+ "scan_until": ast.Constant(value=until + TIMESTAMP_DRIFT),"scope": _scope(insight_ids, series_key),}diff --git a/products/signals/backend/test/test_anomaly_search.py b/products/signals/backend/test/test_anomaly_search.pyindex d848cef44a5a..305a80fae0e9 100644--- a/products/signals/backend/test/test_anomaly_search.py+++ b/products/signals/backend/test/test_anomaly_search.py@@ -120,12 +120,13 @@ class TestAnomalySearchAPI(ClickhouseTestMixin, APIBaseTest):interval: str = "day",insight: Insight | None = None,team: Team | None = None,+ timestamp: datetime.datetime | None = None,) -> None:_create_event(team=team or self.team,event="$anomaly_score",distinct_id=SCORER_DISTINCT_ID,- timestamp=bucket,+ timestamp=timestamp or bucket,properties={"$anomaly_score": score,"$anomaly_flag": flag,@@ -222,6 +223,24 @@ class TestAnomalySearchAPI(ClickhouseTestMixin, APIBaseTest):]assert series["signups"]["buckets"][-1]["details"] == {"z": 3.1}+ def test_insight_keys_buckets_on_the_declared_start_not_the_event_timestamp(self) -> None:+ ms = datetime.timedelta(milliseconds=1)+ self._score(_day(19), score=0.99, flag=1, scored_at=_day(20), timestamp=_day(19) - 5 * ms)+ self._score(_day(19), score=0.40, flag=0, scored_at=_day(21), timestamp=_day(19) + 7 * ms)+ self._score(_day(20), score=0.30, flag=0, timestamp=_day(20) + 41 * ms)+ self._score(_day(22), score=0.98, flag=1, timestamp=_day(22) + 4 * ms)++ body = self._get("insight", insight_id=self.insight.id, date_from="2026-10-19").json()++ [series] = body["series"]+ assert (series["scored_buckets"], series["expected_buckets"]) == (3, 4)+ assert [(bucket["bucket_start"], bucket["scored"], bucket["score"]) for bucket in series["buckets"]] == [+ ("2026-10-19T00:00:00+00:00", True, 0.40),+ ("2026-10-20T00:00:00+00:00", True, 0.30),+ ("2026-10-21T00:00:00+00:00", False, None),+ ("2026-10-22T00:00:00+00:00", True, 0.98),+ ]+def test_insight_narrows_to_one_series_and_shows_warmup_on_request(self) -> None:self._score_history()