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.

refactor(actions): use the experiments facade for action references (#126296)

Anders8fb64e2a8d02
diff --git a/products/actions/backend/api/action.py b/products/actions/backend/api/action.py
index d04f4657edf5..6d9e4bd45d05 100644
--- a/products/actions/backend/api/action.py
+++ b/products/actions/backend/api/action.py
@@ -45,11 +45,15 @@ from products.access_control.backend.presentation.access_control import (
AccessControlViewSetMixin,
UserAccessControlSerializerMixin,
)
+from products.actions.backend.facade.enums import ACTION_REFERENCE_JSONPATH, ACTIONS_ARRAY_JSONPATH
from products.actions.backend.models.action import ACTION_STEP_MATCHING_OPTIONS, Action
from products.actions.backend.models.selector_match_change import ActionSelectorMatchChange
from products.cdp.backend.models.hog_functions.hog_function import HogFunction
from products.cohorts.backend.models.cohort import Cohort
-from products.experiments.backend.models.experiment import Experiment
+from products.experiments.backend.facade.actions import (
+ count_experiments_referencing_actions,
+ experiments_referencing_action,
+)
from products.feature_flags.backend.facade.flags import hides_flag_calls_from_query_builders
from products.product_analytics.backend.facade.models import Insight
@@ -412,25 +416,6 @@ class ActionSelectorMatchChangeSerializer(serializers.Serializer):
)
-_ACTION_JSONPATH = (
- '$.** ? ((@.kind == "ActionsNode" && (@.id == $id || @.id == $id_str))'
- " || (@.actionId == $id || @.actionId == $id_str)"
- ' || (@.type == "actions" && (@.id == $id || @.id == $id_str)))'
-)
-_ACTIONS_ARRAY_JSONPATH = "$.actions[*] ? (@.id == $id || @.id == $id_str)"
-
-_EXPERIMENT_JSON_FIELDS = (
- "metrics",
- "metrics_secondary",
- "filters",
- "parameters",
- "exposure_criteria",
- "stats_config",
- "scheduling_config",
- "variants",
-)
-
-
def find_action_references(action_id: int, team: Team) -> list[dict[str, Any]]:
"""Find resources that reference a given action using database-level jsonb_path queries."""
refs: list[dict[str, Any]] = []
@@ -444,9 +429,9 @@ def find_action_references(action_id: int, team: Team) -> list[dict[str, Any]]:
.extra(
where=[
f"""
- jsonb_path_exists(query, '{_ACTION_JSONPATH}', %s::jsonb)
- OR jsonb_path_exists(filters, '{_ACTION_JSONPATH}', %s::jsonb)
- OR jsonb_path_exists(filters, '{_ACTIONS_ARRAY_JSONPATH}', %s::jsonb)
+ jsonb_path_exists(query, '{ACTION_REFERENCE_JSONPATH}', %s::jsonb)
+ OR jsonb_path_exists(filters, '{ACTION_REFERENCE_JSONPATH}', %s::jsonb)
+ OR jsonb_path_exists(filters, '{ACTIONS_ARRAY_JSONPATH}', %s::jsonb)
"""
],
params=[vars_json] * 3,
@@ -468,19 +453,7 @@ def find_action_references(action_id: int, team: Team) -> list[dict[str, Any]]:
if remaining <= 0:
return refs
- exp_conditions = []
- for field in _EXPERIMENT_JSON_FIELDS:
- exp_conditions.append(f"jsonb_path_exists({field}, '{_ACTION_JSONPATH}', %s::jsonb)")
- exp_conditions.append(f"jsonb_path_exists({field}, '{_ACTIONS_ARRAY_JSONPATH}', %s::jsonb)")
-
- # nosemgrep: python.django.security.audit.query-set-extra.avoid-query-set-extra (parameterized via params)
- experiments = (
- Experiment.objects.filter(team_id=team.pk)
- .exclude(deleted=True)
- .select_related("created_by")
- .extra(where=[" OR ".join(exp_conditions)], params=[vars_json] * len(exp_conditions))
- )
- for exp in experiments[:remaining]:
+ for exp in experiments_referencing_action(team.pk, action_id, limit=remaining):
refs.append(
{
"type": "experiment",
@@ -531,7 +504,7 @@ def find_action_references(action_id: int, team: Team) -> list[dict[str, Any]]:
HogFunction.objects.filter(team_id=team.pk, deleted=False)
.select_related("created_by")
.extra(
- where=[f"jsonb_path_exists(filters, '{_ACTIONS_ARRAY_JSONPATH}', %s::jsonb)"],
+ where=[f"jsonb_path_exists(filters, '{ACTIONS_ARRAY_JSONPATH}', %s::jsonb)"],
params=[vars_json],
)
)
@@ -572,9 +545,9 @@ def count_action_references_bulk(action_ids: list[int], team: Team) -> dict[int,
SELECT 1 FROM {insight_table}
WHERE team_id = %s AND NOT deleted
AND (
- jsonb_path_exists(query, '{_ACTION_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))
- OR jsonb_path_exists(filters, '{_ACTION_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))
- OR jsonb_path_exists(filters, '{_ACTIONS_ARRAY_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))
+ jsonb_path_exists(query, '{ACTION_REFERENCE_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))
+ OR jsonb_path_exists(filters, '{ACTION_REFERENCE_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))
+ OR jsonb_path_exists(filters, '{ACTIONS_ARRAY_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))
)
) AS matched
GROUP BY aid
@@ -584,33 +557,7 @@ def count_action_references_bulk(action_ids: list[int], team: Team) -> dict[int,
for aid, cnt in cursor.fetchall():
counts[aid] += cnt
- exp_conditions = []
- for field in _EXPERIMENT_JSON_FIELDS:
- exp_conditions.append(
- f"jsonb_path_exists({field}, '{_ACTION_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))"
- )
- exp_conditions.append(
- f"jsonb_path_exists({field}, '{_ACTIONS_ARRAY_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))"
- )
- exp_table = Experiment._meta.db_table
- exp_where = " OR ".join(exp_conditions)
-
- # nosemgrep: python.django.security.audit.raw-query.avoid-raw-sql (parameterized via %s)
- with connection.cursor() as cursor:
- cursor.execute(
- f"""
- SELECT aid, count(*) FROM unnest(%s::int[]) AS aid
- CROSS JOIN LATERAL (
- SELECT 1 FROM {exp_table}
- WHERE team_id = %s AND NOT COALESCE(deleted, false)
- AND ({exp_where})
- ) AS matched
- GROUP BY aid
- """,
- [ids_array, team.pk],
- )
- for aid, cnt in cursor.fetchall():
- counts[aid] += cnt
+ counts.update(count_experiments_referencing_actions(team.pk, ids_array))
# nosemgrep: python.django.security.audit.raw-query.avoid-raw-sql (parameterized via %s)
with connection.cursor() as cursor:
@@ -640,7 +587,7 @@ def count_action_references_bulk(action_ids: list[int], team: Team) -> dict[int,
CROSS JOIN LATERAL (
SELECT 1 FROM {hf_table}
WHERE team_id = %s AND NOT deleted
- AND jsonb_path_exists(filters, '{_ACTIONS_ARRAY_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))
+ AND jsonb_path_exists(filters, '{ACTIONS_ARRAY_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))
) AS matched
GROUP BY aid
""",
diff --git a/products/actions/backend/api/test/test_action.py b/products/actions/backend/api/test/test_action.py
index f54089855c53..552f06cbea6f 100644
--- a/products/actions/backend/api/test/test_action.py
+++ b/products/actions/backend/api/test/test_action.py
@@ -16,6 +16,7 @@ from parameterized import parameterized
from rest_framework import status
from rest_framework.exceptions import ValidationError
+from posthog.api.shared import UserBasicSerializer
from posthog.models import Tag, User
from posthog.models.activity_logging.activity_log import ActivityLog
from posthog.test.warehouse_access import WAREHOUSE_ACCESS_CONTROL_FLAG, deny_warehouse_table_to_member
@@ -25,6 +26,7 @@ from products.actions.backend.models.action import Action
from products.actions.backend.models.selector_match_change import ActionSelectorMatchChange
from products.cdp.backend.models.hog_functions.hog_function import HogFunction
from products.cohorts.backend.models.cohort import Cohort
+from products.experiments.backend.facade.testing import create_experiment
from products.feature_flags.backend.facade.enums import FlagEvaluationsMode
from products.feature_flags.backend.models.organization_feature_flags_config import OrganizationFeatureFlagsConfig
from products.product_analytics.backend.facade.models import Insight
@@ -766,16 +768,15 @@ class TestActionApi(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
assert data[0]["name"] == "Legacy insight"
def test_references_returns_experiment_using_action(self):
- from products.experiments.backend.models.experiment import Experiment
from products.feature_flags.backend.models.feature_flag import FeatureFlag
action = Action.objects.create(team=self.team, name="test action", steps_json=[{"event": "$pageview"}])
flag = FeatureFlag.objects.create(team=self.team, key="exp-flag", created_by=self.user)
- exp = Experiment.objects.create(
- team=self.team,
+ experiment_id = create_experiment(
+ team_id=self.team.id,
name="My experiment",
- created_by=self.user,
- feature_flag=flag,
+ created_by_id=self.user.id,
+ feature_flag_id=flag.id,
metrics=[
{
"kind": "ExperimentTrendsQuery",
@@ -792,7 +793,8 @@ class TestActionApi(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
assert len(data) == 1
assert data[0]["type"] == "experiment"
assert data[0]["name"] == "My experiment"
- assert data[0]["url"] == f"/experiments/{exp.id}"
+ assert data[0]["url"] == f"/experiments/{experiment_id}"
+ assert data[0]["created_by"] == UserBasicSerializer(self.user).data
def test_references_returns_cohort_using_action(self):
action = Action.objects.create(team=self.team, name="test action", steps_json=[{"event": "$pageview"}])
@@ -865,16 +867,15 @@ class TestActionApi(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
assert response.json() == []
def test_references_does_not_return_deleted_experiments(self):
- from products.experiments.backend.models.experiment import Experiment
from products.feature_flags.backend.models.feature_flag import FeatureFlag
action = Action.objects.create(team=self.team, name="test action", steps_json=[{"event": "$pageview"}])
flag = FeatureFlag.objects.create(team=self.team, key="deleted-exp-flag", created_by=self.user)
- Experiment.objects.create(
- team=self.team,
+ create_experiment(
+ team_id=self.team.id,
name="Deleted experiment",
- created_by=self.user,
- feature_flag=flag,
+ created_by_id=self.user.id,
+ feature_flag_id=flag.id,
deleted=True,
metrics=[
{
@@ -1018,7 +1019,6 @@ class TestActionApi(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
assert counts[action_without_refs.id] == 0
def test_include_reference_count_counts_across_all_resource_types(self):
- from products.experiments.backend.models.experiment import Experiment
from products.feature_flags.backend.models.feature_flag import FeatureFlag
action = Action.objects.create(team=self.team, name="widely used action", steps_json=[{"event": "$pageview"}])
@@ -1036,11 +1036,11 @@ class TestActionApi(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
)
flag = FeatureFlag.objects.create(team=self.team, key="bulk-count-flag", created_by=self.user)
- Experiment.objects.create(
- team=self.team,
+ create_experiment(
+ team_id=self.team.id,
name="Experiment ref",
- created_by=self.user,
- feature_flag=flag,
+ created_by_id=self.user.id,
+ feature_flag_id=flag.id,
metrics=[
{
"kind": "ExperimentTrendsQuery",
diff --git a/products/actions/backend/facade/enums.py b/products/actions/backend/facade/enums.py
new file mode 100644
index 000000000000..f92e3b7dab56
--- /dev/null
+++ b/products/actions/backend/facade/enums.py
@@ -0,0 +1,12 @@
+"""Exported constants for actions."""
+
+# jsonb_path_exists patterns that find a reference to one action in a stored query or filter. The `id` and `id_str`
+# variables both carry the action id, because a stored reference can hold the id as a number or as a string. Callers
+# put these patterns into SQL text, so they must stay constants.
+ACTION_REFERENCE_JSONPATH = (
+ '$.** ? ((@.kind == "ActionsNode" && (@.id == $id || @.id == $id_str))'
+ " || (@.actionId == $id || @.actionId == $id_str)"
+ ' || (@.type == "actions" && (@.id == $id || @.id == $id_str)))'
+)
+# The same match, for the legacy `actions` array of a filter.
+ACTIONS_ARRAY_JSONPATH = "$.actions[*] ? (@.id == $id || @.id == $id_str)"
diff --git a/products/experiments/backend/experiment_creator.py b/products/experiments/backend/experiment_creator.py
new file mode 100644
index 000000000000..afb918b734ec
--- /dev/null
+++ b/products/experiments/backend/experiment_creator.py
@@ -0,0 +1,17 @@
+from posthog.models.user import User
+
+from products.experiments.backend.facade.contracts import ExperimentCreator
+
+
+def to_experiment_creator(user: User) -> ExperimentCreator:
+ return ExperimentCreator(
+ id=user.id,
+ uuid=user.uuid,
+ distinct_id=user.distinct_id,
+ first_name=user.first_name,
+ last_name=user.last_name,
+ email=user.email,
+ is_email_verified=user.is_email_verified,
+ hedgehog_config=user.hedgehog_config,
+ role_at_organization=user.role_at_organization,
+ )
diff --git a/products/experiments/backend/facade/actions.py b/products/experiments/backend/facade/actions.py
new file mode 100644
index 000000000000..bb6839ec8be6
--- /dev/null
+++ b/products/experiments/backend/facade/actions.py
@@ -0,0 +1,91 @@
+"""Experiments reads for the actions product: the experiments that reference an action."""
+
+import json
+from collections.abc import Sequence
+
+from django.db import connection
+
+from products.actions.backend.facade.enums import ACTION_REFERENCE_JSONPATH, ACTIONS_ARRAY_JSONPATH
+from products.experiments.backend.experiment_creator import to_experiment_creator
+from products.experiments.backend.facade.contracts import ExperimentActionReference
+from products.experiments.backend.models.experiment import Experiment
+
+__all__ = [
+ "count_experiments_referencing_actions",
+ "experiments_referencing_action",
+]
+
+_EXPERIMENT_JSON_FIELDS = (
+ "metrics",
+ "metrics_secondary",
+ "filters",
+ "parameters",
+ "exposure_criteria",
+ "stats_config",
+ "scheduling_config",
+ "variants",
+)
+
+
+def experiments_referencing_action(
+ team_id: int, action_id: int, *, limit: int
+) -> tuple[ExperimentActionReference, ...]:
+ """Up to `limit` experiments of the team that are not deleted and that reference the action in their metrics,
+ filters or configuration."""
+ vars_json = json.dumps({"id": action_id, "id_str": str(action_id)})
+
+ exp_conditions = []
+ for field in _EXPERIMENT_JSON_FIELDS:
+ exp_conditions.append(f"jsonb_path_exists({field}, '{ACTION_REFERENCE_JSONPATH}', %s::jsonb)")
+ exp_conditions.append(f"jsonb_path_exists({field}, '{ACTIONS_ARRAY_JSONPATH}', %s::jsonb)")
+
+ # nosemgrep: python.django.security.audit.query-set-extra.avoid-query-set-extra (parameterized via params)
+ experiments = (
+ Experiment.objects.filter(team_id=team_id)
+ .exclude(deleted=True)
+ .select_related("created_by")
+ .extra(where=[" OR ".join(exp_conditions)], params=[vars_json] * len(exp_conditions))
+ )
+ return tuple(
+ ExperimentActionReference(
+ id=experiment.id,
+ name=experiment.name,
+ created_at=experiment.created_at,
+ created_by=to_experiment_creator(experiment.created_by) if experiment.created_by else None,
+ )
+ for experiment in experiments[:limit]
+ )
+
+
+def count_experiments_referencing_actions(team_id: int, action_ids: Sequence[int]) -> dict[int, int]:
+ """For each action, how many experiments of the team that are not deleted reference it. An action without a
+ reference is absent from the result."""
+ if not action_ids:
+ return {}
+
+ exp_conditions = []
+ for field in _EXPERIMENT_JSON_FIELDS:
+ exp_conditions.append(
+ f"jsonb_path_exists({field}, '{ACTION_REFERENCE_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))"
+ )
+ exp_conditions.append(
+ f"jsonb_path_exists({field}, '{ACTIONS_ARRAY_JSONPATH}', jsonb_build_object('id', aid, 'id_str', aid::text))"
+ )
+ exp_table = Experiment._meta.db_table
+ exp_where = " OR ".join(exp_conditions)
+
+ # nosemgrep: python.django.security.audit.raw-query.avoid-raw-sql (parameterized via %s)
+ with connection.cursor() as cursor:
+ cursor.execute(
+ f"""
+ SELECT aid, count(*) FROM unnest(%s::int[]) AS aid
+ CROSS JOIN LATERAL (
+ SELECT 1 FROM {exp_table}
+ WHERE team_id = %s AND NOT COALESCE(deleted, false)
+ AND ({exp_where})
+ ) AS matched
+ GROUP BY aid
+ """,
+ [list(action_ids), team_id],
+ )
+ return dict(cursor.fetchall())
diff --git a/products/experiments/backend/facade/contracts.py b/products/experiments/backend/facade/contracts.py
index a793696fc58f..0e0b0c2e3432 100644
--- a/products/experiments/backend/facade/contracts.py
+++ b/products/experiments/backend/facade/contracts.py
@@ -15,6 +15,8 @@ from posthog.dataclasses import frozen
from posthog.enums import LabeledStrEnum
if TYPE_CHECKING:
+ from uuid import UUID
+
from posthog.schema import MaxExperimentSummaryContext
# Metrics per section (primary/secondary) included in the AI results summary
@@ -423,6 +425,33 @@ class FeatureFlag:
name: str | None = None
+@frozen
+class ExperimentCreator:
+ """The user who created an experiment, with the fields that core's `UserBasicSerializer` renders. A caller
+ serializes it through that serializer, so the response keeps the `UserBasic` shape."""
+
+ id: int
+ uuid: "UUID"
+ distinct_id: str | None
+ first_name: str
+ last_name: str
+ email: str
+ is_email_verified: bool | None
+ # The stored value. `UserBasicSerializer` shapes it when it renders.
+ hedgehog_config: dict[str, Any] | None
+ role_at_organization: str | None
+
+
+@frozen
+class ExperimentActionReference:
+ """An experiment whose stored metrics, filters or configuration reference an action."""
+
+ id: int
+ name: str
+ created_at: datetime
+ created_by: ExperimentCreator | None
+
+
@dataclass(frozen=True)
class Experiment:
"""Experiment output."""
diff --git a/products/experiments/backend/test/test_facade_actions.py b/products/experiments/backend/test/test_facade_actions.py
new file mode 100644
index 000000000000..44e15840fe8f
--- /dev/null
+++ b/products/experiments/backend/test/test_facade_actions.py
@@ -0,0 +1,48 @@
+from typing import Any
+
+from posthog.test.base import BaseTest
+
+from posthog.models import Team
+
+from products.experiments.backend.experiment_creator import to_experiment_creator
+from products.experiments.backend.facade.actions import (
+ count_experiments_referencing_actions,
+ experiments_referencing_action,
+)
+from products.experiments.backend.models.experiment import Experiment
+from products.feature_flags.backend.models.feature_flag import FeatureFlag
+
+ACTION_ID = 101
+OTHER_ACTION_ID = 102
+
+
+def _action_metric(action_id: int) -> list[dict[str, Any]]:
+ return [{"kind": "ExperimentMetric", "metric_type": "mean", "source": {"kind": "ActionsNode", "id": action_id}}]
+
+
+class TestActionReferencesFacade(BaseTest):
+ def _experiment(self, name: str, *, team: Team | None = None, **fields: Any) -> None:
+ team = team or self.team
+ flag = FeatureFlag.objects.create(team=team, key=f"flag-{name}", created_by=self.user)
+ Experiment.objects.create(team=team, name=name, feature_flag=flag, created_by=self.user, **fields)
+
+ def test_finds_the_live_team_experiments_that_reference_each_action(self) -> None:
+ self._experiment("Metric", metrics=_action_metric(ACTION_ID))
+ self._experiment("Legacy filter", filters={"actions": [{"id": str(ACTION_ID), "type": "actions"}]})
+ self._experiment("Other action", metrics_secondary=_action_metric(OTHER_ACTION_ID))
+ self._experiment("Deleted", deleted=True, metrics=_action_metric(ACTION_ID))
+ self._experiment(
+ "Other team",
+ team=Team.objects.create(organization=self.organization, name="other"),
+ metrics=_action_metric(ACTION_ID),
+ )
+
+ references = experiments_referencing_action(self.team.id, ACTION_ID, limit=10)
+
+ assert sorted(reference.name for reference in references) == ["Legacy filter", "Metric"]
+ assert [reference.created_by for reference in references] == [to_experiment_creator(self.user)] * 2
+ assert len(experiments_referencing_action(self.team.id, ACTION_ID, limit=1)) == 1
+ assert count_experiments_referencing_actions(self.team.id, [ACTION_ID, OTHER_ACTION_ID, 103]) == {
+ ACTION_ID: 2,
+ OTHER_ACTION_ID: 1,
+ }