: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
feat(subscriptions): raise hourly org cap from 1 to 5 (#58548)
Paul D'Ambrae7f19242ded8
diff --git a/ee/api/subscription.py b/ee/api/subscription.pyindex 1b5d389f356d..ef0de645fac2 100644--- a/ee/api/subscription.py+++ b/ee/api/subscription.py@@ -53,6 +53,7 @@ from ee.tasks.subscriptions.subscription_utils import DEFAULT_MAX_ASSET_COUNTSUMMARY_QUOTA_CACHE_TTL_SECONDS = 60SUMMARY_CAP_HIT_DEDUPE_TTL_SECONDS = 600HOURLY_CAP_HIT_DEDUPE_TTL_SECONDS = 600+MAX_ACTIVE_HOURLY_SUBSCRIPTIONS_PER_ORG = 5def _summary_quota_cache_key(organization_id) -> str:@@ -397,18 +398,20 @@ class SubscriptionSerializer(serializers.ModelSerializer):)if self.instance is not None:existing = existing.exclude(pk=self.instance.pk)- if existing.exists():- self._capture_hourly_cap_hit(organization)+ active_count = existing.count()+ limit = MAX_ACTIVE_HOURLY_SUBSCRIPTIONS_PER_ORG+ if active_count >= limit:+ self._capture_hourly_cap_hit(organization, active_count, limit)raise ValidationError({"frequency": [- "Only one active hourly subscription is allowed per organization. "- "Disable or delete the existing hourly subscription before adding another."+ f"Your organization can have up to {limit} active hourly subscriptions. "+ "Disable or delete an existing hourly subscription before adding another."]})- def _capture_hourly_cap_hit(self, organization) -> None:+ def _capture_hourly_cap_hit(self, organization, active_count: int, limit: int) -> None:# Rate-limited to one event per org per 10 minutes so a misbehaving# client retrying in a loop doesn't spam the analytics stream. Within# that window the user-visible 400 still fires every time.@@ -430,6 +433,8 @@ class SubscriptionSerializer(serializers.ModelSerializer):properties={"team_id": self.context.get("team_id"),"organization_id": str(organization.id),+ "active_count": active_count,+ "limit": limit,"is_create": self.instance is None,},groups={"organization": str(organization.id)},diff --git a/ee/api/test/test_subscription.py b/ee/api/test/test_subscription.pyindex 5baadad48556..e7d8ab5eb349 100644--- a/ee/api/test/test_subscription.py+++ b/ee/api/test/test_subscription.py@@ -618,43 +618,53 @@ class TestSubscriptionTemporal(APILicensedTest):else:assert expected_fragment in response.json()["detail"]- def test_cannot_create_second_active_hourly_subscription_in_org(self):- with patch("ee.api.subscription.posthoganalytics.feature_enabled", return_value=True):- first = self._create_subscription(frequency="hourly", title="first hourly")- assert first.status_code == status.HTTP_201_CREATED+ @parameterized.expand(+ [+ ("create_below_cap", 4, status.HTTP_201_CREATED),+ ("create_at_cap", 5, status.HTTP_400_BAD_REQUEST),+ ("create_over_cap_grandfathered", 7, status.HTTP_400_BAD_REQUEST),+ ]+ )+ def test_hourly_subscription_create_respects_org_cap(+ self,+ _name: str,+ existing_active: int,+ expected_status: int,+ ):+ self._seed_active_hourly_subscriptions(existing_active)- second = self._create_subscription(frequency="hourly", title="second hourly")+ with patch("ee.api.subscription.posthoganalytics.feature_enabled", return_value=True):+ response = self._create_subscription(frequency="hourly", title="new hourly")- assert second.status_code == status.HTTP_400_BAD_REQUEST- assert "Only one active hourly subscription" in str(second.json())+ assert response.status_code == expected_status, response.content+ if expected_status == status.HTTP_400_BAD_REQUEST:+ assert "up to 5 active hourly subscriptions" in str(response.json())def test_can_replace_hourly_subscription_after_disable(self):- with patch("ee.api.subscription.posthoganalytics.feature_enabled", return_value=True):- first = self._create_subscription(frequency="hourly", title="first hourly")- assert first.status_code == status.HTTP_201_CREATED+ seeded = self._seed_active_hourly_subscriptions(5)- # Disable the first hourly subscription — it should free up the org slot.+ with patch("ee.api.subscription.posthoganalytics.feature_enabled", return_value=True):+ # Disable one of the seeded hourly subscriptions — it should free up an org slot.patch_response = self.client.patch(- f"/api/projects/{self.team.id}/subscriptions/{first.json()['id']}",+ f"/api/projects/{self.team.id}/subscriptions/{seeded[0].id}",{"enabled": False},)assert patch_response.status_code == status.HTTP_200_OK- second = self._create_subscription(frequency="hourly", title="second hourly")+ response = self._create_subscription(frequency="hourly", title="replacement hourly")- assert second.status_code == status.HTTP_201_CREATED+ assert response.status_code == status.HTTP_201_CREATEDdef test_hourly_cap_hit_emits_telemetry(self):cache.clear()+ self._seed_active_hourly_subscriptions(5)+with (patch("ee.api.subscription.posthoganalytics.feature_enabled", return_value=True),patch("ee.api.subscription.posthoganalytics.capture") as mock_capture,):- first = self._create_subscription(frequency="hourly", title="first hourly")- assert first.status_code == status.HTTP_201_CREATED-- second = self._create_subscription(frequency="hourly", title="second hourly")- assert second.status_code == status.HTTP_400_BAD_REQUEST+ response = self._create_subscription(frequency="hourly", title="cap-buster hourly")+ assert response.status_code == status.HTTP_400_BAD_REQUESTcap_hit_calls = [c for c in mock_capture.call_args_list if c.kwargs.get("event") == "subscription_hourly_cap_hit"@@ -663,6 +673,8 @@ class TestSubscriptionTemporal(APILicensedTest):props = cap_hit_calls[0].kwargs["properties"]assert props["organization_id"] == str(self.organization.id)assert props["is_create"] is True+ assert props["active_count"] == 5+ assert props["limit"] == 5def _seed_active_summary_subscriptions(self, count: int) -> list[Subscription]:# Build raw rows so we can place an org over its tier cap to exercise@@ -683,6 +695,24 @@ class TestSubscriptionTemporal(APILicensedTest):for i in range(count)]+ def _seed_active_hourly_subscriptions(self, count: int) -> list[Subscription]:+ # Build raw rows so we can place an org at or above the hourly cap+ # without going through the enforced API.+ return [+ Subscription.objects.create(+ team=self.team,+ insight=self.insight,+ target_type="email",+ target_value=f"existing-hourly-{i}@posthog.com",+ frequency="hourly",+ interval=1,+ start_date=datetime(2022, 1, 1, tzinfo=UTC),+ title=f"existing hourly {i}",+ created_by=self.user,+ )+ for i in range(count)+ ]+@parameterized.expand([("create_under_limit", 5, 4, status.HTTP_201_CREATED),diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsxindex 1005f4df2509..60690c77544a 100644--- a/frontend/src/lib/constants.tsx+++ b/frontend/src/lib/constants.tsx@@ -466,7 +466,7 @@ export const FEATURE_FLAGS = {SQL_EDITOR_VIM_MODE: 'sql-editor-vim-mode', // owner: @arthurdedeusSSE_DASHBOARDS: 'sse-dashboards', // owner: @aspicer #team-analytics-platformSUBSCRIPTION_AI_SUMMARY_PROMPT_GUIDE: 'subscription-ai-summary-prompt-guide', // owner: #team-analytics-platform, gates the per-subscription prompt guide textarea- SUBSCRIPTION_HOURLY_FREQUENCY: 'subscription-hourly-frequency', // owner: #team-analytics-platform, gates hourly subscription frequency (one active hourly subscription per org)+ SUBSCRIPTION_HOURLY_FREQUENCY: 'subscription-hourly-frequency', // owner: #team-analytics-platform, gates hourly subscription frequency (up to 5 active hourly subscriptions per org)SURVEY_HEADLINE_SUMMARY: 'survey-headline-summary', // owner: @adboio #team-surveysSURVEYS_ERROR_TRACKING_CROSS_SELL: 'surveys-in-error-tracking', // owner: @adboio #team-surveysSURVEYS_FORM_BUILDER: 'surveys-form-builder', // owner: @adboio #team-surveys