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(warehouse-suggestions): price refreshes from refresh cost, not read cost (#122166)

Thiago Salvatore80bd4d1cba90
diff --git a/products/warehouse_suggestions/backend/logic/candidates/materialize.py b/products/warehouse_suggestions/backend/logic/candidates/materialize.py
index 8e9649093ffb..a3757eb4de01 100644
--- a/products/warehouse_suggestions/backend/logic/candidates/materialize.py
+++ b/products/warehouse_suggestions/backend/logic/candidates/materialize.py
@@ -38,12 +38,20 @@ SECONDS_PER_MONTH = DAYS_PER_MONTH * 24 * 60 * 60
@frozen
class SavingsEstimate:
interval: timedelta
+ read_seconds: float
+ read_bytes: float
seconds_saved: float
bytes_saved: float
clears_a_floor: bool
score: float
+@frozen
+class RefreshCost:
+ seconds: float
+ read_bytes: float
+
+
@frozen
class ClassifiedSources:
live: tuple[SourceRef, ...]
@@ -102,6 +110,8 @@ class MaterializeCandidate(Candidate):
score_inputs={
"seconds_saved_per_month": outcome.estimate.seconds_saved,
"bytes_saved_per_month": outcome.estimate.bytes_saved,
+ "read_seconds_per_month": outcome.estimate.read_seconds,
+ "read_bytes_per_month": outcome.estimate.read_bytes,
"refresh_interval_seconds": outcome.estimate.interval.total_seconds(),
},
)
@@ -143,10 +153,11 @@ class MaterializeCandidate(Candidate):
intervals = allowed_intervals(context, frequency.bounds.allowed, reads)
if not intervals:
return f"its sources allow no refresh interval up to {context.rules.refresh.max_interval}"
+ refresh = refresh_cost(context, saved_query.id, reads)
estimate = next(
(
estimate
- for estimate in (estimate_savings(context, reads, interval) for interval in intervals)
+ for estimate in (estimate_savings(context, reads, interval, refresh) for interval in intervals)
if estimate.clears_a_floor
),
None,
@@ -233,22 +244,38 @@ def _with_refresh_mode(
)
-def estimate_savings(context: CandidateContext, reads: SubjectReads, interval: timedelta) -> SavingsEstimate:
+def refresh_cost(context: CandidateContext, saved_query_id: UUID, reads: SubjectReads) -> RefreshCost:
+ refreshes = context.reads.refreshes.get(saved_query_id)
+ if refreshes is not None and refreshes.runs > 0:
+ return RefreshCost(
+ seconds=refreshes.duration_ms / MILLISECONDS_PER_SECOND / refreshes.runs,
+ read_bytes=refreshes.read_bytes / refreshes.runs,
+ )
+ return RefreshCost(
+ seconds=reads.alone_duration_ms_p90 / MILLISECONDS_PER_SECOND, read_bytes=reads.alone_read_bytes_p90
+ )
+
+
+def estimate_savings(
+ context: CandidateContext, reads: SubjectReads, interval: timedelta, refresh: RefreshCost
+) -> SavingsEstimate:
rules = context.rules.materialize
scale = context.month_scale
refreshes_per_month = SECONDS_PER_MONTH / interval.total_seconds()
body_seconds = reads.alone_duration_ms_median / MILLISECONDS_PER_SECOND
read_seconds = reads.human_duration_ms / MILLISECONDS_PER_SECOND * scale
read_bytes = reads.human_read_bytes * scale
- seconds_saved = min(read_seconds, reads.human_reads * body_seconds * scale) - refreshes_per_month * body_seconds
+ seconds_saved = min(read_seconds, reads.human_reads * body_seconds * scale) - refreshes_per_month * refresh.seconds
bytes_saved = (
min(read_bytes, reads.human_reads * reads.alone_read_bytes_median * scale)
- - refreshes_per_month * reads.alone_read_bytes_median
+ - refreshes_per_month * refresh.read_bytes
)
clears_time = seconds_saved >= rules.min_seconds_saved and seconds_saved >= rules.min_saved_share * read_seconds
clears_bytes = bytes_saved >= rules.min_bytes_saved and bytes_saved >= rules.min_saved_share * read_bytes
return SavingsEstimate(
interval=interval,
+ read_seconds=read_seconds,
+ read_bytes=read_bytes,
seconds_saved=seconds_saved,
bytes_saved=bytes_saved,
clears_a_floor=clears_time or clears_bytes,
diff --git a/products/warehouse_suggestions/backend/logic/reads.py b/products/warehouse_suggestions/backend/logic/reads.py
index e4de3b37b0b2..59ae3c6a4d79 100644
--- a/products/warehouse_suggestions/backend/logic/reads.py
+++ b/products/warehouse_suggestions/backend/logic/reads.py
@@ -14,6 +14,7 @@ from .rules import Rules, Surface, SurfaceRules, TrafficRules
READS_QUERY_SETTINGS = {"max_execution_time": 30, "max_threads": 2}
FIRST_QUANTILE = 1
+SECOND_QUANTILE = 2
DAYS_FILTER = """day >= %(window_start)s
AND day < %(window_end)s"""
@@ -69,6 +70,8 @@ class SubjectReads:
alone_reads: int
alone_duration_ms_median: float
alone_read_bytes_median: float
+ alone_duration_ms_p90: float
+ alone_read_bytes_p90: float
last_read_at: datetime
requests_by_surface: Mapping[Surface, int]
@@ -212,6 +215,8 @@ SELECT
sumIf(read_count, read_alone),
quantilesMergeIf(0.5, 0.9)(duration_ms_quantiles, read_alone)[{FIRST_QUANTILE}],
quantilesMergeIf(0.5, 0.9)(read_bytes_quantiles, read_alone)[{FIRST_QUANTILE}],
+ quantilesMergeIf(0.5, 0.9)(duration_ms_quantiles, read_alone)[{SECOND_QUANTILE}],
+ quantilesMergeIf(0.5, 0.9)(read_bytes_quantiles, read_alone)[{SECOND_QUANTILE}],
toTimeZone(max(max_event_time), 'UTC')
FROM (
SELECT *, {{human}} AS is_human
@@ -279,7 +284,9 @@ def _subject_reads(
alone_reads=row[9],
alone_duration_ms_median=float(row[10]),
alone_read_bytes_median=float(row[11]),
- last_read_at=row[12],
+ alone_duration_ms_p90=float(row[12]),
+ alone_read_bytes_p90=float(row[13]),
+ last_read_at=row[14],
requests_by_surface=surface_counts.get(subject, {}),
)
return reads
diff --git a/products/warehouse_suggestions/backend/tests/factories.py b/products/warehouse_suggestions/backend/tests/factories.py
index 0061e1f690aa..25f81f03de54 100644
--- a/products/warehouse_suggestions/backend/tests/factories.py
+++ b/products/warehouse_suggestions/backend/tests/factories.py
@@ -42,6 +42,8 @@ def busy_reads(**overrides: Any) -> SubjectReads:
alone_reads=10,
alone_duration_ms_median=10_000.0,
alone_read_bytes_median=float(10**9),
+ alone_duration_ms_p90=10_000.0,
+ alone_read_bytes_p90=float(10**9),
last_read_at=datetime(2026, 10, 6, tzinfo=UTC),
requests_by_surface={Surface.SQL_EDITOR: 120, Surface.DASHBOARD: 80},
)
diff --git a/products/warehouse_suggestions/backend/tests/test_candidates.py b/products/warehouse_suggestions/backend/tests/test_candidates.py
index f8fee1e37bee..e73d6c3283d3 100644
--- a/products/warehouse_suggestions/backend/tests/test_candidates.py
+++ b/products/warehouse_suggestions/backend/tests/test_candidates.py
@@ -23,7 +23,9 @@ from products.warehouse_suggestions.backend.logic.candidates.materialize import
allowed_intervals,
estimate_savings,
interval_basis,
+ refresh_cost,
)
+from products.warehouse_suggestions.backend.logic.reads import SubjectRefreshes
from products.warehouse_suggestions.backend.logic.rules import RULES, Surface
from products.warehouse_suggestions.backend.tests.factories import (
busy_reads,
@@ -155,17 +157,40 @@ class TestMaterializeRules(SimpleTestCase):
assert intervals == expected
- @parameterized.expand([("at_floor", 90, True), ("one_read_short", 89, False)])
- def test_time_saved_floor(self, _name: str, human_reads: int, expect_clear: bool) -> None:
+ @parameterized.expand(
+ [
+ ("at_floor", 90, 10_000.0, None, True),
+ ("one_read_short", 89, 10_000.0, None, False),
+ ("slow_reads_raise_the_refresh_price", 90, 20_000.0, None, False),
+ (
+ "recorded_refreshes_set_the_price",
+ 90,
+ 20_000.0,
+ SubjectRefreshes(runs=3, duration_ms=30_000, read_bytes=3),
+ True,
+ ),
+ ]
+ )
+ def test_time_saved_floor(
+ self,
+ _name: str,
+ human_reads: int,
+ alone_duration_ms_p90: float,
+ refreshes: SubjectRefreshes | None,
+ expect_clear: bool,
+ ) -> None:
reads = busy_reads(
human_reads=human_reads,
human_duration_ms=human_reads * 10_000,
alone_duration_ms_median=10_000.0,
+ alone_duration_ms_p90=alone_duration_ms_p90,
human_read_bytes=human_reads,
alone_read_bytes_median=1.0,
+ alone_read_bytes_p90=1.0,
)
+ ctx = context(team_reads({}, refreshes={VIEW_ID: refreshes} if refreshes else None))
- estimate = estimate_savings(context(team_reads({})), reads, DAY)
+ estimate = estimate_savings(ctx, reads, DAY, refresh_cost(ctx, VIEW_ID, reads))
assert estimate.clears_a_floor is expect_clear
diff --git a/products/warehouse_suggestions/backend/tests/test_reads.py b/products/warehouse_suggestions/backend/tests/test_reads.py
index fcda7847537b..0495e29afc9d 100644
--- a/products/warehouse_suggestions/backend/tests/test_reads.py
+++ b/products/warehouse_suggestions/backend/tests/test_reads.py
@@ -100,7 +100,7 @@ class TestReadTeamReads(ClickhouseTestMixin, BaseTest):
assert (reads.readers, reads.view_readers, reads.view_reads) == (3, 2, 2)
assert (view_reads.human_requests, view_reads.human_users, view_reads.human_days) == (2, 2, 2)
assert (view_reads.human_reads, view_reads.human_duration_ms, view_reads.alone_reads) == (3, 1300, 2)
- assert 100 <= view_reads.alone_duration_ms_median <= 300
+ assert 100 <= view_reads.alone_duration_ms_median < view_reads.alone_duration_ms_p90 <= 300
assert view_reads.last_read_at == datetime.combine(YESTERDAY, time(hour=12), tzinfo=UTC)