: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(experiments): route pulse and reminders reads via the facade (#126264)
Anders4f827cf3acee
diff --git a/products/experiments/backend/facade/contracts.py b/products/experiments/backend/facade/contracts.pyindex 64decbba16d7..e3cd75db6d8e 100644--- a/products/experiments/backend/facade/contracts.py+++ b/products/experiments/backend/facade/contracts.py@@ -561,6 +561,15 @@ class Experiment:updated_at: datetime | None = None+@frozen+class ExperimentRef:+ """The fields that an object access check reads from one experiment."""++ id: int+ team_id: int+ created_by_id: int | None++@frozenclass MetricCalculationKeys:"""The calculation key of each metric on an experiment. Result rows and the stored metricdiff --git a/products/experiments/backend/facade/pulse.py b/products/experiments/backend/facade/pulse.pynew file mode 100644index 000000000000..6c4bf15db7c9--- /dev/null+++ b/products/experiments/backend/facade/pulse.py@@ -0,0 +1,10 @@+"""Experiment reads for Pulse, which links the experiments a brief cites."""++from collections.abc import Collection++from products.experiments.backend.models.experiment import Experiment+++def existing_experiment_ids(*, team_id: int, experiment_ids: Collection[int]) -> set[int]:+ """Which of the given experiment ids the team has. Deleted and archived experiments count."""+ return set(Experiment.objects.filter(team_id=team_id, id__in=experiment_ids).values_list("id", flat=True))diff --git a/products/experiments/backend/facade/reminders.py b/products/experiments/backend/facade/reminders.pynew file mode 100644index 000000000000..2c4653ed2199--- /dev/null+++ b/products/experiments/backend/facade/reminders.py@@ -0,0 +1,15 @@+"""Experiment reads for reminders, which can point a reminder at an experiment."""++from products.experiments.backend.facade.contracts import ExperimentRef+from products.experiments.backend.models.experiment import Experiment+++def get_experiment_ref(*, team_id: int, experiment_id: int) -> ExperimentRef | None:+ """The experiment with this id on this exact team, or None. A deleted experiment is returned too."""+ # `team=` matches this team only. RootTeamQuerySet rewrites `team_id=` to the parent team when the team has one.+ experiment = (+ Experiment.objects.filter(team=team_id, id=experiment_id).only("id", "team_id", "created_by_id").first()+ )+ if experiment is None:+ return None+ return ExperimentRef(id=experiment.id, team_id=experiment.team_id, created_by_id=experiment.created_by_id)diff --git a/products/experiments/backend/test/test_facade_pulse.py b/products/experiments/backend/test/test_facade_pulse.pynew file mode 100644index 000000000000..1daab600e460--- /dev/null+++ b/products/experiments/backend/test/test_facade_pulse.py@@ -0,0 +1,30 @@+from posthog.test.base import BaseTest++from posthog.models.team import Team++from products.experiments.backend.facade import testing+from products.experiments.backend.facade.pulse import existing_experiment_ids+from products.feature_flags.backend.models.feature_flag import FeatureFlag+++class TestExistingExperimentIds(BaseTest):+ def _create_experiment(self, team: Team, key: str, *, deleted: bool = False, archived: bool = False) -> int:+ flag = FeatureFlag.objects.create(team=team, key=key, created_by=self.user)+ return testing.create_experiment(+ team_id=team.id, feature_flag_id=flag.id, name=key, deleted=deleted, archived=archived+ )++ def test_returns_the_listed_ids_the_team_has(self) -> None:+ other_team = Team.objects.create(organization=self.organization, name="Other")+ live = self._create_experiment(self.team, "live")+ archived = self._create_experiment(self.team, "archived", archived=True)+ deleted = self._create_experiment(self.team, "deleted", deleted=True)+ not_listed = self._create_experiment(self.team, "not_listed")+ of_other_team = self._create_experiment(other_team, "other_team")+ unknown_id = max(live, archived, deleted, not_listed, of_other_team) + 1++ found = existing_experiment_ids(+ team_id=self.team.id, experiment_ids=[live, archived, deleted, of_other_team, unknown_id]+ )++ assert found == {live, archived, deleted}diff --git a/products/experiments/backend/test/test_facade_reminders.py b/products/experiments/backend/test/test_facade_reminders.pynew file mode 100644index 000000000000..0a82629990dc--- /dev/null+++ b/products/experiments/backend/test/test_facade_reminders.py@@ -0,0 +1,32 @@+from posthog.test.base import BaseTest++from parameterized import parameterized++from posthog.models.team import Team++from products.experiments.backend.facade import testing+from products.experiments.backend.facade.contracts import ExperimentRef+from products.experiments.backend.facade.reminders import get_experiment_ref+from products.feature_flags.backend.models.feature_flag import FeatureFlag+++class TestGetExperimentRef(BaseTest):+ @parameterized.expand(+ [+ ("live", False, False, True),+ ("deleted", False, True, True),+ ("another_team", True, False, False),+ ]+ )+ def test_finds_an_experiment_of_the_team(self, _name: str, other_team: bool, deleted: bool, found: bool) -> None:+ team = Team.objects.create(organization=self.organization, name="Other") if other_team else self.team+ flag = FeatureFlag.objects.create(team=team, key="experiment-flag", created_by=self.user)+ experiment_id = testing.create_experiment(+ team_id=team.id, feature_flag_id=flag.id, created_by_id=self.user.id, deleted=deleted+ )++ ref = get_experiment_ref(team_id=self.team.id, experiment_id=experiment_id)++ assert ref == (+ ExperimentRef(id=experiment_id, team_id=self.team.id, created_by_id=self.user.id) if found else None+ )diff --git a/products/pulse/backend/generation/persist.py b/products/pulse/backend/generation/persist.pyindex 3fa14a788b3a..b801916e9888 100644--- a/products/pulse/backend/generation/persist.py+++ b/products/pulse/backend/generation/persist.py@@ -9,7 +9,7 @@ import structlogfrom products.alerts.backend.models import AlertConfigurationfrom products.annotations.backend.models.annotation import Annotationfrom products.dashboards.backend.models.dashboard import Dashboard-from products.experiments.backend.models.experiment import Experiment+from products.experiments.backend.facade import pulse as experiments_pulsefrom products.product_analytics.backend.facade.models import Insightfrom products.pulse.backend.generation.schemas import BriefOut, BriefSectionOut, OpportunityOutfrom products.pulse.backend.models import Opportunity, ProductBrief, ResourceLink, build_action@@ -107,14 +107,22 @@ def _build_opportunity()-# A subscription resolves to its id, because its model stays behind the subscriptions facade.-_LinkTarget = Insight | Dashboard | Annotation | Experiment | AlertConfiguration | int+# A subscription or an experiment resolves to its id, because its model stays behind its product's facade.+_LinkTarget = Insight | Dashboard | Annotation | AlertConfiguration | int+++def _numeric_refs(evidence_type: EvidenceType, refs: set[str]) -> list[int]:+ numeric_ids = [int(r) for r in refs if r.isdigit()]+ if non_numeric := refs - {str(i) for i in numeric_ids}:+ # A non-numeric ref cannot match an integer id, so its link stays FK-less.+ logger.warning("pulse_persist_non_numeric_ref", evidence_type=evidence_type, refs=sorted(non_numeric))+ return numeric_idsdef _resolve_link_fks(team_id: int, evidence: list[EvidenceRef]) -> dict[tuple[EvidenceType, str], _LinkTarget]:"""Batch-resolve cited refs, keyed by EvidenceRef.key, to the model instances the ResourceLink- FKs point at, or to the id for a subscription. A ref that resolves to nothing is still linked- (cached columns only)."""+ FKs point at, or to the id for a subscription or an experiment. A ref that resolves to nothing+ is still linked (cached columns only)."""by_type: dict[EvidenceType, set[str]] = {}for ref in evidence:by_type.setdefault(ref.type, set()).add(ref.ref)@@ -136,25 +144,21 @@ def _resolve_link_fks(team_id: int, evidence: list[EvidenceRef]) -> dict[tuple[Efor evidence_type, model in ((EvidenceType.DASHBOARD, Dashboard),(EvidenceType.ANNOTATION, Annotation),- (EvidenceType.EXPERIMENT, Experiment),):refs = by_type.get(evidence_type)if not refs:continue- numeric_ids = [int(r) for r in refs if r.isdigit()]- if non_numeric := refs - {str(i) for i in numeric_ids}:- # These models have integer PKs; a non-numeric ref can't resolve (its link stays FK-less).- logger.warning("pulse_persist_non_numeric_ref", evidence_type=evidence_type, refs=sorted(non_numeric))- for obj in model.objects.filter(team_id=team_id, id__in=numeric_ids):+ for obj in model.objects.filter(team_id=team_id, id__in=_numeric_refs(evidence_type, refs)):resolved[(evidence_type, str(obj.id))] = obj+ experiment_refs = by_type.get(EvidenceType.EXPERIMENT)+ if experiment_refs:+ experiment_ids = _numeric_refs(EvidenceType.EXPERIMENT, experiment_refs)+ for experiment_id in experiments_pulse.existing_experiment_ids(team_id=team_id, experiment_ids=experiment_ids):+ resolved[(EvidenceType.EXPERIMENT, str(experiment_id))] = experiment_idsubscription_refs = by_type.get(EvidenceType.SUBSCRIPTION)if subscription_refs:- numeric_ids = [int(r) for r in subscription_refs if r.isdigit()]- if non_numeric := subscription_refs - {str(i) for i in numeric_ids}:- logger.warning(- "pulse_persist_non_numeric_ref", evidence_type=EvidenceType.SUBSCRIPTION, refs=sorted(non_numeric)- )- for subscription_id in subscription_ids_in_team(team_id=team_id, subscription_ids=numeric_ids):+ subscription_ids = _numeric_refs(EvidenceType.SUBSCRIPTION, subscription_refs)+ for subscription_id in subscription_ids_in_team(team_id=team_id, subscription_ids=subscription_ids):resolved[(EvidenceType.SUBSCRIPTION, str(subscription_id))] = subscription_idreturn resolveddiff --git a/products/pulse/backend/test/test_persist.py b/products/pulse/backend/test/test_persist.pyindex 493a69b0dda6..f59be4389d8d 100644--- a/products/pulse/backend/test/test_persist.py+++ b/products/pulse/backend/test/test_persist.py@@ -7,6 +7,8 @@ from posthog.models.scoping import team_scopefrom posthog.schema_enums import AlertStatefrom products.alerts.backend.models import AlertConfiguration+from products.experiments.backend.facade.testing import create_experiment+from products.feature_flags.backend.models.feature_flag import FeatureFlagfrom products.product_analytics.backend.facade.models import Insightfrom products.pulse.backend.generation.persist import _fingerprint, persist_brief_outputfrom products.pulse.backend.generation.schemas import BriefOut, BriefSectionOut, OpportunityOut@@ -197,15 +199,17 @@ class TestPersistBriefOutput(BaseTest):brief = persist_brief_output(brief=self._brief(), out=out, items=[_item()])assert brief.status == ProductBrief.Status.READY- def test_alert_and_subscription_refs_resolve_to_typed_links(self) -> None:- # Alerts (UUID pk) and subscriptions (int pk) are first-class evidence types with their own- # FKs; a "fix" opportunity citing both must persist links with each FK resolved.+ def test_alert_subscription_and_experiment_refs_resolve_to_typed_links(self) -> None:+ # Alerts (UUID pk), subscriptions and experiments (int pk) are first-class evidence types with+ # their own FKs; a "fix" opportunity citing them must persist links with each FK resolved.with team_scope(self.team.pk, canonical=True):insight = Insight.objects.create(team=self.team, name="Signups")alert = AlertConfiguration.objects.create(team=self.team, insight=insight, name="a", state=AlertState.ERRORED, enabled=True)subscription_id = create_subscription(team_id=self.team.id, title="s")+ flag = FeatureFlag.objects.create(team=self.team, key="experiment-flag", created_by=self.user)+ experiment_id = create_experiment(team_id=self.team.id, feature_flag_id=flag.id)item = SourceItem(source="resource_health",kind=SourceItemKind.HEALTH,@@ -214,6 +218,7 @@ class TestPersistBriefOutput(BaseTest):evidence=[EvidenceRef(type=EvidenceType.ALERT, ref=str(alert.id), label="a", url=""),EvidenceRef(type=EvidenceType.SUBSCRIPTION, ref=str(subscription_id), label="s", url=""),+ EvidenceRef(type=EvidenceType.EXPERIMENT, ref=str(experiment_id), label="e", url=""),],fingerprint_hint="alert:x",)@@ -225,7 +230,7 @@ class TestPersistBriefOutput(BaseTest):title="t",summary="s",suggested_action="a",- evidence_refs=["c1", "c2"],+ evidence_refs=["c1", "c2", "c3"],fingerprint_hint="alert:x",confidence=0.9,)@@ -235,3 +240,4 @@ class TestPersistBriefOutput(BaseTest):links = {link.resource_type: link for link in self._links()}assert links[ResourceType.ALERT].alert_id == alert.idassert links[ResourceType.SUBSCRIPTION].subscription_id == subscription_id+ assert links[ResourceType.EXPERIMENT].experiment_id == experiment_iddiff --git a/products/reminders/backend/api/reminder.py b/products/reminders/backend/api/reminder.pyindex bb18ddd583a2..5486a5f81f1b 100644--- a/products/reminders/backend/api/reminder.py+++ b/products/reminders/backend/api/reminder.py@@ -34,7 +34,7 @@ from posthog.permissions import (from posthog.user_permissions import UserPermissionsfrom products.access_control.backend.facade.user_access_control import UserAccessControl-from products.reminders.backend.constants import MAX_ACTIVE_REMINDERS_PER_USER, RESOURCE_MODELS, RESOURCE_TYPES+from products.reminders.backend.constants import MAX_ACTIVE_REMINDERS_PER_USER, RESOURCE_LOOKUPS, RESOURCE_TYPESfrom products.reminders.backend.models import Reminderfrom products.reminders.backend.scheduling import compute_next_fire_at, exceeds_daily_frequency_cap, resolve_timezone@@ -297,18 +297,21 @@ class ReminderSerializer(serializers.ModelSerializer):raise ValidationError("resource_type and resource_id must be provided together.")if team is None:raise ValidationError("A team must be set to attach a resource to a reminder.")- if resource_type not in RESOURCE_MODELS:+ if resource_type not in RESOURCE_LOOKUPS:raise ValidationError(f"Unknown resource_type: {resource_type}")- model, lookup_field, _ = RESOURCE_MODELS[resource_type]- resource = model._default_manager.filter(team=team, **{lookup_field: resource_id}).first()- if resource is None:- raise ValidationError(f"No {resource_type} with id {resource_id} in this project.")-# Membership in the project is not access to every object in it. Report an object the# caller cannot view as missing, so the message does not confirm that it exists.- user = cast(User, self.context["request"].user)- if not UserAccessControl(user=user, team=team).check_access_level_for_object(resource, "viewer"):+ access = UserAccessControl(user=cast(User, self.context["request"].user), team=team)+ lookup, _ = RESOURCE_LOOKUPS[resource_type]+ if isinstance(lookup, tuple):+ model, lookup_field = lookup+ resource = model._default_manager.filter(team=team, **{lookup_field: resource_id}).first()+ can_view = resource is not None and access.check_access_level_for_object(resource, "viewer")+ else:+ ref = lookup(team.id, resource_id)+ can_view = ref is not None and access.check_access_level_for_ref(ref, "viewer")+ if not can_view:raise ValidationError(f"No {resource_type} with id {resource_id} in this project.")def _validate_active_cap(self) -> None:diff --git a/products/reminders/backend/constants.py b/products/reminders/backend/constants.pyindex 8e0ee05750a0..c2757e8c315c 100644--- a/products/reminders/backend/constants.py+++ b/products/reminders/backend/constants.py@@ -1,30 +1,49 @@+from collections.abc import Callable+from django.apps import appsfrom django.db import modelsfrom posthog.session_recordings.models.session_recording import SessionRecording+from products.access_control.backend.facade.contracts import ObjectAccessReffrom products.dashboards.backend.models.dashboard import Dashboard-from products.experiments.backend.models.experiment import Experiment+from products.experiments.backend.facade import reminders as experiments_remindersfrom products.feature_flags.backend.models.feature_flag import FeatureFlagfrom products.notebooks.backend.models import Notebookfrom products.product_analytics.backend.facade.models import Insightfrom products.surveys.backend.models import Survey-# resource_type -> (model, lookup_field, frontend_path_segment)-# lookup_field is the column used to match resource_id within the team.+FacadeLookup = Callable[[int, str], ObjectAccessRef | None]+++def _find_experiment(team_id: int, resource_id: str) -> ObjectAccessRef | None:+ experiment = experiments_reminders.get_experiment_ref(team_id=team_id, experiment_id=int(resource_id))+ if experiment is None:+ return None+ return ObjectAccessRef(+ resource="experiment",+ id=str(experiment.id),+ team_id=experiment.team_id,+ created_by_id=experiment.created_by_id,+ )+++# resource_type -> (lookup, frontend_path_segment)+# lookup is a model and the column used to match resource_id within the team. A resource whose model stays+# behind its product's facade has a function instead, which returns the resource's access reference.# frontend_path_segment builds the notification deep link: /project/{project_id}/{segment}/{resource_id}-RESOURCE_MODELS: dict[str, tuple[type[models.Model], str, str]] = {- "dashboard": (Dashboard, "pk", "dashboard"),- "insight": (Insight, "short_id", "insights"),- "experiment": (Experiment, "pk", "experiments"),- "feature_flag": (FeatureFlag, "pk", "feature_flags"),- "survey": (Survey, "pk", "surveys"),- "notebook": (Notebook, "short_id", "notebooks"),- "replay": (SessionRecording, "session_id", "replay"),- "error_tracking": (apps.get_model("error_tracking", "ErrorTrackingIssue"), "pk", "error_tracking"),+RESOURCE_LOOKUPS: dict[str, tuple[tuple[type[models.Model], str] | FacadeLookup, str]] = {+ "dashboard": ((Dashboard, "pk"), "dashboard"),+ "insight": ((Insight, "short_id"), "insights"),+ "experiment": (_find_experiment, "experiments"),+ "feature_flag": ((FeatureFlag, "pk"), "feature_flags"),+ "survey": ((Survey, "pk"), "surveys"),+ "notebook": ((Notebook, "short_id"), "notebooks"),+ "replay": ((SessionRecording, "session_id"), "replay"),+ "error_tracking": ((apps.get_model("error_tracking", "ErrorTrackingIssue"), "pk"), "error_tracking"),}-RESOURCE_TYPES: tuple[str, ...] = tuple(RESOURCE_MODELS.keys())+RESOURCE_TYPES: tuple[str, ...] = tuple(RESOURCE_LOOKUPS.keys())# Max reminders a single user may have in the `active` status per team.MAX_ACTIVE_REMINDERS_PER_USER = 50diff --git a/products/reminders/backend/firing.py b/products/reminders/backend/firing.pyindex e2c8e44b2d3e..0fa797e02144 100644--- a/products/reminders/backend/firing.py+++ b/products/reminders/backend/firing.py@@ -14,7 +14,7 @@ from products.notifications.backend.facade.api import (TargetType,create_notification,)-from products.reminders.backend.constants import RESOURCE_MODELS+from products.reminders.backend.constants import RESOURCE_LOOKUPSfrom products.reminders.backend.models import Reminderfrom products.reminders.backend.scheduling import compute_next_fire_at, resolve_timezone@@ -27,10 +27,10 @@ MAX_CATCHUP_ITERATIONS = 1000def _build_source_url(reminder: Reminder) -> str:if reminder.team is None or not (reminder.resource_type and reminder.resource_id):return ""- mapping = RESOURCE_MODELS.get(reminder.resource_type)+ mapping = RESOURCE_LOOKUPS.get(reminder.resource_type)if not mapping:return ""- _, _, segment = mapping+ _, segment = mappingreturn f"/project/{reminder.team.project_id}/{segment}/{reminder.resource_id}"diff --git a/products/reminders/backend/test/test_api.py b/products/reminders/backend/test/test_api.pyindex 57521cd389c0..ee63d5835632 100644--- a/products/reminders/backend/test/test_api.py+++ b/products/reminders/backend/test/test_api.py@@ -16,6 +16,8 @@ from posthog.models.utils import generate_random_token_personalfrom products.access_control.backend.models.access_control import AccessControlfrom products.dashboards.backend.models.dashboard import Dashboard+from products.experiments.backend.facade.testing import create_experiment+from products.feature_flags.backend.models.feature_flag import FeatureFlagfrom products.reminders.backend.models import Reminder@@ -373,13 +375,32 @@ class TestReminderAPI(APIBaseTest):self.assertEqual(response.status_code, 200, response.content)self.assertEqual(response.json()["results"], [])- def test_attaching_a_resource_the_caller_cannot_view_is_refused(self) -> None:- dashboard = Dashboard.objects.create(team=self.team, name="Revenue")+ @parameterized.expand(+ [+ ("dashboard_viewable", "dashboard", False, False, True),+ ("dashboard_hidden", "dashboard", True, False, False),+ ("experiment_viewable", "experiment", False, False, True),+ ("experiment_hidden", "experiment", True, False, False),+ ("experiment_hidden_but_created_by_the_caller", "experiment", True, True, True),+ ]+ )+ def test_attaching_a_resource_requires_view_access(+ self, _name: str, resource_type: str, hidden: bool, created_by_caller: bool, attached: bool+ ) -> None:+ created_by = self.user if created_by_caller else None+ if resource_type == "dashboard":+ resource_id = Dashboard.objects.create(team=self.team, name="Revenue", created_by=created_by).pk+ else:+ flag = FeatureFlag.objects.create(team=self.team, key="experiment-flag")+ resource_id = create_experiment(+ team_id=self.team.id, feature_flag_id=flag.id, created_by_id=created_by.id if created_by else None+ )self.organization.available_product_features = [{"key": AvailableFeature.ACCESS_CONTROL, "name": "Access control"}]self.organization.save(update_fields=["available_product_features"])- AccessControl.objects.create(team=self.team, resource="dashboard", access_level="none")+ if hidden:+ AccessControl.objects.create(team=self.team, resource=resource_type, access_level="none")membership = self.organization.memberships.get(user=self.user)membership.level = OrganizationMembership.Level.MEMBERmembership.save(update_fields=["level"])@@ -392,15 +413,19 @@ class TestReminderAPI(APIBaseTest):"team": self.team.id,"title": "Review the weekly numbers","recurrence_interval": "weekly",- "resource_type": "dashboard",- "resource_id": str(dashboard.pk),+ "resource_type": resource_type,+ "resource_id": str(resource_id),},)- self.assertEqual(response.status_code, 400, response.content)- # Same message an absent dashboard gets, so the refusal does not confirm it exists.- self.assertIn("No dashboard with id", response.content.decode())- self.assertEqual(Reminder.objects.count(), 0)+ if attached:+ self.assertEqual(response.status_code, 201, response.content)+ self.assertEqual(Reminder.objects.get().resource_id, str(resource_id))+ else:+ self.assertEqual(response.status_code, 400, response.content)+ # Same message an absent resource gets, so the refusal does not confirm it exists.+ self.assertIn(f"No {resource_type} with id", response.content.decode())+ self.assertEqual(Reminder.objects.count(), 0)def test_list_hides_reminders_from_an_organization_enforcing_a_domain_the_user_is_outside(self) -> None:self._make_reminder()