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.

feat(warehouse-suggestions): record realized savings on accepted suggestions (#122199)

Thiago Salvatoree810894f05ed
diff --git a/products/warehouse_suggestions/backend/facade/api.py b/products/warehouse_suggestions/backend/facade/api.py
index 071db93be185..aa7521a208fb 100644
--- a/products/warehouse_suggestions/backend/facade/api.py
+++ b/products/warehouse_suggestions/backend/facade/api.py
@@ -239,6 +239,8 @@ def _to_contract(row: WarehouseSuggestion, access: SubjectAccess, payload: Sugge
dismissal_note=row.dismissal_note,
created_asset=row.created_asset,
asset_outcome=WarehouseSuggestionAssetOutcome(row.asset_outcome) if row.asset_outcome else None,
+ realized_seconds_saved_per_month=row.realized_seconds_saved_per_month,
+ realized_bytes_saved_per_month=row.realized_bytes_saved_per_month,
can_act=access.can_act_on(row),
)
diff --git a/products/warehouse_suggestions/backend/facade/contracts.py b/products/warehouse_suggestions/backend/facade/contracts.py
index 8bd2785dbf53..6a062ad5f5d4 100644
--- a/products/warehouse_suggestions/backend/facade/contracts.py
+++ b/products/warehouse_suggestions/backend/facade/contracts.py
@@ -148,6 +148,8 @@ class Suggestion:
dismissal_note: str | None
created_asset: dict[str, Any] | None
asset_outcome: WarehouseSuggestionAssetOutcome | None
+ realized_seconds_saved_per_month: float | None
+ realized_bytes_saved_per_month: float | None
can_act: bool
diff --git a/products/warehouse_suggestions/backend/logic/candidates/base.py b/products/warehouse_suggestions/backend/logic/candidates/base.py
index ecc0cabb6874..812c28f50f0d 100644
--- a/products/warehouse_suggestions/backend/logic/candidates/base.py
+++ b/products/warehouse_suggestions/backend/logic/candidates/base.py
@@ -73,6 +73,37 @@ def inlining_could_deny_a_reader(team_id: int, saved_query_id: UUID) -> bool:
)
+@frozen
+class QueryCost:
+ seconds: float
+ read_bytes: float
+
+
+@frozen
+class RealizedSavings:
+ seconds: float
+ read_bytes: float
+
+
+def monthly_cost(context: "CandidateContext", duration_ms: int, read_bytes: int) -> QueryCost:
+ return QueryCost(
+ seconds=duration_ms / MILLISECONDS_PER_SECOND * context.month_scale,
+ read_bytes=read_bytes * context.month_scale,
+ )
+
+
+def stored_cost(score_inputs: Mapping[str, float], seconds_key: str, bytes_key: str) -> QueryCost | None:
+ if seconds_key not in score_inputs or bytes_key not in score_inputs:
+ return None
+ return QueryCost(seconds=score_inputs[seconds_key], read_bytes=score_inputs[bytes_key])
+
+
+def savings_between(cost_without: QueryCost, cost_now: QueryCost) -> RealizedSavings:
+ return RealizedSavings(
+ seconds=cost_without.seconds - cost_now.seconds, read_bytes=cost_without.read_bytes - cost_now.read_bytes
+ )
+
+
@frozen
class CandidateContext:
team_id: int
@@ -140,6 +171,11 @@ class Candidate(ABC):
self, context: CandidateContext, subject: Subject, accepted_at: datetime | None
) -> WarehouseSuggestionAssetOutcome: ...
+ def realized_savings(
+ self, context: CandidateContext, subject: Subject, score_inputs: Mapping[str, float]
+ ) -> RealizedSavings | None:
+ return None
+
def draft(
self,
context: CandidateContext,
diff --git a/products/warehouse_suggestions/backend/logic/candidates/materialize.py b/products/warehouse_suggestions/backend/logic/candidates/materialize.py
index da220557dd30..9b8da6b6ca2e 100644
--- a/products/warehouse_suggestions/backend/logic/candidates/materialize.py
+++ b/products/warehouse_suggestions/backend/logic/candidates/materialize.py
@@ -1,4 +1,4 @@
-from collections.abc import Iterable
+from collections.abc import Iterable, Mapping
from dataclasses import replace
from datetime import datetime, timedelta
from functools import cached_property
@@ -30,9 +30,24 @@ from ...facade.enums import (
WarehouseSuggestionSubjectKind,
)
from ..reads import Subject, SubjectReads
-from .base import DAYS_PER_MONTH, MILLISECONDS_PER_SECOND, Candidate, CandidateContext, CandidateResult, Rejection
+from .base import (
+ DAYS_PER_MONTH,
+ MILLISECONDS_PER_SECOND,
+ Candidate,
+ CandidateContext,
+ CandidateResult,
+ QueryCost,
+ RealizedSavings,
+ Rejection,
+ monthly_cost,
+ savings_between,
+ stored_cost,
+)
SECONDS_PER_MONTH = DAYS_PER_MONTH * 24 * 60 * 60
+READ_SECONDS_PER_MONTH = "read_seconds_per_month"
+READ_BYTES_PER_MONTH = "read_bytes_per_month"
+HUMAN_READS_PER_MONTH = "human_reads_per_month"
@frozen
@@ -46,12 +61,6 @@ class SavingsEstimate:
score: float
-@frozen
-class RefreshCost:
- seconds: float
- read_bytes: float
-
-
@frozen
class ClassifiedSources:
live: tuple[SourceRef, ...]
@@ -110,8 +119,9 @@ 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,
+ READ_SECONDS_PER_MONTH: outcome.estimate.read_seconds,
+ READ_BYTES_PER_MONTH: outcome.estimate.read_bytes,
+ HUMAN_READS_PER_MONTH: reads.human_reads * context.month_scale,
"refresh_interval_seconds": outcome.estimate.interval.total_seconds(),
},
)
@@ -129,6 +139,25 @@ class MaterializeCandidate(Candidate):
return WarehouseSuggestionAssetOutcome.LIVE
return WarehouseSuggestionAssetOutcome.UNUSED
+ def realized_savings(
+ self, context: CandidateContext, subject: Subject, score_inputs: Mapping[str, float]
+ ) -> RealizedSavings | None:
+ saved_query = context.inventory.saved_queries.get(subject.id)
+ refreshes = context.reads.refreshes.get(subject.id)
+ if saved_query is None or not saved_query.materializes or refreshes is None or refreshes.runs == 0:
+ return None
+ reads = context.reads.reads_of(subject)
+ reads_per_month = (reads.human_reads if reads else 0) * context.month_scale
+ cost_without = _read_cost_before_at(score_inputs, reads_per_month)
+ if cost_without is None:
+ return None
+ cost_now = monthly_cost(
+ context,
+ duration_ms=(reads.human_duration_ms if reads else 0) + refreshes.duration_ms,
+ read_bytes=(reads.human_read_bytes if reads else 0) + refreshes.read_bytes,
+ )
+ return savings_between(cost_without, cost_now)
+
def is_resolved(self, context: CandidateContext, subject: Subject) -> bool:
saved_query = context.inventory.saved_queries.get(subject.id)
return saved_query is None or saved_query.materializes
@@ -246,20 +275,20 @@ def _with_refresh_mode(
)
-def refresh_cost(context: CandidateContext, saved_query_id: UUID, reads: SubjectReads) -> RefreshCost:
+def refresh_cost(context: CandidateContext, saved_query_id: UUID, reads: SubjectReads) -> QueryCost:
refreshes = context.reads.refreshes.get(saved_query_id)
if refreshes is not None and refreshes.runs > 0:
- return RefreshCost(
+ return QueryCost(
seconds=refreshes.duration_ms / MILLISECONDS_PER_SECOND / refreshes.runs,
read_bytes=refreshes.read_bytes / refreshes.runs,
)
- return RefreshCost(
+ return QueryCost(
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
+ context: CandidateContext, reads: SubjectReads, interval: timedelta, refresh: QueryCost
) -> SavingsEstimate:
rules = context.rules.materialize
scale = context.month_scale
@@ -285,6 +314,15 @@ def estimate_savings(
)
+def _read_cost_before_at(score_inputs: Mapping[str, float], reads_per_month: float) -> QueryCost | None:
+ cost_before = stored_cost(score_inputs, READ_SECONDS_PER_MONTH, READ_BYTES_PER_MONTH)
+ reads_before = score_inputs.get(HUMAN_READS_PER_MONTH)
+ if cost_before is None or not reads_before:
+ return None
+ volume = reads_per_month / reads_before
+ return QueryCost(seconds=cost_before.seconds * volume, read_bytes=cost_before.read_bytes * volume)
+
+
def _classify_sources(
context: CandidateContext, saved_query_id: UUID, frequency: SavedQueryFrequencyBounds
) -> ClassifiedSources:
diff --git a/products/warehouse_suggestions/backend/logic/candidates/unmaterialize.py b/products/warehouse_suggestions/backend/logic/candidates/unmaterialize.py
index ca62eb8c58e8..e31fb4335350 100644
--- a/products/warehouse_suggestions/backend/logic/candidates/unmaterialize.py
+++ b/products/warehouse_suggestions/backend/logic/candidates/unmaterialize.py
@@ -30,13 +30,20 @@ from .base import (
Candidate,
CandidateContext,
CandidateResult,
+ QueryCost,
+ RealizedSavings,
Rejection,
inlining_could_deny_a_reader,
+ monthly_cost,
+ savings_between,
+ stored_cost,
)
INCREMENTAL_REJECTION = "refreshes incrementally, so one refresh does not show what a full read costs"
CALLS_DECISION_FUNCTIONS_REJECTION = "calls jev() or decide(), which would run a model on every read"
READ_TIME_QUANTILE = 0.9
+REFRESH_SECONDS_PER_MONTH = "refresh_seconds_per_month"
+REFRESH_BYTES_PER_MONTH = "refresh_bytes_per_month"
@frozen
@@ -83,9 +90,9 @@ class UnmaterializeCandidate(Candidate):
proposal.payload,
score=proposal.score,
score_inputs={
- "refresh_seconds_per_month": proposal.payload.refresh_seconds_per_month,
+ REFRESH_SECONDS_PER_MONTH: proposal.payload.refresh_seconds_per_month,
"live_seconds_per_month": proposal.payload.live_seconds_per_month,
- "refresh_bytes_per_month": proposal.payload.refresh_bytes_per_month,
+ REFRESH_BYTES_PER_MONTH: proposal.payload.refresh_bytes_per_month,
"live_bytes_per_month": proposal.payload.live_bytes_per_month,
},
)
@@ -102,6 +109,17 @@ class UnmaterializeCandidate(Candidate):
return WarehouseSuggestionAssetOutcome.UNUSED
return WarehouseSuggestionAssetOutcome.LIVE
+ def realized_savings(
+ self, context: CandidateContext, subject: Subject, score_inputs: Mapping[str, float]
+ ) -> RealizedSavings | None:
+ saved_query = context.inventory.saved_queries.get(subject.id)
+ if saved_query is None or saved_query.materializes:
+ return None
+ refresh_cost_before = stored_cost(score_inputs, REFRESH_SECONDS_PER_MONTH, REFRESH_BYTES_PER_MONTH)
+ if refresh_cost_before is None:
+ return None
+ return savings_between(refresh_cost_before, _monthly_read_cost(context, subject))
+
def is_resolved(self, context: CandidateContext, subject: Subject) -> bool:
saved_query = context.inventory.saved_queries.get(subject.id)
return saved_query is None or not saved_query.materializes
@@ -226,5 +244,16 @@ def _read_time_quantile_ms(runs: Sequence[RefreshRun]) -> int:
return durations[math.ceil(READ_TIME_QUANTILE * len(durations)) - 1]
+def _monthly_read_cost(context: CandidateContext, subject: Subject) -> QueryCost:
+ reads = context.reads.reads_of(subject)
+ if reads is None:
+ return QueryCost(seconds=0.0, read_bytes=0.0)
+ return monthly_cost(
+ context,
+ duration_ms=reads.human_duration_ms + reads.background_duration_ms,
+ read_bytes=reads.human_read_bytes + reads.background_read_bytes,
+ )
+
+
def _subject(saved_query: SavedQueryDefinition) -> Subject:
return Subject(kind=WarehouseSuggestionSubjectKind.SAVED_QUERY, id=saved_query.id)
diff --git a/products/warehouse_suggestions/backend/logic/job.py b/products/warehouse_suggestions/backend/logic/job.py
index d48f8e347410..ddf4f05e25b7 100644
--- a/products/warehouse_suggestions/backend/logic/job.py
+++ b/products/warehouse_suggestions/backend/logic/job.py
@@ -21,6 +21,8 @@ from .lifecycle import FLIP_KINDS, LifecycleResult, apply_run
from .reads import ReadWindow, RollupDays, TeamReads, read_rollup_days, read_team_reads
from .rules import RULES, EligibilityRules, Rules
+MAINTAINED_STATUSES = (WarehouseSuggestionStatus.PROPOSED, WarehouseSuggestionStatus.ACCEPTED)
+
class TeamRunStatus(StrEnum):
DISABLED = "disabled"
@@ -110,14 +112,19 @@ def _recent_flip_accepts(team_id: int, rules: Rules, now: datetime) -> dict[Ware
return {kind: frozenset(ids) for kind, ids in subject_ids.items()}
-def _has_suggestions_to_maintain(team_id: int) -> bool:
- return (
- WarehouseSuggestion.objects.for_team(team_id)
- .filter(status__in=[WarehouseSuggestionStatus.PROPOSED, WarehouseSuggestionStatus.ACCEPTED])
- .exists()
+def team_ids_with_suggestions_to_maintain() -> set[int]:
+ return set(
+ WarehouseSuggestion.objects.unscoped()
+ .filter(status__in=MAINTAINED_STATUSES)
+ .values_list("team_id", flat=True)
+ .distinct()
)
+def _has_suggestions_to_maintain(team_id: int) -> bool:
+ return WarehouseSuggestion.objects.for_team(team_id).filter(status__in=MAINTAINED_STATUSES).exists()
+
+
def _locked_config(team_id: int) -> WarehouseSuggestionTeamConfig:
return WarehouseSuggestionTeamConfig.objects.select_for_update().get(team_id=team_id)
diff --git a/products/warehouse_suggestions/backend/logic/lifecycle.py b/products/warehouse_suggestions/backend/logic/lifecycle.py
index 50390fb925ec..370dd1424c3b 100644
--- a/products/warehouse_suggestions/backend/logic/lifecycle.py
+++ b/products/warehouse_suggestions/backend/logic/lifecycle.py
@@ -240,16 +240,41 @@ def _reconcile_assets(context: CandidateContext) -> int:
suggestions = WarehouseSuggestion.objects.for_team(context.team_id)
changed = 0
for row in suggestions.filter(status=WarehouseSuggestionStatus.ACCEPTED).only(
- "id", "kind", "subject_kind", "subject_id", "reviewed_at", "asset_outcome"
+ "id",
+ "kind",
+ "subject_kind",
+ "subject_id",
+ "reviewed_at",
+ "score_inputs",
+ "asset_outcome",
+ "realized_seconds_saved_per_month",
+ "realized_bytes_saved_per_month",
):
- outcome = CANDIDATES[WarehouseSuggestionKind(row.kind)].asset_outcome(
- context, _subject(row), accepted_at=row.reviewed_at
- )
- if outcome != row.asset_outcome:
- changed += suggestions.filter(id=row.id).update(asset_outcome=outcome)
+ updates = _asset_updates(context, row)
+ if any(getattr(row, field) != value for field, value in updates.items()):
+ changed += suggestions.filter(id=row.id).update(**updates)
return changed
+def _asset_updates(context: CandidateContext, row: WarehouseSuggestion) -> dict[str, object]:
+ candidate = CANDIDATES[WarehouseSuggestionKind(row.kind)]
+ subject = _subject(row)
+ savings = (
+ candidate.realized_savings(context, subject, row.score_inputs)
+ if _window_starts_after(context, row.reviewed_at)
+ else None
+ )
+ return {
+ "asset_outcome": candidate.asset_outcome(context, subject, accepted_at=row.reviewed_at),
+ "realized_seconds_saved_per_month": savings.seconds if savings else None,
+ "realized_bytes_saved_per_month": savings.read_bytes if savings else None,
+ }
+
+
+def _window_starts_after(context: CandidateContext, accepted_at: datetime | None) -> bool:
+ return accepted_at is not None and accepted_at < context.reads.window.starts_at
+
+
def _move_all(
rows: Iterable[WarehouseSuggestion], team_id: int, status: WarehouseSuggestionStatus
) -> list[WarehouseSuggestion]:
diff --git a/products/warehouse_suggestions/backend/logic/reads.py b/products/warehouse_suggestions/backend/logic/reads.py
index 2d342603d15a..5f6f53bf4ef5 100644
--- a/products/warehouse_suggestions/backend/logic/reads.py
+++ b/products/warehouse_suggestions/backend/logic/reads.py
@@ -65,6 +65,8 @@ class SubjectReads:
human_days: int
background_requests: int
background_reads: int
+ background_duration_ms: int
+ background_read_bytes: int
human_reads: int
human_duration_ms: int
human_read_bytes: int
@@ -220,6 +222,8 @@ SELECT
uniqIf(day, is_human),
uniqMergeIf(requests, NOT is_human),
sumIf(read_count, NOT is_human),
+ sumIf(duration_ms_sum, NOT is_human),
+ sumIf(read_bytes_sum, NOT is_human),
sumIf(read_count, is_human),
sumIf(duration_ms_sum, is_human),
sumIf(read_bytes_sum, is_human),
@@ -305,15 +309,17 @@ def _subject_reads(
human_days=row[4],
background_requests=row[5],
background_reads=row[6],
- human_reads=row[7],
- human_duration_ms=row[8],
- human_read_bytes=row[9],
- alone_reads=row[10],
- alone_duration_ms_median=float(row[11]),
- alone_read_bytes_median=float(row[12]),
- alone_duration_ms_p90=float(row[13]),
- alone_read_bytes_p90=float(row[14]),
- last_read_at=row[15],
+ background_duration_ms=row[7],
+ background_read_bytes=row[8],
+ human_reads=row[9],
+ human_duration_ms=row[10],
+ human_read_bytes=row[11],
+ alone_reads=row[12],
+ alone_duration_ms_median=float(row[13]),
+ alone_read_bytes_median=float(row[14]),
+ alone_duration_ms_p90=float(row[15]),
+ alone_read_bytes_p90=float(row[16]),
+ last_read_at=row[17],
requests_by_surface=surface_counts.get(subject, {}),
)
return reads
diff --git a/products/warehouse_suggestions/backend/logic/suggestions.py b/products/warehouse_suggestions/backend/logic/suggestions.py
index 92731ec44fea..26a379839924 100644
--- a/products/warehouse_suggestions/backend/logic/suggestions.py
+++ b/products/warehouse_suggestions/backend/logic/suggestions.py
@@ -87,6 +87,8 @@ def _clear_review(suggestion: WarehouseSuggestion) -> None:
suggestion.dismissal_reason = None
suggestion.dismissal_note = None
suggestion.dismissed_at_score = None
+ suggestion.realized_seconds_saved_per_month = None
+ suggestion.realized_bytes_saved_per_month = None
def _record_review(suggestion: WarehouseSuggestion, user_id: int | None) -> None:
diff --git a/products/warehouse_suggestions/backend/migrations/0004_realized_savings.py b/products/warehouse_suggestions/backend/migrations/0004_realized_savings.py
new file mode 100644
index 000000000000..9051de067545
--- /dev/null
+++ b/products/warehouse_suggestions/backend/migrations/0004_realized_savings.py
@@ -0,0 +1,22 @@
+# Generated by Django 5.2.18 on 2026-10-22 16:14
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("warehouse_suggestions", "0003_warehouse_suggestion_unmaterialize_kind"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="warehousesuggestion",
+ name="realized_bytes_saved_per_month",
+ field=models.FloatField(blank=True, null=True),
+ ),
+ migrations.AddField(
+ model_name="warehousesuggestion",
+ name="realized_seconds_saved_per_month",
+ field=models.FloatField(blank=True, null=True),
+ ),
+ ]
diff --git a/products/warehouse_suggestions/backend/migrations/max_migration.txt b/products/warehouse_suggestions/backend/migrations/max_migration.txt
index 6ab01f61df99..92987d6c880e 100644
--- a/products/warehouse_suggestions/backend/migrations/max_migration.txt
+++ b/products/warehouse_suggestions/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0003_warehouse_suggestion_unmaterialize_kind
+0004_realized_savings
diff --git a/products/warehouse_suggestions/backend/models.py b/products/warehouse_suggestions/backend/models.py
index a16d62542ed7..b4fa8a3a5bad 100644
--- a/products/warehouse_suggestions/backend/models.py
+++ b/products/warehouse_suggestions/backend/models.py
@@ -46,6 +46,8 @@ class WarehouseSuggestion(ModelActivityMixin, TeamScopedRootMixin, UUIDModel):
asset_outcome = models.CharField(
max_length=16, choices=WarehouseSuggestionAssetOutcome.choices, null=True, blank=True
)
+ realized_seconds_saved_per_month = models.FloatField(null=True, blank=True)
+ realized_bytes_saved_per_month = models.FloatField(null=True, blank=True)
run_id = models.CharField(max_length=255, blank=True)
class Meta:
diff --git a/products/warehouse_suggestions/backend/presentation/serializers.py b/products/warehouse_suggestions/backend/presentation/serializers.py
index c39aa0d119b4..3e918c11ce0b 100644
--- a/products/warehouse_suggestions/backend/presentation/serializers.py
+++ b/products/warehouse_suggestions/backend/presentation/serializers.py
@@ -264,6 +264,19 @@ class WarehouseSuggestionSerializer(DataclassSerializer):
"surfaced_at": {"help_text": "When the suggestion was first shown. Null while it waits for a slot."},
"reviewed_at": {"help_text": "When the suggestion was accepted or dismissed."},
"dismissal_note": {"help_text": "Free-text note left when dismissing."},
+ "realized_seconds_saved_per_month": {
+ "help_text": "Query time an accepted materialize or unmaterialize suggestion saved over the last 30 "
+ "days, scaled to a month: what the view would cost without the change minus what it costs now. For "
+ "materialize, people's reads cost what they cost per read before acceptance, at today's read volume, "
+ "and the cost now is those reads plus refreshes. For unmaterialize, the cost without the change is "
+ "its refreshes, and the cost now is every read. Negative when the change costs more than it saves. "
+ "Null until 30 days have passed since acceptance, when a materialized view has no refresh in that "
+ "time, and for other kinds."
+ },
+ "realized_bytes_saved_per_month": {
+ "help_text": "Bytes read that an accepted materialize or unmaterialize suggestion saved over the "
+ "last 30 days, scaled to a month, measured the same way as realized_seconds_saved_per_month."
+ },
"can_act": {
"help_text": "Whether the caller has edit access to the subject and so may accept, dismiss or resume."
},
diff --git a/products/warehouse_suggestions/backend/temporal/activities.py b/products/warehouse_suggestions/backend/temporal/activities.py
index 85eedf3c726e..e3e715e38b55 100644
--- a/products/warehouse_suggestions/backend/temporal/activities.py
+++ b/products/warehouse_suggestions/backend/temporal/activities.py
@@ -16,7 +16,7 @@ from posthog.temporal.common.logger import get_logger
from posthog.temporal.common.rollout import filter_ids_for_rollout
from ..logic.flags import is_warehouse_suggestions_enabled
-from ..logic.job import TeamRunStatus, run_team
+from ..logic.job import TeamRunStatus, run_team, team_ids_with_suggestions_to_maintain
from ..logic.reads import ReadWindow, read_rollup_days
from ..logic.rules import RULES
from .contracts import BatchOutcome, WarehouseSuggestionsInputs
@@ -35,7 +35,7 @@ def team_batches(inputs: WarehouseSuggestionsInputs) -> list[list[int]]:
if get_kill_switch_level() != KillSwitchLevel.OFF:
LOGGER.info("warehouse_suggestions.skipped_by_kill_switch")
return []
- candidate_ids = inputs.team_ids if inputs.team_ids is not None else _teams_with_reads()
+ candidate_ids = inputs.team_ids if inputs.team_ids is not None else _teams_to_run()
teams = (
Team.objects.filter(
id__in=candidate_ids, is_demo=False, parent_team__isnull=True, organization__for_internal_metrics=False
@@ -67,6 +67,10 @@ def run_batch(team_ids: list[int], run_id: str) -> BatchOutcome:
)
+def _teams_to_run() -> set[int]:
+ return {*_teams_with_reads(), *team_ids_with_suggestions_to_maintain()}
+
+
def _teams_with_reads() -> list[int]:
tag_queries(
product=Product.DATA_MODELING, feature=Feature.WAREHOUSE_SUGGESTIONS, name="warehouse_suggestions_teams"
diff --git a/products/warehouse_suggestions/backend/tests/factories.py b/products/warehouse_suggestions/backend/tests/factories.py
index c8b3a6c4b1be..7de3f9529161 100644
--- a/products/warehouse_suggestions/backend/tests/factories.py
+++ b/products/warehouse_suggestions/backend/tests/factories.py
@@ -38,6 +38,8 @@ def busy_reads(**overrides: Any) -> SubjectReads:
human_days=25,
background_requests=0,
background_reads=0,
+ background_duration_ms=0,
+ background_read_bytes=0,
human_reads=200,
human_duration_ms=200 * 20_000,
human_read_bytes=200 * 10**9,
diff --git a/products/warehouse_suggestions/backend/tests/test_lifecycle.py b/products/warehouse_suggestions/backend/tests/test_lifecycle.py
index 3e9c0cdb7798..13f181e6e10a 100644
--- a/products/warehouse_suggestions/backend/tests/test_lifecycle.py
+++ b/products/warehouse_suggestions/backend/tests/test_lifecycle.py
@@ -1,4 +1,5 @@
from collections import Counter
+from dataclasses import replace
from datetime import UTC, datetime, timedelta
from uuid import UUID, uuid4
@@ -26,6 +27,7 @@ from products.warehouse_suggestions.backend.facade.enums import (
from products.warehouse_suggestions.backend.logic.candidates.base import CandidateContext
from products.warehouse_suggestions.backend.logic.candidates.registry import CANDIDATES
from products.warehouse_suggestions.backend.logic.lifecycle import _pick_in_turns, apply_run
+from products.warehouse_suggestions.backend.logic.reads import SubjectReads, SubjectRefreshes
from products.warehouse_suggestions.backend.logic.suggestions import transition_to
from products.warehouse_suggestions.backend.models import WarehouseSuggestion
from products.warehouse_suggestions.backend.tests.factories import busy_reads, context, team_reads, view, view_subject
@@ -33,6 +35,16 @@ from products.warehouse_suggestions.backend.tests.factories import busy_reads, c
from .test_accept import REPORT
NOW = datetime(2026, 10, 7, 9, tzinfo=UTC)
+READ_COST_BEFORE = {"read_seconds_per_month": 5000.0, "read_bytes_per_month": 5e12, "human_reads_per_month": 200.0}
+REFRESH_COST_BEFORE = {"refresh_seconds_per_month": 5000.0, "refresh_bytes_per_month": 5e12}
+READS_NOW = busy_reads(
+ human_reads=200,
+ human_duration_ms=100_000,
+ human_read_bytes=10**9,
+ background_duration_ms=50_000,
+ background_read_bytes=10**9,
+)
+REFRESHES_NOW = SubjectRefreshes(runs=30, duration_ms=600_000, read_bytes=10**12)
PAYLOADS: dict[WarehouseSuggestionKind, SuggestionPayload] = {
WarehouseSuggestionKind.CERTIFY: CertifyPayload(subject_name="orders"),
WarehouseSuggestionKind.DEPRECATE: DeprecatePayload(
@@ -208,7 +220,9 @@ class TestApplyRun(BaseTest):
apply_run(ctx, self.team, first_run, NOW - timedelta(days=accepted_days_ago), surface=True)
suggestions = WarehouseSuggestion.objects.for_team(self.team.pk)
suggestions.filter(kind=WarehouseSuggestionKind.MATERIALIZE).update(
- status=WarehouseSuggestionStatus.ACCEPTED, reviewed_at=NOW - timedelta(days=accepted_days_ago)
+ status=WarehouseSuggestionStatus.ACCEPTED,
+ reviewed_at=NOW - timedelta(days=accepted_days_ago),
+ realized_seconds_saved_per_month=1.0,
)
suggestions.filter(kind=WarehouseSuggestionKind.UNMATERIALIZE).update(
status=WarehouseSuggestionStatus.ACCEPTED, reviewed_at=NOW - timedelta(days=40)
@@ -217,7 +231,11 @@ class TestApplyRun(BaseTest):
apply_run(ctx, self.team, [materialize], NOW, surface=False)
row = suggestions.get(kind=WarehouseSuggestionKind.MATERIALIZE)
- assert (row.status, row.reproposed_count) == (expected_status, reproposed)
+ assert (row.status, row.reproposed_count, row.realized_seconds_saved_per_month) == (
+ expected_status,
+ reproposed,
+ None,
+ )
@parameterized.expand(
[("expired", WarehouseSuggestionStatus.EXPIRED), ("auto_resolved", WarehouseSuggestionStatus.AUTO_RESOLVED)]
@@ -289,6 +307,121 @@ class TestApplyRun(BaseTest):
assert WarehouseSuggestion.objects.for_team(self.team.pk).get().asset_outcome == expected
+ @parameterized.expand(
+ [
+ (
+ "materialize_measured_after_acceptance",
+ WarehouseSuggestionKind.MATERIALIZE,
+ True,
+ timedelta(days=40),
+ READ_COST_BEFORE,
+ READS_NOW,
+ REFRESHES_NOW,
+ (4300.0, 5e12 - 1e12 - 10**9),
+ ),
+ (
+ "materialize_priced_at_todays_read_volume",
+ WarehouseSuggestionKind.MATERIALIZE,
+ True,
+ timedelta(days=40),
+ READ_COST_BEFORE,
+ replace(READS_NOW, human_reads=2000, human_duration_ms=1_000_000, human_read_bytes=10 * 10**9),
+ REFRESHES_NOW,
+ (48400.0, 50e12 - 1e12 - 10 * 10**9),
+ ),
+ (
+ "materialize_nobody_reads_it",
+ WarehouseSuggestionKind.MATERIALIZE,
+ True,
+ timedelta(days=40),
+ READ_COST_BEFORE,
+ None,
+ REFRESHES_NOW,
+ (-600.0, -1e12),
+ ),
+ (
+ "materialize_no_refresh_rows",
+ WarehouseSuggestionKind.MATERIALIZE,
+ True,
+ timedelta(days=40),
+ READ_COST_BEFORE,
+ READS_NOW,
+ None,
+ (None, None),
+ ),
+ (
+ "materialize_window_holds_reads_from_before",
+ WarehouseSuggestionKind.MATERIALIZE,
+ True,
+ timedelta(days=2),
+ READ_COST_BEFORE,
+ READS_NOW,
+ REFRESHES_NOW,
+ (None, None),
+ ),
+ (
+ "materialize_scored_before_read_cost_was_kept",
+ WarehouseSuggestionKind.MATERIALIZE,
+ True,
+ timedelta(days=40),
+ {},
+ READS_NOW,
+ REFRESHES_NOW,
+ (None, None),
+ ),
+ (
+ "unmaterialize_measured_after_acceptance",
+ WarehouseSuggestionKind.UNMATERIALIZE,
+ False,
+ timedelta(days=40),
+ REFRESH_COST_BEFORE,
+ READS_NOW,
+ None,
+ (4850.0, 5e12 - 2 * 10**9),
+ ),
+ (
+ "unmaterialize_materialized_again",
+ WarehouseSuggestionKind.UNMATERIALIZE,
+ True,
+ timedelta(days=40),
+ REFRESH_COST_BEFORE,
+ READS_NOW,
+ REFRESHES_NOW,
+ (None, None),
+ ),
+ ]
+ )
+ def test_records_the_savings_an_accepted_suggestion_realized(
+ self,
+ _name: str,
+ kind: WarehouseSuggestionKind,
+ materialized_now: bool,
+ accepted_ago: timedelta,
+ score_inputs: dict[str, float],
+ reads_now: SubjectReads | None,
+ refreshes_now: SubjectRefreshes | None,
+ expected: tuple[float | None, float | None],
+ ) -> None:
+ view_id = uuid4()
+ ctx = self._context(view_id)
+ apply_run(ctx, self.team, [self._draft(ctx, kind, view_id, 1)], NOW, surface=False)
+ WarehouseSuggestion.objects.for_team(self.team.pk).update(
+ status=WarehouseSuggestionStatus.ACCEPTED, reviewed_at=NOW - accepted_ago, score_inputs=score_inputs
+ )
+ later = context(
+ team_reads(
+ {view_subject(view_id): reads_now} if reads_now else {},
+ refreshes={view_id: refreshes_now} if refreshes_now else None,
+ ),
+ views=[view(view_id, materializes=materialized_now)],
+ team_id=self.team.pk,
+ )
+
+ apply_run(later, self.team, [], NOW, surface=False)
+
+ row = WarehouseSuggestion.objects.for_team(self.team.pk).get()
+ assert (row.realized_seconds_saved_per_month, row.realized_bytes_saved_per_month) == expected
+
CERTIFY = WarehouseSuggestionKind.CERTIFY
DEPRECATE = WarehouseSuggestionKind.DEPRECATE
diff --git a/products/warehouse_suggestions/backend/tests/test_reads.py b/products/warehouse_suggestions/backend/tests/test_reads.py
index 43244e35e196..51a855a07ae8 100644
--- a/products/warehouse_suggestions/backend/tests/test_reads.py
+++ b/products/warehouse_suggestions/backend/tests/test_reads.py
@@ -83,7 +83,7 @@ class TestReadTeamReads(ClickhouseTestMixin, BaseTest):
RollupRead(subject_id=view, day=TODAY - timedelta(days=2), request_id="b", user_id=2, duration_ms=900),
RollupRead(subject_id=view, day=TODAY - timedelta(days=40), request_id="old", user_id=3),
RollupRead(subject_id=view, day=TODAY, request_id="today", user_id=4),
- RollupRead(subject_id=view, day=YESTERDAY, request_id="warmup", user_id=0),
+ RollupRead(subject_id=view, day=YESTERDAY, request_id="warmup", user_id=0, duration_ms=50),
RollupRead(
subject_id=view,
day=YESTERDAY,
@@ -136,7 +136,7 @@ class TestReadTeamReads(ClickhouseTestMixin, BaseTest):
assert rollup_days == RollupDays(days_with_data=3, recent_days_with_data=3)
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.background_reads == 1
+ assert (view_reads.background_reads, view_reads.background_duration_ms) == (1, 50)
refreshes = reads.refreshes[view]
assert (refreshes.runs, refreshes.duration_ms) == (2, 4090)
assert {run.workflow_id: run.duration_ms for run in reads.refresh_runs[view]} == {"run-1": 1000, "run-2": 3090}
diff --git a/products/warehouse_suggestions/backend/tests/test_temporal.py b/products/warehouse_suggestions/backend/tests/test_temporal.py
index cf4ce6cec358..fd7fb9ea9a74 100644
--- a/products/warehouse_suggestions/backend/tests/test_temporal.py
+++ b/products/warehouse_suggestions/backend/tests/test_temporal.py
@@ -10,8 +10,13 @@ from temporalio.worker import UnsandboxedWorkflowRunner, Worker
from posthog.clickhouse.client.execute import KillSwitchLevel
from posthog.models.team import Team
+from products.warehouse_suggestions.backend.facade.contracts import CertifyPayload
+from products.warehouse_suggestions.backend.facade.enums import WarehouseSuggestionKind, WarehouseSuggestionStatus
+from products.warehouse_suggestions.backend.logic.candidates.registry import CANDIDATES
from products.warehouse_suggestions.backend.logic.job import TeamRunResult, TeamRunStatus
from products.warehouse_suggestions.backend.logic.reads import RollupDays
+from products.warehouse_suggestions.backend.logic.suggestions import upsert_suggestions
+from products.warehouse_suggestions.backend.models import WarehouseSuggestion
from products.warehouse_suggestions.backend.temporal import (
ACTIVITIES as WAREHOUSE_SUGGESTIONS_ACTIVITIES,
WORKFLOWS,
@@ -22,6 +27,7 @@ from products.warehouse_suggestions.backend.temporal.contracts import (
BatchOutcome,
WarehouseSuggestionsInputs,
)
+from products.warehouse_suggestions.backend.tests.factories import context, team_reads, view_subject
ACTIVITIES = "products.warehouse_suggestions.backend.temporal.activities"
@@ -55,6 +61,31 @@ class TestTeamBatches(BaseTest):
assert batches == ([[self.team.pk]] if expect_team else [])
+ def test_picks_an_idle_team_that_still_has_an_accepted_suggestion(self) -> None:
+ dismissed_team = Team.objects.create(organization=self.organization)
+ for team, status in (
+ (self.team, WarehouseSuggestionStatus.ACCEPTED),
+ (dismissed_team, WarehouseSuggestionStatus.DISMISSED),
+ ):
+ draft = CANDIDATES[WarehouseSuggestionKind.CERTIFY].draft(
+ context(team_reads({}), team_id=team.pk),
+ view_subject(uuid.uuid4()),
+ CertifyPayload(subject_name="orders"),
+ score=1,
+ score_inputs={},
+ )
+ upsert_suggestions(team.pk, [draft])
+ WarehouseSuggestion.objects.for_team(team.pk).update(status=status)
+
+ with (
+ patch(f"{ACTIVITIES}.get_kill_switch_level", return_value=KillSwitchLevel.OFF),
+ patch(f"{ACTIVITIES}._teams_with_reads", return_value=[]),
+ patch(f"{ACTIVITIES}.is_warehouse_suggestions_enabled", return_value=True),
+ ):
+ batches = team_batches(WarehouseSuggestionsInputs())
+
+ assert batches == [[self.team.pk]]
+
async def test_a_worker_runs_a_nonempty_batch_without_failing_a_team() -> None:
task_queue = str(uuid.uuid4())
diff --git a/products/warehouse_suggestions/frontend/generated/api.schemas.ts b/products/warehouse_suggestions/frontend/generated/api.schemas.ts
index 5bd3837bb6aa..dfccc10aa11e 100644
--- a/products/warehouse_suggestions/frontend/generated/api.schemas.ts
+++ b/products/warehouse_suggestions/frontend/generated/api.schemas.ts
@@ -302,6 +302,16 @@ export interface WarehouseSuggestionApi {
* @nullable
*/
dismissal_note: string | null
+ /**
+ * Query time an accepted materialize or unmaterialize suggestion saved over the last 30 days, scaled to a month: what the view would cost without the change minus what it costs now. For materialize, people's reads cost what they cost per read before acceptance, at today's read volume, and the cost now is those reads plus refreshes. For unmaterialize, the cost without the change is its refreshes, and the cost now is every read. Negative when the change costs more than it saves. Null until 30 days have passed since acceptance, when a materialized view has no refresh in that time, and for other kinds.
+ * @nullable
+ */
+ realized_seconds_saved_per_month: number | null
+ /**
+ * Bytes read that an accepted materialize or unmaterialize suggestion saved over the last 30 days, scaled to a month, measured the same way as realized_seconds_saved_per_month.
+ * @nullable
+ */
+ realized_bytes_saved_per_month: number | null
/** Whether the caller has edit access to the subject and so may accept, dismiss or resume. */
can_act: boolean
}
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index c87501a84852..0aec156867b8 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -81023,6 +81023,16 @@ export namespace Schemas {
* @nullable
*/
dismissal_note: string | null;
+ /**
+ * Query time an accepted materialize or unmaterialize suggestion saved over the last 30 days, scaled to a month: what the view would cost without the change minus what it costs now. For materialize, people's reads cost what they cost per read before acceptance, at today's read volume, and the cost now is those reads plus refreshes. For unmaterialize, the cost without the change is its refreshes, and the cost now is every read. Negative when the change costs more than it saves. Null until 30 days have passed since acceptance, when a materialized view has no refresh in that time, and for other kinds.
+ * @nullable
+ */
+ realized_seconds_saved_per_month: number | null;
+ /**
+ * Bytes read that an accepted materialize or unmaterialize suggestion saved over the last 30 days, scaled to a month, measured the same way as realized_seconds_saved_per_month.
+ * @nullable
+ */
+ realized_bytes_saved_per_month: number | null;
/** Whether the caller has edit access to the subject and so may accept, dismiss or resume. */
can_act: boolean;
}