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(customer-analytics): notify usage spikes from relationship assignments (#68998)

Arthur Moreira de Deus0a1809a70de9
diff --git a/products/customer_analytics/backend/logic/usage_spike_notifications.py b/products/customer_analytics/backend/logic/usage_spike_notifications.py
index 73ea21acf4bf..065e46e89825 100644
--- a/products/customer_analytics/backend/logic/usage_spike_notifications.py
+++ b/products/customer_analytics/backend/logic/usage_spike_notifications.py
@@ -7,7 +7,7 @@ from posthog.exceptions_capture import capture_exception
from products.customer_analytics.backend.account_urls import build_account_deeplink
from products.customer_analytics.backend.constants import CUSTOMER_ANALYTICS_CSP_FLAG
-from products.customer_analytics.backend.models import Account
+from products.customer_analytics.backend.models import Account, AccountRelationship
from products.notifications.backend.facade.api import (
NotificationData,
NotificationType,
@@ -126,12 +126,21 @@ def _is_csp_enabled(account: Account) -> bool:
def _get_manager_user_ids(account: Account) -> list[int]:
- properties = account.properties
- user_ids: list[int] = []
- for assignment in (properties.csm, properties.account_executive):
- if assignment is not None and assignment.id not in user_ids:
- user_ids.append(assignment.id)
- return user_ids
+ # Only members of the organization that owns the accounts workspace. Relationship
+ # holders outside it (e.g. a customer assigned as account owner, or a teammate whose
+ # membership lapsed after assignment) must never receive internal usage-spike
+ # notifications.
+ return list(
+ AccountRelationship.objects.for_team(account.team_id)
+ .filter(
+ account=account,
+ ended_at__isnull=True,
+ user__isnull=False,
+ user__organization_membership__organization_id=account.team.organization_id,
+ )
+ .values_list("user_id", flat=True)
+ .distinct()
+ )
def _build_body(spikes: list[dict[str, Any]], detected_at: str | None) -> str:
diff --git a/products/customer_analytics/backend/test/test_usage_spike_notifications.py b/products/customer_analytics/backend/test/test_usage_spike_notifications.py
index 8752119f68c7..8de2f66bd50c 100644
--- a/products/customer_analytics/backend/test/test_usage_spike_notifications.py
+++ b/products/customer_analytics/backend/test/test_usage_spike_notifications.py
@@ -3,9 +3,11 @@ from unittest.mock import patch
from parameterized import parameterized
-from posthog.models import Team
+from posthog.models import Organization, Team, User
+from products.customer_analytics.backend.logic import relationships as relationships_logic
from products.customer_analytics.backend.logic.usage_spike_notifications import notify_managers_of_usage_spike
+from products.customer_analytics.backend.models import Account, AccountRelationshipDefinition
from products.customer_analytics.backend.test.factories import create_account
from products.notifications.backend.facade.enums import NotificationType, Priority, SourceType, TargetType
@@ -22,40 +24,52 @@ class TestNotifyManagersOfUsageSpike(BaseTest):
csp_patcher = patch("posthoganalytics.feature_enabled", return_value=True)
self.mock_feature_enabled = csp_patcher.start()
self.addCleanup(csp_patcher.stop)
+ self.csm_user = self._create_user("[email protected]")
+ self.ae_user = self._create_user("[email protected]")
+ self.csm_definition = self._create_definition("CSM")
+ self.ae_definition = self._create_definition("Account executive")
+
+ def _create_definition(self, name: str, team_id: int | None = None) -> AccountRelationshipDefinition:
+ team_id = self.team.id if team_id is None else team_id
+ return AccountRelationshipDefinition.objects.for_team(team_id).create(team_id=team_id, name=name)
+
+ def _assign(
+ self, account: Account, definition: AccountRelationshipDefinition, user: User, team_id: int | None = None
+ ) -> None:
+ relationships_logic.assign(
+ team_id=self.team.id if team_id is None else team_id,
+ account=account,
+ definition=definition,
+ user=user,
+ created_by=None,
+ )
def _notify_managers(self, *, team_id: int | None = None, **kwargs):
notify_managers_of_usage_spike(
team_id=self.team.id if team_id is None else team_id, spike_id="spike-1", spikes=SPIKES, **kwargs
)
- def _create_account_with_managers(self, *, csm_id: int | None = 101, ae_id: int | None = 102, **account_kwargs):
- properties: dict = {}
- if csm_id is not None:
- properties["csm"] = {"id": csm_id, "email": f"csm{csm_id}@example.com"}
- if ae_id is not None:
- properties["account_executive"] = {"id": ae_id, "email": f"ae{ae_id}@example.com"}
- return create_account(team_id=self.team.id, name="Acme Corp", properties=properties, **account_kwargs)
+ def _create_account_with_managers(self, **account_kwargs) -> Account:
+ account = create_account(team_id=self.team.id, name="Acme Corp", **account_kwargs)
+ self._assign(account, self.csm_definition, self.csm_user)
+ self._assign(account, self.ae_definition, self.ae_user)
+ return account
@parameterized.expand(
[
- ("external_id", {"external_id": "org-123"}, {}, {"organization_id": "org-123"}),
- ("billing_id", {}, {"billing_id": "bill-9"}, {"billing_id": "bill-9"}),
- ("stripe_customer_id", {}, {"stripe_customer_id": "cus_42"}, {"stripe_customer_id": "cus_42"}),
+ ("external_id", {"external_id": "org-123"}, {"organization_id": "org-123"}),
+ ("billing_id", {"properties": {"billing_id": "bill-9"}}, {"billing_id": "bill-9"}),
+ ("stripe_customer_id", {"properties": {"stripe_customer_id": "cus_42"}}, {"stripe_customer_id": "cus_42"}),
]
)
- def test_matches_account_by_identifier(
- self, mock_create, _mock_dispatched, _name, account_kwargs, properties_extra, lookup
- ):
- account = self._create_account_with_managers(**account_kwargs)
- if properties_extra:
- account.properties = {**account.properties.model_dump(mode="json"), **properties_extra}
- account.save()
+ def test_matches_account_by_identifier(self, mock_create, _mock_dispatched, _name, account_kwargs, lookup):
+ self._create_account_with_managers(**account_kwargs)
self._notify_managers(**lookup)
assert mock_create.call_count == 2
target_ids = {call.args[0].target_id for call in mock_create.call_args_list}
- assert target_ids == {"101", "102"}
+ assert target_ids == {str(self.csm_user.id), str(self.ae_user.id)}
def test_no_match_does_not_notify(self, mock_create, mock_dispatched):
self._create_account_with_managers(external_id="org-123")
@@ -65,21 +79,43 @@ class TestNotifyManagersOfUsageSpike(BaseTest):
def test_account_on_other_team_is_not_matched(self, mock_create, mock_dispatched):
other_team = Team.objects.create(organization=self.organization, name="Other")
- create_account(
- team_id=other_team.id,
- name="Acme Corp",
- properties={"csm": {"id": 101, "email": "[email protected]"}},
- external_id="org-123",
- )
+ other_definition = self._create_definition("CSM", team_id=other_team.id)
+ account = create_account(team_id=other_team.id, name="Acme Corp", external_id="org-123")
+ self._assign(account, other_definition, self.csm_user, team_id=other_team.id)
+
self._notify_managers(organization_id="org-123")
+
mock_create.assert_not_called()
mock_dispatched.assert_not_called()
- def test_account_without_managers_does_not_notify(self, mock_create, _mock_dispatched):
- self._create_account_with_managers(csm_id=None, ae_id=None, external_id="org-123")
+ def test_account_without_relationships_does_not_notify(self, mock_create, _mock_dispatched):
+ create_account(team_id=self.team.id, name="Acme Corp", external_id="org-123")
self._notify_managers(organization_id="org-123")
mock_create.assert_not_called()
+ def test_relationship_holder_outside_the_organization_is_not_notified(self, mock_create, _mock_dispatched):
+ customer_org = Organization.objects.create(name="Customer Org")
+ customer_user = User.objects.create_and_join(customer_org, "[email protected]", None)
+ owner_definition = self._create_definition("Account owner")
+ account = create_account(team_id=self.team.id, name="Acme Corp", external_id="org-123")
+ self._assign(account, self.csm_definition, self.csm_user)
+ self._assign(account, owner_definition, customer_user)
+
+ self._notify_managers(organization_id="org-123")
+
+ mock_create.assert_called_once()
+ assert mock_create.call_args.args[0].target_id == str(self.csm_user.id)
+
+ def test_ended_assignment_is_not_notified(self, mock_create, _mock_dispatched):
+ account = create_account(team_id=self.team.id, name="Acme Corp", external_id="org-123")
+ self._assign(account, self.csm_definition, self.csm_user)
+ self._assign(account, self.csm_definition, self.ae_user)
+
+ self._notify_managers(organization_id="org-123")
+
+ mock_create.assert_called_once()
+ assert mock_create.call_args.args[0].target_id == str(self.ae_user.id)
+
def test_csp_disabled_does_not_notify(self, mock_create, mock_dispatched):
self.mock_feature_enabled.return_value = False
self._create_account_with_managers(external_id="org-123")
@@ -92,11 +128,15 @@ class TestNotifyManagersOfUsageSpike(BaseTest):
self._notify_managers(organization_id="org-123")
mock_create.assert_not_called()
- def test_dedupes_when_csm_and_ae_are_same_user(self, mock_create, _mock_dispatched):
- self._create_account_with_managers(csm_id=55, ae_id=55, external_id="org-123")
+ def test_dedupes_when_same_user_holds_multiple_relationships(self, mock_create, _mock_dispatched):
+ account = create_account(team_id=self.team.id, name="Acme Corp", external_id="org-123")
+ self._assign(account, self.csm_definition, self.csm_user)
+ self._assign(account, self.ae_definition, self.csm_user)
+
self._notify_managers(organization_id="org-123")
+
mock_create.assert_called_once()
- assert mock_create.call_args.args[0].target_id == "55"
+ assert mock_create.call_args.args[0].target_id == str(self.csm_user.id)
def test_idempotent_when_already_dispatched(self, mock_create, mock_dispatched):
mock_dispatched.return_value = True
@@ -105,7 +145,9 @@ class TestNotifyManagersOfUsageSpike(BaseTest):
mock_create.assert_not_called()
def test_notification_content(self, mock_create, _mock_dispatched):
- account = self._create_account_with_managers(csm_id=101, ae_id=None, external_id="org-123")
+ account = create_account(team_id=self.team.id, name="Acme Corp", external_id="org-123")
+ self._assign(account, self.csm_definition, self.csm_user)
+
self._notify_managers(organization_id="org-123", detected_at="2026-06-09")
data = mock_create.call_args.args[0]