: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(alerts): add real_time calculation interval schema, feature, and model helpers (#67933)
Matt Pua6d8d49497602
diff --git a/ee/models/license.py b/ee/models/license.pyindex 2e683b34a83d..1a223e6af19c 100644--- a/ee/models/license.py+++ b/ee/models/license.py@@ -68,6 +68,7 @@ class License(models.Model):AvailableFeature.RECORDINGS_FILE_EXPORT,AvailableFeature.RECORDINGS_PERFORMANCE,AvailableFeature.HIGH_FREQUENCY_ALERTS,+ AvailableFeature.REAL_TIME_ALERTS,]ENTERPRISE_PLAN = "enterprise"diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex c0b686da7e91..a8e2367c7147 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -1383,7 +1383,7 @@ snapshots:components-playerinspector--with-logs-filter--dark:hash: v1.k794b7964.4a27837802248b94d5c146c18b6962bfc713ce40ec3da10a23d4fc89639cf794.PRcX4NksDqdhgKXU-BTlmrZ4mkRVcQIAlLsj5nbUq9Icomponents-playerinspector--with-logs-filter--light:- hash: v1.k794b7964.92f552147017e8b361ea4c2eff62c087ce37fedfc44d40945ba40dbf12e01f08.KBu7dt9Qb1dmTqIGmFIKmCjktRGu8gAFQa0YzD86djA+ hash: v1.k794b7964.97c396a5d41b78e6f653cdcb59a7e59dce49c44d08a9c3903dd69324eb20fdd6.UcFWQBXiTFaua5umBEqB8d8UUCI6aLN4cXg2abaVyJ0components-playerinspector--with-logs-filter-upsell--dark:hash: v1.k794b7964.5e4afe863e34df7278c0dd092946adfd396ee778f0cd0a41e5e3f5e1771a9601.SBOdVp0jJnn1w2H4fMR-3Vf2ac00QSBFQjsl93AbcYAcomponents-playerinspector--with-logs-filter-upsell--light:diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.jsonindex 8449228049fc..f1189e722626 100644--- a/frontend/src/queries/schema.json+++ b/frontend/src/queries/schema.json@@ -612,7 +612,7 @@"type": "string"},"AlertCalculationInterval": {- "enum": ["every_15_minutes", "hourly", "daily", "weekly", "monthly"],+ "enum": ["real_time", "every_15_minutes", "hourly", "daily", "weekly", "monthly"],"type": "string"},"AlertCondition": {diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.tsindex d18b5f2a7ca5..4ed473511e49 100644--- a/frontend/src/queries/schema/schema-general.ts+++ b/frontend/src/queries/schema/schema-general.ts@@ -5087,6 +5087,7 @@ export enum AlertState {}export enum AlertCalculationInterval {+ REAL_TIME = 'real_time',EVERY_15_MINUTES = 'every_15_minutes',HOURLY = 'hourly',DAILY = 'daily',diff --git a/posthog/constants.py b/posthog/constants.pyindex 825e0db0d791..1e99db77696c 100644--- a/posthog/constants.py+++ b/posthog/constants.py@@ -40,6 +40,7 @@ class AvailableFeature(StrEnum):DATA_PIPELINES = "data_pipelines"ALERTS = "alerts"HIGH_FREQUENCY_ALERTS = "high_frequency_alerts"+ REAL_TIME_ALERTS = "real_time_alerts"DATA_COLOR_THEMES = "data_color_themes"API_QUERIES_CONCURRENCY = "api_queries_concurrency"ORGANIZATION_INVITE_SETTINGS = "organization_invite_settings"diff --git a/posthog/schema_enums.py b/posthog/schema_enums.pyindex 0a9a939cdb10..6bababbfb721 100644--- a/posthog/schema_enums.py+++ b/posthog/schema_enums.py@@ -65,6 +65,7 @@ class AggregationAxisFormat(StrEnum):class AlertCalculationInterval(StrEnum):+ REAL_TIME = "real_time"EVERY_15_MINUTES = "every_15_minutes"HOURLY = "hourly"DAILY = "daily"diff --git a/products/alerts/backend/models/alert.py b/products/alerts/backend/models/alert.pyindex 4d7f1b267e5a..5673dd9aa45b 100644--- a/products/alerts/backend/models/alert.py+++ b/products/alerts/backend/models/alert.py@@ -195,7 +195,10 @@ class AlertConfiguration(ModelActivityMixin, CreatedMetaFields, UUIDTModel):@propertydef is_high_frequency_interval(self) -> bool:- return self.calculation_interval == AlertCalculationInterval.EVERY_15_MINUTES+ return self.calculation_interval in (+ AlertCalculationInterval.EVERY_15_MINUTES,+ AlertCalculationInterval.REAL_TIME,+ )def get_subscribed_users_emails(self) -> list[str]:return list(@@ -333,6 +336,43 @@ class AlertConfiguration(ModelActivityMixin, CreatedMetaFields, UUIDTModel):return "15-minute alert intervals require a Boost, Scale, or Enterprise platform add-on."return None+ @classmethod+ def real_time_interval_validation_error(+ cls,+ *,+ calculation_interval: str | AlertCalculationInterval | None,+ organization: Organization,+ ) -> str | None:+ if calculation_interval != AlertCalculationInterval.REAL_TIME:+ return None+ if not organization.is_feature_available(AvailableFeature.REAL_TIME_ALERTS):+ return "Real-time alert intervals require a Scale or Enterprise plan."+ return None++ @classmethod+ def check_real_time_alert_limit(cls, team_id: int, organization: Organization) -> str | None:+ """Return an error message if the team has reached its real-time alert limit, else None.++ Unlike check_alert_limit (which counts every alert against the ALERTS feature), this+ counts only real-time alerts against the REAL_TIME_ALERTS feature's limit. Orgs without+ the feature are already blocked by real_time_interval_validation_error.+ """+ feature = organization.get_available_feature(AvailableFeature.REAL_TIME_ALERTS)+ if not feature:+ return None++ allowed = feature.get("limit")+ # If allowed is None then the org is allowed unlimited real-time alerts+ if allowed is None:+ return None++ existing_count = cls.objects.filter(+ team_id=team_id, calculation_interval=AlertCalculationInterval.REAL_TIME, enabled=True+ ).count()+ if existing_count >= allowed:+ return f"Your team has reached the limit of {allowed} real-time alerts on your plan."+ return None+class AlertSubscription(ModelActivityMixin, CreatedMetaFields, UUIDTModel):user = models.ForeignKey(diff --git a/products/alerts/frontend/components/editAlertModal/editAlertModalUtils.tsx b/products/alerts/frontend/components/editAlertModal/editAlertModalUtils.tsxindex c0410b5652bf..1f9cd9229b28 100644--- a/products/alerts/frontend/components/editAlertModal/editAlertModalUtils.tsx+++ b/products/alerts/frontend/components/editAlertModal/editAlertModalUtils.tsx@@ -4,6 +4,12 @@ import { AlertCalculationInterval } from '~/queries/schema/schema-general'export function getSimulationRangeOptions(interval: AlertCalculationInterval): { label: string; value: string }[] {switch (interval) {+ case AlertCalculationInterval.REAL_TIME:+ return [+ { label: 'Last 10 minutes', value: '-10m' },+ { label: 'Last 1 hour', value: '-1h' },+ { label: 'Last 3 hours', value: '-3h' },+ ]case AlertCalculationInterval.EVERY_15_MINUTES:return [{ label: 'Last 12h', value: '-12h' },@@ -44,6 +50,8 @@ export function getSimulationRangeOptions(interval: AlertCalculationInterval): {export function alertCalculationIntervalToLabel(interval: AlertCalculationInterval): string {switch (interval) {+ case AlertCalculationInterval.REAL_TIME:+ return 'Real time'case AlertCalculationInterval.EVERY_15_MINUTES:return '15 minutes'case AlertCalculationInterval.HOURLY:diff --git a/products/alerts/frontend/logic/alertIntervalHelpers.ts b/products/alerts/frontend/logic/alertIntervalHelpers.tsindex e16a601d56f9..8674ab1007ed 100644--- a/products/alerts/frontend/logic/alertIntervalHelpers.ts+++ b/products/alerts/frontend/logic/alertIntervalHelpers.ts@@ -7,6 +7,8 @@ import { AvailableFeature } from '~/types'export function getDefaultSimulationRange(interval: AlertCalculationInterval): string {switch (interval) {+ case AlertCalculationInterval.REAL_TIME:+ return '-1h'case AlertCalculationInterval.EVERY_15_MINUTES:return '-12h'case AlertCalculationInterval.HOURLY: