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.

fix(alerts): allow saving anomaly alerts with null bounds (#74943)

Matt Puadfd01da03444
diff --git a/products/alerts/frontend/logic/alertFormLogic.test.ts b/products/alerts/frontend/logic/alertFormLogic.test.ts
index 9461559bf077..d37206424634 100644
--- a/products/alerts/frontend/logic/alertFormLogic.test.ts
+++ b/products/alerts/frontend/logic/alertFormLogic.test.ts
@@ -362,9 +362,37 @@ describe('alertFormLogic', () => {
expect(createSpy).not.toHaveBeenCalled()
expect(successToastSpy).not.toHaveBeenCalled()
+ expect(errorToastSpy).toHaveBeenCalledWith(
+ "Couldn't save alert: Enter at least one threshold (less than or more than)"
+ )
expect(logic.values.thresholdBoundsFormError).toBe('Enter at least one threshold (less than or more than)')
})
+ it('shows multiple validation errors without duplicate punctuation', async () => {
+ const logic = mountForm()
+ logic.actions.setAlertFormValues({
+ ...makeFormDefaults({
+ name: '',
+ threshold: {
+ configuration: {
+ type: InsightThresholdType.ABSOLUTE,
+ bounds: {},
+ },
+ },
+ schedule_restriction: { blocked_windows: [{ start: '09:00', end: '09:00' }] },
+ }),
+ checks: undefined,
+ })
+
+ await expectLogic(logic, () => {
+ logic.actions.submitAlertForm()
+ }).toFinishAllListeners()
+
+ expect(errorToastSpy).toHaveBeenCalledWith(
+ "Couldn't save alert: You need to give your alert a name. Start and end must differ. Enter at least one threshold (less than or more than)"
+ )
+ })
+
it('treats cleared threshold inputs as missing bounds', () => {
expect(
thresholdAlertHasBounds({
diff --git a/products/alerts/frontend/logic/alertFormLogic.ts b/products/alerts/frontend/logic/alertFormLogic.ts
index 9fb974ae3b2c..daca730fcae8 100644
--- a/products/alerts/frontend/logic/alertFormLogic.ts
+++ b/products/alerts/frontend/logic/alertFormLogic.ts
@@ -404,12 +404,12 @@ export interface alertFormLogicMeta {
hogqlAlertPreview: (
insightData: Record<string, any>,
arg: AlertConfig,
- arg2: InsightsThresholdBounds | undefined
+ arg2: InsightsThresholdBounds | null | undefined
) => HogQLAlertPreview | null
funnelAlertPreview: (
insightData: Record<string, any>,
arg: AlertConfig,
- arg2: InsightsThresholdBounds | undefined,
+ arg2: InsightsThresholdBounds | null | undefined,
arg3: AlertConditionType,
arg4: InsightThresholdType
) => FunnelAlertPreview | null
@@ -890,6 +890,13 @@ export const alertFormLogic = kea<alertFormLogicType>([
},
submitAlertForm: () => {
actions.setAlertFormSubmitAttempted()
+ const validationErrors = Object.values(values.alertFormValidationErrors).filter(
+ (error): error is string => typeof error === 'string'
+ )
+ if (validationErrors.length > 0) {
+ const message = validationErrors.map((error) => error.replace(/[.!?]+$/, '')).join('. ')
+ lemonToast.error(`Couldn't save alert: ${message}`)
+ }
},
submitAlertFormSuccess: async () => {
// Background sync to pick up any server-side changes
diff --git a/products/alerts/frontend/logic/alertFormSchema.test.ts b/products/alerts/frontend/logic/alertFormSchema.test.ts
index 4b5892da6a45..9074168ca1f7 100644
--- a/products/alerts/frontend/logic/alertFormSchema.test.ts
+++ b/products/alerts/frontend/logic/alertFormSchema.test.ts
@@ -105,7 +105,7 @@ describe('alertFormSchema', () => {
).toBe(false)
})
- it('skips threshold bounds when detector_config is set', () => {
+ it.each([{}, null])('skips threshold bounds when detector_config is set and bounds are %p', (bounds) => {
expect(
getAlertFormValidationErrors({
...baseAlert,
@@ -113,7 +113,7 @@ describe('alertFormSchema', () => {
threshold: {
configuration: {
type: InsightThresholdType.ABSOLUTE,
- bounds: {},
+ bounds,
},
},
})
diff --git a/products/alerts/frontend/logic/alertFormSchema.ts b/products/alerts/frontend/logic/alertFormSchema.ts
index 48291b0e6faf..79fa0ce0dbcc 100644
--- a/products/alerts/frontend/logic/alertFormSchema.ts
+++ b/products/alerts/frontend/logic/alertFormSchema.ts
@@ -41,7 +41,7 @@ const alertFormSchema = z
lower: z.number().finite().nullish(),
upper: z.number().finite().nullish(),
})
- .optional(),
+ .nullish(),
})
.optional(),
})
diff --git a/products/alerts/frontend/logic/thresholdPercent.ts b/products/alerts/frontend/logic/thresholdPercent.ts
index b2fdf53c2ee0..7d7f82cdda35 100644
--- a/products/alerts/frontend/logic/thresholdPercent.ts
+++ b/products/alerts/frontend/logic/thresholdPercent.ts
@@ -1,4 +1,6 @@
-import { AlertConditionType, InsightThreshold, InsightThresholdType } from '~/queries/schema/schema-general'
+import { AlertConditionType, InsightThresholdType } from '~/queries/schema/schema-general'
+
+import type { AlertThreshold } from '../types'
// PERCENTAGE thresholds are stored as a 0–1 fraction and shown ×100; ABSOLUTE ones store the raw
// percent. These helpers keep the on-screen number stable across that boundary and round the
@@ -26,9 +28,9 @@ export const rescaleThresholdBound = (value: number | undefined, toType: Insight
}
export function thresholdForUnitChange(
- configuration: InsightThreshold,
+ configuration: AlertThreshold,
targetType: InsightThresholdType
-): InsightThreshold {
+): AlertThreshold {
if (configuration.type === targetType) {
return configuration
}
@@ -43,10 +45,10 @@ export function thresholdForUnitChange(
}
export function thresholdForConditionChange(
- configuration: InsightThreshold,
+ configuration: AlertThreshold,
conditionType: AlertConditionType,
forcePercentageForRelative: boolean
-): InsightThreshold {
+): AlertThreshold {
let targetType = configuration.type
if (conditionType === AlertConditionType.ABSOLUTE_VALUE) {
targetType = InsightThresholdType.ABSOLUTE
diff --git a/products/alerts/frontend/types.ts b/products/alerts/frontend/types.ts
index 15c636264ee0..d6fb1b130404 100644
--- a/products/alerts/frontend/types.ts
+++ b/products/alerts/frontend/types.ts
@@ -8,6 +8,7 @@ import {
FunnelsAlertConfig,
HogQLAlertConfig,
InsightThreshold,
+ InsightsThresholdBounds,
MetricsAlertConfig,
TrendsAlertConfig,
} from '~/queries/schema/schema-general'
@@ -99,10 +100,14 @@ export interface AnomalyPoint {
export type InvestigationInconclusiveAction = 'notify' | 'suppress'
+export type AlertThreshold = Omit<InsightThreshold, 'bounds'> & {
+ bounds?: InsightsThresholdBounds | null
+}
+
export interface AlertTypeBase {
name: string
condition: AlertCondition
- threshold: { configuration: InsightThreshold }
+ threshold: { configuration: AlertThreshold }
enabled: boolean
insight: QueryBasedInsightModel
config: AlertConfig