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.

chore(experiments): evaluate the exposures precompute team gates once per query (#128240)

Rodrigo Iloro301a83b09719
diff --git a/products/experiments/backend/hogql_queries/exposures_precompute.py b/products/experiments/backend/hogql_queries/exposures_precompute.py
index 2a9ceaa11058..ce7834737d5a 100644
--- a/products/experiments/backend/hogql_queries/exposures_precompute.py
+++ b/products/experiments/backend/hogql_queries/exposures_precompute.py
@@ -7,6 +7,7 @@ composes them with the build limits and runs the build.
"""
from datetime import datetime
+from functools import cached_property
from typing import Any, Optional
from rest_framework.exceptions import ValidationError
@@ -32,7 +33,7 @@ from products.experiments.backend.models.experiment import Experiment
from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig
-@frozen
+@frozen(slots=False) # _ineligibility_reason is a cached_property, which needs an instance __dict__
class ExposuresPrecompute:
"""The exposures precompute decisions and builds for one experiment query."""
@@ -80,6 +81,11 @@ class ExposuresPrecompute:
def ineligibility_reason(self) -> Optional[PrecomputeSkipReason]:
"""Why precompute was not used, for the query-performance UI. None when it was attempted."""
+ return self._ineligibility_reason
+
+ @cached_property
+ def _ineligibility_reason(self) -> Optional[PrecomputeSkipReason]:
+ # The team gates can run a cohort query, and both the build decision and the query tag read this reason.
if self.precomputation_mode == PrecomputationMode.PRECOMPUTED:
return None
if self.precomputation_mode == PrecomputationMode.DIRECT:
diff --git a/products/experiments/backend/hogql_queries/test/test_precomputation_gate.py b/products/experiments/backend/hogql_queries/test/test_precomputation_gate.py
index 1414c44b2df1..d631ee5ddcd9 100644
--- a/products/experiments/backend/hogql_queries/test/test_precomputation_gate.py
+++ b/products/experiments/backend/hogql_queries/test/test_precomputation_gate.py
@@ -1,6 +1,7 @@
from datetime import UTC, datetime, timedelta
import time_machine
+from unittest.mock import patch
from django.test import override_settings
@@ -22,6 +23,8 @@ from products.experiments.backend.hogql_queries.precompute_policy import (
from products.experiments.backend.hogql_queries.test.experiment_query_runner.base import ExperimentQueryRunnerBaseTest
from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig
+_POLICY_MODULE = "products.experiments.backend.hogql_queries.precompute_policy"
+
class TestPrecomputationDurationGate:
@parameterized.expand(
@@ -107,11 +110,16 @@ class TestUncalculatedCohortGate(ExperimentQueryRunnerBaseTest):
metric = ExperimentMeanMetric(source=EventsNode(event="purchase"))
query = ExperimentQuery(experiment_id=experiment.id, kind="ExperimentQuery", metric=metric)
+
runner = ExperimentQueryRunner(query=query, team=self.team)
+ with patch(f"{_POLICY_MODULE}.has_uncalculated_cohorts", wraps=has_uncalculated_cohorts) as cohort_check:
+ runner.calculate()
+ assert cohort_check.call_count == 1
assert runner._exposures_precompute_applicable() is False
assert runner._precompute_skip_reason() == "cohort_not_calculated"
Cohort.objects.filter(pk=cohort.pk).update(version=1, is_calculating=False)
+ runner = ExperimentQueryRunner(query=query, team=self.team)
assert runner._exposures_precompute_applicable() is True
assert runner._precompute_skip_reason() is None