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.

fix(eng-analytics): bound DORA coverage by window end (#103877)

Raúl Negrón-Otero85ca6a073de7
diff --git a/products/engineering_analytics/backend/logic/queries/dora.py b/products/engineering_analytics/backend/logic/queries/dora.py
index 976e6fbca145..0a962e76770d 100644
--- a/products/engineering_analytics/backend/logic/queries/dora.py
+++ b/products/engineering_analytics/backend/logic/queries/dora.py
@@ -262,8 +262,8 @@ _LEAD_TIME_INNER = """
"""
# The two one-row CROSS JOIN halves put attribution coverage on the same round trip: how many
-# PRs merged in the window at all (same bot/draft/team recipe), and how many of those an
-# in-scope deployment attributed: the honest denominator behind unattributed_merged_pr_share.
+# PRs merged in the window at all (same bot/draft/team recipe), and how many an in-scope
+# deployment attributed by the report horizon: the honest denominator behind unattributed_merged_pr_share.
_LEAD_TIME_HEADLINE_SELECT = f"""
SELECT
lead.deployed_cur,
@@ -285,7 +285,7 @@ _LEAD_TIME_HEADLINE_SELECT = f"""
FROM ({_LEAD_TIME_INNER})
) AS lead
CROSS JOIN (
- SELECT countIf(__CUR_MERGED__) AS attributed_cur FROM deployed_prs
+ SELECT countIf(__CUR_ATTRIBUTED__) AS attributed_cur FROM deployed_prs
) AS attributed
CROSS JOIN (
SELECT countIf(__CUR_MERGED__) AS merged_cur
@@ -658,10 +658,12 @@ def _query_lead_time(
windows = window_pair_predicates("deployed_at", date_to=scan.date_to)
merged_window = window_pair_predicates("merged_at", date_to=scan.date_to)
+ attributed_window = f"{merged_window.current} {scan.date_to_filter('deployed_at')}".strip()
headline_sql = f"WITH {attribution_ctes} " + (
_LEAD_TIME_HEADLINE_SELECT.replace("__CUR_DEPLOYED__", windows.current)
.replace("__PREV_DEPLOYED__", windows.previous)
.replace("__CUR_MERGED__", merged_window.current)
+ .replace("__CUR_ATTRIBUTED__", attributed_window)
.replace("__PR_SOURCE__", scan.curated.pr_source())
.replace("__TEAM_FILTER__", team_filter)
)
diff --git a/products/engineering_analytics/backend/tests/test_dora.py b/products/engineering_analytics/backend/tests/test_dora.py
index f22cabdb656a..b8620a4a9596 100644
--- a/products/engineering_analytics/backend/tests/test_dora.py
+++ b/products/engineering_analytics/backend/tests/test_dora.py
@@ -138,6 +138,7 @@ class TestDoraQuery(ClickhouseTestMixin, BaseTest):
_deployment_row(4, "sha-d", "staging", "2026-01-12 08:30:00", production=False),
_deployment_row(5, "sha-e", "prod", "2026-01-05 09:30:00", production=True),
_deployment_row(6, "sha-f", "prod", "2026-01-14 09:30:00", production=True),
+ _deployment_row(7, "sha-g", "prod", "2026-01-21 09:30:00", production=True),
],
status_rows=[
_status_row(11, 1, "in_progress", "prod", "2026-01-12 09:31:00"),
@@ -146,6 +147,7 @@ class TestDoraQuery(ClickhouseTestMixin, BaseTest):
_status_row(31, 3, "success", "prod", "2026-01-13 12:00:00"),
_status_row(41, 4, "success", "staging", "2026-01-12 09:00:00"),
_status_row(51, 5, "success", "prod", "2026-01-05 10:00:00"),
+ _status_row(71, 7, "success", "prod", "2026-01-21 10:00:00"),
],
pr_rows=[
# alice heads d1 and merges 2h before its success; bob heads d3, 2.5h before its.
@@ -173,8 +175,18 @@ class TestDoraQuery(ClickhouseTestMixin, BaseTest):
),
# Bot merge in the same slot as PR 1: must not move the lead-time figures.
_pr_row(3, "dependabot[bot]", "closed", 0, "2026-01-11 08:00:00", merged_at="2026-01-12 08:00:00"),
- # Merged but never deployed in the window: not part of the deployed population.
- _pr_row(4, "alice", "closed", 0, "2026-01-19 08:00:00", merged_at="2026-01-19 23:00:00"),
+ # Merged in the window but deployed after it: not yet attributed at the horizon.
+ _pr_row(
+ 4,
+ "alice",
+ "closed",
+ 0,
+ "2026-01-19 08:00:00",
+ merged_at="2026-01-19 23:00:00",
+ merge_commit_sha="sha-g" if merge_shas_available else None,
+ base_ref="main",
+ default_branch="main",
+ ),
# Previous window: deployed by d5 (which it heads), backing the _prev twins.
_pr_row(
5,
@@ -208,6 +220,7 @@ class TestDoraQuery(ClickhouseTestMixin, BaseTest):
(102, "sha-a", 2 if merge_shas_available else 1, "2026-01-12 08:02:00"),
(103, "sha-c", 2, "2026-01-13 09:31:00"),
(104, "sha-e", 5, "2026-01-05 08:01:00"),
+ (105, "sha-g", 4, "2026-01-21 08:01:00"),
]
],
member_rows=member_rows,
@@ -248,8 +261,8 @@ class TestDoraQuery(ClickhouseTestMixin, BaseTest):
assert result.median_open_to_deploy_seconds == 100800.0
assert result.median_open_to_deploy_seconds_prev == 14400.0
assert result.merged_pr_count == 4 # PRs 1, 2, 4, 6; the bot merge is excluded
- assert result.unattributed_merged_pr_share == 0.25 # PR 4 merged Jan 19, never deployed
- assert result.latest_deploy_status_at == datetime(2026, 1, 13, 12, 0, tzinfo=UTC)
+ assert result.unattributed_merged_pr_share == 0.25 # PR 4 deploys after the report horizon
+ assert result.latest_deploy_status_at == datetime(2026, 1, 21, 10, 0, tzinfo=UTC)
assert result.series_granularity == "day"
frequency = {bucket.bucket_start: bucket.deployment_count for bucket in result.deployment_frequency_series}