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): match alert banner and anomaly button to backend rules (#116600)

Jimmy Zhubc8f5aaac69f
diff --git a/frontend/src/lib/components/Cards/InsightCard/InsightMeta.tsx b/frontend/src/lib/components/Cards/InsightCard/InsightMeta.tsx
index 665dd13c7636..976a44cf7be1 100644
--- a/frontend/src/lib/components/Cards/InsightCard/InsightMeta.tsx
+++ b/frontend/src/lib/components/Cards/InsightCard/InsightMeta.tsx
@@ -294,10 +294,9 @@ export function InsightMeta({
) : null
const showDashboardAlertsMenuItem = isUsedAsDashboardTile && !!dashboardId && !!insight.id && canViewInsight
- const canCreateAlertForInsight = areAlertsSupportedForInsight(query, {
- metricsAlertsEnabled: !!featureFlags[FEATURE_FLAGS.METRICS],
- })
- const canCreateAnomalyAlertForInsight = areAnomalyAlertsSupportedForInsight(query)
+ const alertSupportOptions = { metricsAlertsEnabled: !!featureFlags[FEATURE_FLAGS.METRICS] }
+ const canCreateAlertForInsight = areAlertsSupportedForInsight(query, alertSupportOptions)
+ const canCreateAnomalyAlertForInsight = areAnomalyAlertsSupportedForInsight(query, alertSupportOptions)
const showCopyTableData = isInsightVizNode(query) && isTrendsQuery(query.source)
const copyTableAdapter = useMemo(() => getInsightExportAdapter(insightDataRaw, query), [insightDataRaw, query])
diff --git a/products/alerts/frontend/logic/insightAlertsLogic.test.ts b/products/alerts/frontend/logic/insightAlertsLogic.test.ts
index 5accda41fec9..fd8fde8c38a8 100644
--- a/products/alerts/frontend/logic/insightAlertsLogic.test.ts
+++ b/products/alerts/frontend/logic/insightAlertsLogic.test.ts
@@ -7,7 +7,7 @@ import { insightDataLogic } from 'scenes/insights/insightDataLogic'
import { createEmptyInsight, insightLogic } from 'scenes/insights/insightLogic'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
-import { NodeKind } from '~/queries/schema/schema-general'
+import { Node, NodeKind } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'
import { ChartDisplayType, HogFunctionType, InsightLogicProps, InsightShortId } from '~/types'
@@ -44,6 +44,21 @@ const METRICS_QUERY = {
clauses: [{ name: 'a', metricName: 'queue.depth', aggregation: 'avg' }],
}
+const funnelWithViz = (funnelVizType?: string): Node & { source: Record<string, any> } => ({
+ ...FUNNEL_QUERY,
+ source: { ...FUNNEL_QUERY.source, funnelsFilter: { funnelVizType } },
+})
+
+const SQL_QUERY = {
+ kind: NodeKind.DataVisualizationNode,
+ source: { kind: NodeKind.HogQLQuery, query: 'SELECT count() FROM events' },
+}
+
+const RETENTION_QUERY = {
+ kind: NodeKind.InsightVizNode,
+ source: { kind: NodeKind.RetentionQuery, retentionFilter: {} },
+}
+
describe('insightAlertsLogic', () => {
let listSpy: jest.SpyInstance
let hogFunctionsListSpy: jest.SpyInstance
@@ -276,11 +291,14 @@ describe('insightAlertsLogic', () => {
})
it.each([
- { metricsEnabled: true, expectedWarning: false },
- { metricsEnabled: false, expectedWarning: true },
+ { name: 'metrics', query: METRICS_QUERY, metricsEnabled: true, expectedWarning: false },
+ { name: 'metrics', query: METRICS_QUERY, metricsEnabled: false, expectedWarning: false },
+ { name: 'flow funnel', query: funnelWithViz('flow'), metricsEnabled: false, expectedWarning: false },
+ { name: 'SQL', query: SQL_QUERY, metricsEnabled: false, expectedWarning: false },
+ { name: 'retention', query: RETENTION_QUERY, metricsEnabled: true, expectedWarning: true },
])(
- 'with metrics flag $metricsEnabled, a metrics query on an insight with alerts sets the deletion warning to $expectedWarning',
- async ({ metricsEnabled, expectedWarning }) => {
+ 'with metrics flag $metricsEnabled, a $name query on an insight with alerts sets the deletion warning to $expectedWarning',
+ async ({ query, metricsEnabled, expectedWarning }) => {
featureFlagLogic.mount()
featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.METRICS]: metricsEnabled })
const insightLogicProps: InsightLogicProps = {
@@ -288,7 +306,7 @@ describe('insightAlertsLogic', () => {
cachedInsight: {
...createEmptyInsight(Insight42),
id: 42,
- query: METRICS_QUERY,
+ query,
alerts: [{ id: 'alert-a' }] as AlertType[],
},
}
@@ -297,7 +315,7 @@ describe('insightAlertsLogic', () => {
alertsLogic.mount()
await expectLogic(alertsLogic, () => {
- insightVizDataLogic(insightLogicProps).actions.setQuery(METRICS_QUERY)
+ insightVizDataLogic(insightLogicProps).actions.setQuery(query)
}).toDispatchActions(['setShouldShowAlertDeletionWarning'])
expect(alertsLogic.values.shouldShowAlertDeletionWarning).toBe(expectedWarning)
}
@@ -367,30 +385,27 @@ describe('areAlertsSupportedForInsight', () => {
})
it('supports steps and trends funnels but not time-to-convert or flow', () => {
- const withViz = (funnelVizType?: string): Record<string, any> => ({
- ...FUNNEL_QUERY,
- source: { ...FUNNEL_QUERY.source, funnelsFilter: { funnelVizType } },
- })
- expect(areAlertsSupportedForInsight(withViz('steps'))).toBe(true)
- expect(areAlertsSupportedForInsight(withViz('trends'))).toBe(true)
- expect(areAlertsSupportedForInsight(withViz('time_to_convert'))).toBe(false)
- expect(areAlertsSupportedForInsight(withViz('flow'))).toBe(false)
+ expect(areAlertsSupportedForInsight(funnelWithViz('steps'))).toBe(true)
+ expect(areAlertsSupportedForInsight(funnelWithViz('trends'))).toBe(true)
+ expect(areAlertsSupportedForInsight(funnelWithViz('time_to_convert'))).toBe(false)
+ expect(areAlertsSupportedForInsight(funnelWithViz('flow'))).toBe(false)
})
- it.each<[string, ChartDisplayType | undefined, boolean]>([
- ['line graph', ChartDisplayType.ActionsLineGraph, true],
- ['default display', undefined, true],
- ['bold number', ChartDisplayType.BoldNumber, false],
- ['bar value', ChartDisplayType.ActionsBarValue, false],
- ])('reports anomaly support for %s trends', (_name, display, expected) => {
- const query = {
- ...API_QUERY,
- source: {
- ...API_QUERY.source,
- trendsFilter: display ? { display } : null,
- },
- }
- expect(areAnomalyAlertsSupportedForInsight(query)).toBe(expected)
+ const trendsWithDisplay = (display?: ChartDisplayType): Record<string, any> => ({
+ ...API_QUERY,
+ source: { ...API_QUERY.source, trendsFilter: display ? { display } : null },
+ })
+
+ it.each<[string, Record<string, any>, boolean]>([
+ ['line graph trends', trendsWithDisplay(ChartDisplayType.ActionsLineGraph), true],
+ ['default display trends', trendsWithDisplay(), true],
+ ['bold number trends', trendsWithDisplay(ChartDisplayType.BoldNumber), false],
+ ['bar value trends', trendsWithDisplay(ChartDisplayType.ActionsBarValue), false],
+ ['SQL', SQL_QUERY, true],
+ ['funnel', FUNNEL_QUERY, false],
+ ['metrics', METRICS_QUERY, false],
+ ])('reports anomaly support for %s with the metrics flag on', (_name, query, expected) => {
+ expect(areAnomalyAlertsSupportedForInsight(query, { metricsAlertsEnabled: true })).toBe(expected)
})
})
@@ -407,10 +422,6 @@ describe('alertsUnsupportedReason', () => {
// A time-to-convert/flow funnel is itself a funnel, so the generic "funnel insights are supported"
// copy reads as a contradiction; the reason should name the real cause instead.
it('gives a funnel-viz-specific reason for time-to-convert / flow funnels', () => {
- const funnelWithViz = (funnelVizType: string): Record<string, any> => ({
- ...FUNNEL_QUERY,
- source: { ...FUNNEL_QUERY.source, funnelsFilter: { funnelVizType } },
- })
for (const viz of ['time_to_convert', 'flow']) {
const reason = alertsUnsupportedReason({}, funnelWithViz(viz))
expect(reason).toContain('conversion rate')
diff --git a/products/alerts/frontend/logic/insightAlertsLogic.ts b/products/alerts/frontend/logic/insightAlertsLogic.ts
index d895d19b936b..4f7088b70220 100644
--- a/products/alerts/frontend/logic/insightAlertsLogic.ts
+++ b/products/alerts/frontend/logic/insightAlertsLogic.ts
@@ -2,12 +2,17 @@ import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path,
import { loaders } from 'kea-loaders'
import api from 'lib/api'
-import { FEATURE_FLAGS, NON_TIME_SERIES_DISPLAY_TYPES } from 'lib/constants'
-import { FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'
+import { NON_TIME_SERIES_DISPLAY_TYPES } from 'lib/constants'
import { insightLogic } from 'scenes/insights/insightLogic'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
-import { AlertConditionType, BreakdownFilter, GoalLine, InsightThresholdType } from '~/queries/schema/schema-general'
+import {
+ AlertConditionType,
+ BreakdownFilter,
+ GoalLine,
+ InsightThresholdType,
+ NodeKind,
+} from '~/queries/schema/schema-general'
import {
containsHogQLQuery,
hasBreakdownFilter,
@@ -74,7 +79,24 @@ export const areAnomalyAlertsSupportedForInsight = (
const display = query.source.trendsFilter?.display
return !display || !NON_TIME_SERIES_DISPLAY_TYPES.includes(display)
}
- return true
+ // Matches the backend DETECTOR_EXTRACTORS, which rejects an anomaly alert on any kind but trends and SQL.
+ return containsHogQLQuery(query)
+}
+
+const ALERT_SURVIVING_QUERY_KINDS: ReadonlySet<string> = new Set([
+ NodeKind.TrendsQuery,
+ NodeKind.HogQLQuery,
+ NodeKind.FunnelsQuery,
+ NodeKind.MetricsQuery,
+])
+
+// Matches the backend Insight.alertable_query_kind, which reads no flag or funnel viz when it deletes alerts on save.
+const alertsSurviveQuery = (query?: Record<string, any> | null): boolean => {
+ let node = query
+ while (node?.source) {
+ node = node.source
+ }
+ return !!node && ALERT_SURVIVING_QUERY_KINDS.has(node.kind)
}
// List only the insight types this account can actually alert on — naming a flag-gated type the
@@ -131,7 +153,6 @@ export function alertsToThresholdGoalLines(alerts: AlertType[]): GoalLine[] {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface insightAlertsLogicValues {
- featureFlags: FeatureFlagsSet // featureFlagLogic
insight: Partial<InsightModel<Node<Record<string, any>>>> // insightLogic
breakdownFilter: BreakdownFilter | null | undefined // insightVizDataLogic
showAlertThresholdLines: boolean | null | undefined // insightVizDataLogic
@@ -252,8 +273,6 @@ export const insightAlertsLogic = kea<insightAlertsLogicType>([
['showAlertThresholdLines', 'breakdownFilter'],
insightLogic(props.insightLogicProps),
['insight'],
- featureFlagLogic,
- ['featureFlags'],
],
})),
@@ -415,10 +434,7 @@ export const insightAlertsLogic = kea<insightAlertsLogicType>([
actions.loadAlertDestinationCounts(values.alerts)
},
setQuery: ({ query }) => {
- const metricsAlertsEnabled = !!values.featureFlags[FEATURE_FLAGS.METRICS]
- actions.setShouldShowAlertDeletionWarning(
- values.alerts.length > 0 && !areAlertsSupportedForInsight(query, { metricsAlertsEnabled })
- )
+ actions.setShouldShowAlertDeletionWarning(values.alerts.length > 0 && !alertsSurviveQuery(query))
},
setShowAlertAnomalyPoints: ({ show }) => {
// When toggling on, reload alerts from the API to get latest check data