: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
refactor(data-warehouse): drop managed warehouse compute activation (#74248)
Anna Szelld1b5b0e7d347
diff --git a/posthog/models/product_intent/product_intent.py b/posthog/models/product_intent/product_intent.pyindex c44eee0831d2..18d91eaff98c 100644--- a/posthog/models/product_intent/product_intent.py+++ b/posthog/models/product_intent/product_intent.py@@ -1,5 +1,5 @@from collections.abc import Callable-from datetime import UTC, datetime, timedelta+from datetime import UTC, datetimefrom typing import Any, Optionalfrom django.core.cache import cache@@ -58,48 +58,6 @@ is created won't have tracking events sent for them. Unless you want to solve thmake activation metrics require multiple things to happen."""-# Activation threshold for the managed warehouse: a warehouse that is provisioned but never-# queried still emits small idle compute heartbeats, so we require meaningful compute before-# counting the team as activated. Provisional value drawn from PostHog's own dogfood warehouse-# (the only one with data today); retune once real customer warehouses report usage.-MANAGED_WAREHOUSE_ACTIVATION_CPU_SECONDS = 100_000-MANAGED_WAREHOUSE_ACTIVATION_WINDOW_DAYS = 30-# Throttle the (ClickHouse-backed) managed warehouse activation check to at most once per hour-# per team, since register() runs it synchronously on every intent PATCH. Activation is sticky-# and also re-evaluated by the daily calculate_product_activation task, so an hour of latency-# on first activation is harmless.-MANAGED_WAREHOUSE_ACTIVATION_CHECK_TTL_SECONDS = 60 * 60---def get_managed_warehouse_compute_usage(team_id: int, start: datetime, end: datetime) -> float:- # Sums cpu_seconds from the `managed warehouse compute usage` heartbeat (emitted by the- # external duckgres service into the owning project) over the [start, end) window.- #- # Trust note: this is a public capture event and carries no non-public provenance signal- # today (its `token` property is the public project token), so a customer could forge it to- # inflate their own activation metric. Blast radius is limited to that team's activation- # state — no cross-tenant, billing, or security impact. Hardening path: have duckgres attach- # an internal shared secret and validate it here, mirroring the DECIDE_BILLING_ANALYTICS_TOKEN- # check in posthog/tasks/usage_report.py (a duckgres-side change, out of scope for this PR).- #- # Local import: this module loads during django.setup() (via posthog.models), so keep the- # ClickHouse client off that path.- from posthog.clickhouse.client import sync_execute # noqa: PLC0415-- rows = sync_execute(- """- SELECT sum(JSONExtractFloat(properties, 'cpu_seconds'))- FROM events- WHERE team_id = %(team_id)s- AND event = 'managed warehouse compute usage'- AND timestamp >= %(start)s- AND timestamp < %(end)s- """,- {"team_id": team_id, "start": start, "end": end},- team_id=team_id,- )- return (rows and rows[0][0]) or 0.0-class ProductIntentSerializer(serializers.Serializer):"""@@ -274,38 +232,6 @@ class ProductIntent(UUIDTModel, RootTeamMixin):# At least one workflow needs to be active (not just drafted)return HogFlow.objects.filter(team=self.team, status=HogFlow.State.ACTIVE).exists()- def has_activated_managed_warehouse(self) -> bool:- # Provisioning is the intent; genuine usage is compute crossing the threshold within- # 30 days of the intent (mirrors has_activated_data_warehouse's window).- window_end = self.created_at + timedelta(days=MANAGED_WAREHOUSE_ACTIVATION_WINDOW_DAYS)-- # Unlike the other (Postgres-backed) checks, this one reads ClickHouse. Gate the query- # behind cheap Postgres checks so it only fires when it could change the answer:- # 1. once the 30-day window has closed, activation is permanently impossible, and- # 2. a team that never emitted the compute-usage heartbeat cannot have any usage.- if datetime.now(tz=UTC) > window_end:- return False- if not EventDefinition.objects.filter(team=self.team, name="managed warehouse compute usage").exists():- return False-- # register() calls check_and_update_activation() synchronously on every intent PATCH, so- # without this an authenticated member could drive an unbounded ClickHouse query rate by- # re-hitting add_product_intent. Debounce the query per team via a short cache TTL; on a- # miss we skip and report not-activated for now — the daily calculate_product_activation- # task (and the next call after the TTL) will run the real check. Fail open on cache errors.- debounce_key = f"managed_warehouse_activation_checked:{self.team_id}"- try:- should_query = cache.add(debounce_key, "1", timeout=MANAGED_WAREHOUSE_ACTIVATION_CHECK_TTL_SECONDS)- except Exception as e:- logger.warning("managed_warehouse_activation_debounce_cache_failure", team_id=self.team_id, exc_info=True)- capture_exception(e)- should_query = True- if not should_query:- return False-- usage = get_managed_warehouse_compute_usage(self.team_id, self.created_at, window_end)- return usage >= MANAGED_WAREHOUSE_ACTIVATION_CPU_SECONDS-def check_and_update_activation(self, skip_reporting: bool = False) -> bool:# If the intent is already activated, we don't need to check againif self.activated_at:@@ -415,7 +341,6 @@ ACTIVATION_CHECKS: dict[str, Callable[[ProductIntent], bool]] = {"llm_analytics": ProductIntent.has_activated_llm_analytics,"mcp_analytics": ProductIntent.has_activated_mcp_analytics,"workflows": ProductIntent.has_activated_workflows,- "managed_warehouse": ProductIntent.has_activated_managed_warehouse,}ACTIVATION_CHECK_PRODUCT_KEYS: frozenset[str] = frozenset(ACTIVATION_CHECKS)diff --git a/posthog/models/test/test_product_intent.py b/posthog/models/test/test_product_intent.pyindex af7d9542b9cc..79d3d9d7f1e3 100644--- a/posthog/models/test/test_product_intent.py+++ b/posthog/models/test/test_product_intent.py@@ -13,7 +13,6 @@ from posthog.schema import ProductIntentContext, ProductKeyfrom posthog.models.file_system.user_product_list import UserProductListfrom posthog.models.product_intent.product_intent import (- MANAGED_WAREHOUSE_ACTIVATION_CPU_SECONDS,ProductIntent,_fetch_product_intents,_team_product_intents_cache_key,@@ -40,9 +39,6 @@ class TestProductIntent(BaseTest):# Joining the org seeds the fixed default product set; these tests assert on# product-intent-driven rows, so start from a clean slate.UserProductList.objects.filter(user=self.user, team=self.team).delete()- # The managed warehouse activation check debounces via the cache; clear it so the- # per-team key from one test can't suppress the ClickHouse check in the next.- cache.clear()self.product_intent = ProductIntent.objects.create(team=self.team, product_type=ProductKey.DATA_WAREHOUSE)def test_str_representation(self):@@ -380,12 +376,11 @@ class TestProductIntent(BaseTest):)@freeze_time("2024-01-01T12:00:00Z")- @patch("posthog.models.product_intent.product_intent.get_managed_warehouse_compute_usage", return_value=0.0)@patch("posthog.event_usage.report_user_action")- def test_register_managed_warehouse_intent(self, mock_report_user_action, mock_compute_usage):+ def test_register_managed_warehouse_intent(self, mock_report_user_action):# Managed warehouse is a distinct product from data_warehouse imports. Provisioning is the- # intent; activation requires compute usage above a threshold within 30 days, which cannot- # be true at intent time (usage only accrues after provisioning), so it never auto-activates here.+ # intent; there is no activation criterion yet (it depends on a per-org usage signal that+ # isn't available in production), so registering intent never auto-activates.ProductIntent.register(team=self.team,product_type=ProductKey.MANAGED_WAREHOUSE,@@ -414,62 +409,6 @@ class TestProductIntent(BaseTest):team=self.team,)- @parameterized.expand(- [- ("zero", 0.0, False),- ("below_threshold", float(MANAGED_WAREHOUSE_ACTIVATION_CPU_SECONDS - 1), False),- ("at_threshold", float(MANAGED_WAREHOUSE_ACTIVATION_CPU_SECONDS), True),- ("above_threshold", float(MANAGED_WAREHOUSE_ACTIVATION_CPU_SECONDS * 2), True),- ]- )- @patch("posthog.models.product_intent.product_intent.get_managed_warehouse_compute_usage")- def test_has_activated_managed_warehouse(self, _name, usage, expected, mock_compute_usage):- mock_compute_usage.return_value = usage- EventDefinition.objects.create(team=self.team, name="managed warehouse compute usage")- intent = ProductIntent.objects.create(team=self.team, product_type=ProductKey.MANAGED_WAREHOUSE)- assert intent.has_activated_managed_warehouse() is expected-- @freeze_time("2024-01-01T12:00:00Z")- @patch("posthog.models.product_intent.product_intent.get_managed_warehouse_compute_usage", return_value=0.0)- def test_managed_warehouse_activation_uses_30_day_window(self, mock_compute_usage):- # Usage is only counted within 30 days of the intent, so the compute lookup must be- # bounded to [created_at, created_at + 30 days).- EventDefinition.objects.create(team=self.team, name="managed warehouse compute usage")- intent = ProductIntent.objects.create(team=self.team, product_type=ProductKey.MANAGED_WAREHOUSE)- assert intent.has_activated_managed_warehouse() is False- mock_compute_usage.assert_called_once_with(- self.team.id,- datetime(2024, 1, 1, 12, 0, 0, tzinfo=UTC),- datetime(2024, 1, 31, 12, 0, 0, tzinfo=UTC),- )-- @patch("posthog.models.product_intent.product_intent.get_managed_warehouse_compute_usage")- def test_managed_warehouse_activation_skips_clickhouse_without_usage_event(self, mock_compute_usage):- # A team that never emitted the compute-usage heartbeat must not trigger a ClickHouse query.- intent = ProductIntent.objects.create(team=self.team, product_type=ProductKey.MANAGED_WAREHOUSE)- assert intent.has_activated_managed_warehouse() is False- mock_compute_usage.assert_not_called()-- @patch("posthog.models.product_intent.product_intent.get_managed_warehouse_compute_usage")- def test_managed_warehouse_activation_skips_clickhouse_after_window(self, mock_compute_usage):- # Once the 30-day window has closed, activation is impossible, so no ClickHouse query runs.- EventDefinition.objects.create(team=self.team, name="managed warehouse compute usage")- with freeze_time("2024-01-01T12:00:00Z"):- intent = ProductIntent.objects.create(team=self.team, product_type=ProductKey.MANAGED_WAREHOUSE)- with freeze_time("2024-03-01T12:00:00Z"):- assert intent.has_activated_managed_warehouse() is False- mock_compute_usage.assert_not_called()-- @patch("posthog.models.product_intent.product_intent.get_managed_warehouse_compute_usage", return_value=0.0)- def test_managed_warehouse_activation_query_is_debounced(self, mock_compute_usage):- # register() runs this check synchronously on every intent PATCH, so repeated calls must- # not each hit ClickHouse — the per-team cache TTL collapses them to one query.- EventDefinition.objects.create(team=self.team, name="managed warehouse compute usage")- intent = ProductIntent.objects.create(team=self.team, product_type=ProductKey.MANAGED_WAREHOUSE)- assert intent.has_activated_managed_warehouse() is False- assert intent.has_activated_managed_warehouse() is False- mock_compute_usage.assert_called_once()-def test_has_activated_product_analytics_with_all_criteria(self):self.product_intent.product_type = ProductKey.PRODUCT_ANALYTICSself.product_intent.save()