: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
perf(alerts): load insight alerts only where alerts show (#126308)
Paul D'Ambraa96c4863b6af
diff --git a/frontend/src/scenes/insights/insightLogic.tsx b/frontend/src/scenes/insights/insightLogic.tsxindex ca36f0372108..189c923e6665 100644--- a/frontend/src/scenes/insights/insightLogic.tsx+++ b/frontend/src/scenes/insights/insightLogic.tsx@@ -31,6 +31,7 @@ import { InsightEventSource, sanitizeInsight, sanitizeQuery } from 'lib/utils/evimport { isEmptyObject, isObject } from 'lib/utils/guards'import { objectsEqual } from 'lib/utils/objects'import { RetainedValues } from 'lib/utils/retainedValues'+import { retryImport } from 'lib/utils/retryImport'import { withBackLink } from 'scenes/analytics/analyticsBackLink'import { isDashboardFilterOverrideEmpty } from 'scenes/dashboard/dashboardFilterEmpty'import { liveDashboardIds } from 'scenes/dashboard/liveDashboardIds'@@ -75,7 +76,6 @@ import {SetInsightOptions,} from '~/types'-import { insightAlertsLogic } from 'products/alerts/frontend/logic/insightAlertsLogic'import {titleMentionsMCP,tryShowMCPAnalyticsNudge,@@ -135,6 +135,18 @@ function getRetainedInsightForProps(props: InsightLogicProps): Partial<InsightMoreturn props.retainResponseOnUnmount && props.dashboardItemId ? retainedInsights.get(props.dashboardItemId) : null}+// A mounted insightAlertsLogic means its module is already loaded, so the import fetches nothing new.+// A failed import means no insightAlertsLogic is mounted, so there are no alerts to refresh.+async function refreshMountedInsightAlerts(props: InsightLogicProps, insightId: number | undefined): Promise<void> {+ const alertsModule = await retryImport(() => import('products/alerts/frontend/logic/insightAlertsLogic')).catch(+ () => null+ )+ const alertsLogic = alertsModule?.insightAlertsLogic.findMounted({ insightLogicProps: props, insightId })+ if (alertsLogic && alertsLogic.values.alerts.length > 0) {+ alertsLogic.actions.loadAlerts()+ }+}+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface insightLogicValues {cohortsById: Partial<Record<number | string, CohortType>> // cohortsModel@@ -1198,16 +1210,7 @@ export const insightLogic: LogicWrapper<insightLogicType> = kea<insightLogicType})actions.reloadSavedInsights() // Load insights afresh- const alertsLogic = insightAlertsLogic.findMounted({- insightLogicProps: props,- insightId: insightNumericId,- })-- if (alertsLogic != null) {- if (alertsLogic.values.alerts.length > 0) {- alertsLogic.actions.loadAlerts()- }- }+ void refreshMountedInsightAlerts(props, insightNumericId)if (!insightNumericId) {// saving the new insight consumes its draft; updating an existing insight// must leave the (unrelated) draft alonediff --git a/products/alerts/frontend/logic/insightAlertsLogic.test.ts b/products/alerts/frontend/logic/insightAlertsLogic.test.tsindex fd8fde8c38a8..626b8b345f58 100644--- a/products/alerts/frontend/logic/insightAlertsLogic.test.ts+++ b/products/alerts/frontend/logic/insightAlertsLogic.test.ts@@ -6,10 +6,11 @@ import { featureFlagLogic } from 'lib/logic/featureFlagLogic'import { insightDataLogic } from 'scenes/insights/insightDataLogic'import { createEmptyInsight, insightLogic } from 'scenes/insights/insightLogic'import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'+import { insightsApi } from 'scenes/insights/utils/api'import { Node, NodeKind } from '~/queries/schema/schema-general'import { initKeaTests } from '~/test/init'-import { ChartDisplayType, HogFunctionType, InsightLogicProps, InsightShortId } from '~/types'+import { ChartDisplayType, HogFunctionType, InsightLogicProps, InsightModel, InsightShortId } from '~/types'import type { AlertType } from '../types'import {@@ -106,6 +107,31 @@ describe('insightAlertsLogic', () => {expect(hogFunctionsListSpy).not.toHaveBeenCalled()})+ it('reloads alerts after the insight saves', async () => {+ const updateSpy = jest+ .spyOn(insightsApi, 'update')+ .mockResolvedValue({ ...createEmptyInsight(Insight42), id: 42 } as InsightModel)+ const insightLogicProps: InsightLogicProps = {+ dashboardItemId: Insight42,+ dashboardId: 1,+ cachedInsight: {+ ...createEmptyInsight(Insight42),+ id: 42,+ query: API_QUERY,+ alerts: [{ id: 'alert-a', name: 'Inline' }] as AlertType[],+ },+ }+ mountInsightStack(insightLogicProps)+ const alertsLogic = insightAlertsLogic({ insightId: 42, insightLogicProps, deferInitialAlertsLoad: true })+ alertsLogic.mount()++ insightLogic(insightLogicProps).actions.saveInsight()++ await expectLogic(alertsLogic).toDispatchActions(['loadAlerts', 'loadAlertsSuccess'])+ expect(listSpy).toHaveBeenCalled()+ updateSpy.mockRestore()+ })+it('does not fetch on a deferred mount when the cached insight has no alerts field', async () => {const insightLogicProps: InsightLogicProps = {dashboardItemId: Insight42,