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(insights): humanize query changes in insight activity log (#73816)

Thomas Obermüllerbdd5c9e73ea6
diff --git a/frontend/src/lib/components/ActivityLog/activityLogLogic.insight.test.tsx b/frontend/src/lib/components/ActivityLog/activityLogLogic.insight.test.tsx
index 9a0d3d33759d..f2f42c047f09 100644
--- a/frontend/src/lib/components/ActivityLog/activityLogLogic.insight.test.tsx
+++ b/frontend/src/lib/components/ActivityLog/activityLogLogic.insight.test.tsx
@@ -86,105 +86,162 @@ describe('the activity log logic', () => {
expect(renderedDescription).toHaveTextContent('peter changed query definition on test insight')
})
- it('can handle change of insight query', async () => {
- const insightMock = {
- type: ActivityScope.INSIGHT,
- action: 'changed',
- field: 'query',
- after: {
- kind: 'TrendsQuery',
- properties: {
- type: 'AND',
+ const trendsQuery = {
+ kind: 'TrendsQuery',
+ properties: {
+ type: 'AND',
+ values: [
+ {
+ type: 'OR',
values: [
{
- type: 'OR',
- values: [
- {
- type: 'event',
- key: '$current_url',
- operator: 'exact',
- value: ['https://hedgebox.net/files/'],
- },
- {
- type: 'event',
- key: '$geoip_country_code',
- operator: 'exact',
- value: ['US', 'AU'],
- },
- ],
+ type: 'event',
+ key: '$current_url',
+ operator: 'exact',
+ value: ['https://hedgebox.net/files/'],
+ },
+ {
+ type: 'event',
+ key: '$geoip_country_code',
+ operator: 'exact',
+ value: ['US', 'AU'],
},
],
},
- filterTestAccounts: false,
- interval: 'day',
- dateRange: {
- date_from: '-7d',
- },
- series: [
+ ],
+ },
+ filterTestAccounts: false,
+ interval: 'day',
+ dateRange: {
+ date_from: '-7d',
+ },
+ series: [
+ {
+ kind: 'EventsNode',
+ name: '$pageview',
+ custom_name: 'Views',
+ event: '$pageview',
+ properties: [
{
- kind: 'EventsNode',
- name: '$pageview',
- custom_name: 'Views',
- event: '$pageview',
- properties: [
- {
- type: 'event',
- key: '$browser',
- operator: 'exact',
- value: 'Chrome',
- },
- {
- type: 'cohort',
- key: 'id',
- operator: 'in',
- value: 2,
- },
- ],
- limit: 100,
+ type: 'event',
+ key: '$browser',
+ operator: 'exact',
+ value: 'Chrome',
+ },
+ {
+ type: 'cohort',
+ key: 'id',
+ operator: 'in',
+ value: 2,
},
],
- trendsFilter: {
- display: 'ActionsAreaGraph',
- },
- breakdownFilter: {
- breakdown: '$geoip_country_code',
- breakdown_type: 'event',
- },
+ limit: 100,
},
- }
-
- let logic = await insightTestSetup('test insight', 'updated', [insightMock as any])
- let actual = logic.values.humanizedActivity
+ ],
+ trendsFilter: {
+ display: 'ActionsAreaGraph',
+ },
+ breakdownFilter: {
+ breakdown: '$geoip_country_code',
+ breakdown_type: 'event',
+ },
+ }
+
+ it.each([
+ ['a bare query node', trendsQuery],
+ ['a query wrapped in an InsightVizNode', { kind: 'InsightVizNode', source: trendsQuery }],
+ ])('can handle change of insight query as %s', async (_label, after) => {
+ const logic = await insightTestSetup('test insight', 'updated', [
+ {
+ type: ActivityScope.INSIGHT,
+ action: 'changed',
+ field: 'query',
+ after,
+ } as any,
+ ])
+ const actual = logic.values.humanizedActivity
- let renderedDescription = render(<>{actual[0].description}</>).container
+ const renderedDescription = render(<>{actual[0].description}</>).container
expect(renderedDescription).toHaveTextContent('peter changed query definition on test insight')
- let renderedExtendedDescription = render(<>{actual[0].extendedDescription}</>).container
+ const renderedExtendedDescription = render(<>{actual[0].extendedDescription}</>).container
expect(renderedExtendedDescription).toHaveTextContent(
"QueryACounting \"Views\"Pageviewby total countwhere event'sBrowser= equals Chromeand person belongs to cohortUser in ID 2FiltersEvent'sCurrent URL= equals https://hedgebox.net/files/or event'sCountry code= equals US or AUBreakdown byCountry code"
)
- ;(insightMock.after.breakdownFilter as BreakdownFilter) = {
- breakdowns: [
- {
- property: '$geoip_country_code',
- type: 'event',
- },
- {
- property: '$session_duration',
- type: 'session',
+ })
+
+ it('can handle change of insight query with multiple breakdowns', async () => {
+ const logic = await insightTestSetup('test insight', 'updated', [
+ {
+ type: ActivityScope.INSIGHT,
+ action: 'changed',
+ field: 'query',
+ after: {
+ ...trendsQuery,
+ breakdownFilter: {
+ breakdowns: [
+ {
+ property: '$geoip_country_code',
+ type: 'event',
+ },
+ {
+ property: '$session_duration',
+ type: 'session',
+ },
+ ],
+ } as BreakdownFilter,
},
- ],
- }
+ } as any,
+ ])
+ const actual = logic.values.humanizedActivity
- logic = await insightTestSetup('test insight', 'updated', [insightMock as any])
- actual = logic.values.humanizedActivity
+ const renderedDescription = render(<>{actual[0].description}</>).container
+ expect(renderedDescription).toHaveTextContent('peter changed query definition on test insight')
- renderedDescription = render(<>{actual[0].description}</>).container
+ const renderedExtendedDescription = render(<>{actual[0].extendedDescription}</>).container
+ expect(renderedExtendedDescription).toHaveTextContent('Breakdown byCountry codeSession duration')
+ })
+
+ it('can handle change of a SQL insight query', async () => {
+ const logic = await insightTestSetup('test insight', 'updated', [
+ {
+ type: ActivityScope.INSIGHT,
+ action: 'changed',
+ field: 'query',
+ after: {
+ kind: 'DataVisualizationNode',
+ source: {
+ kind: 'HogQLQuery',
+ query: 'select event from events',
+ },
+ },
+ } as any,
+ ])
+ const actual = logic.values.humanizedActivity
+
+ const renderedDescription = render(<>{actual[0].description}</>).container
expect(renderedDescription).toHaveTextContent('peter changed query definition on test insight')
- renderedExtendedDescription = render(<>{actual[0].extendedDescription}</>).container
- expect(renderedExtendedDescription).toHaveTextContent(
- "QueryACounting \"Views\"Pageviewby total countwhere event'sBrowser= equals Chromeand person belongs to cohortUser in ID 2FiltersEvent'sCurrent URL= equals https://hedgebox.net/files/or event'sCountry code= equals US or AUBreakdown byCountry code"
+ const renderedExtendedDescription = render(<>{actual[0].extendedDescription}</>).container
+ expect(renderedExtendedDescription).toHaveTextContent('select event from events')
+ })
+
+ it('can handle change of a query it cannot summarize', async () => {
+ const logic = await insightTestSetup('test insight', 'updated', [
+ {
+ type: ActivityScope.INSIGHT,
+ action: 'changed',
+ field: 'query',
+ after: {
+ kind: 'DataTableNode',
+ source: { kind: 'EventsQuery', select: ['*'] },
+ },
+ } as any,
+ ])
+ const actual = logic.values.humanizedActivity
+
+ expect(render(<>{actual[0].description}</>).container).toHaveTextContent(
+ 'peter changed the query on test insight'
)
})
diff --git a/frontend/src/scenes/saved-insights/activityDescriptions.tsx b/frontend/src/scenes/saved-insights/activityDescriptions.tsx
index dd2d4db26ff3..39417818d666 100644
--- a/frontend/src/scenes/saved-insights/activityDescriptions.tsx
+++ b/frontend/src/scenes/saved-insights/activityDescriptions.tsx
@@ -24,9 +24,14 @@ import { pluralize } from 'lib/utils/strings'
import { urls } from 'scenes/urls'
import { filtersToQueryNode } from '~/queries/nodes/InsightQuery/utils/filtersToQueryNode'
-import { queryNodeToFilter } from '~/queries/nodes/InsightQuery/utils/queryNodeToFilter'
-import { InsightQueryNode, QuerySchema, TrendsQuery } from '~/queries/schema/schema-general'
-import { isInsightQueryNode, hasBreakdownFilter } from '~/queries/utils'
+import { HogQLQuery, InsightQueryNode, QuerySchema } from '~/queries/schema/schema-general'
+import {
+ isDataTableNodeWithHogQLQuery,
+ isDataVisualizationNode,
+ isHogQLQuery,
+ isInsightQueryNode,
+ isInsightVizNode,
+} from '~/queries/utils'
import { FilterType, InsightModel, InsightShortId } from '~/types'
const nameOrLinkToInsight = (short_id?: InsightShortId | null, name?: string | null): string | JSX.Element => {
@@ -78,7 +83,11 @@ const insightActionsMapping: Record<
filters: function onChangedFilter(change) {
const filtersAfter = change?.after as Partial<FilterType>
- return areObjectValuesEmpty(filtersAfter) ? null : summarizeChanges(filtersAfter)
+ return areObjectValuesEmpty(filtersAfter)
+ ? null
+ : summarizeQueryChanges(
+ filtersToQueryNode(filtersAfter, { source: 'saved_insights_activity_descriptions' })
+ )
},
query: function onChangedQuery(change) {
if (change?.action === 'deleted') {
@@ -87,9 +96,17 @@ const insightActionsMapping: Record<
}
const queryAfter = change?.after as QuerySchema
- return isInsightQueryNode(queryAfter)
- ? summarizeChanges(queryNodeToFilter(change?.after as InsightQueryNode))
- : { description: ["cannot yet summarize changes to this insight's query: " + queryAfter?.kind] }
+ // saved insights store the actual query wrapped in an InsightVizNode (or in a
+ // DataVisualizationNode / DataTableNode for SQL insights), so summarize the source
+ const source =
+ isInsightVizNode(queryAfter) ||
+ isDataVisualizationNode(queryAfter) ||
+ isDataTableNodeWithHogQLQuery(queryAfter)
+ ? queryAfter.source
+ : queryAfter
+ return isInsightQueryNode(source) || isHogQLQuery(source)
+ ? summarizeQueryChanges(source)
+ : { description: ['changed the query'] }
},
deleted: function onSoftDelete(change, logItem, asNotification) {
const isDeleted = detectBoolean(change?.after)
@@ -244,19 +261,14 @@ const insightActionsMapping: Record<
filter_override_context: () => null,
}
-function summarizeChanges(filtersAfter: Partial<FilterType>): ChangeMapping | null {
- const query = filtersToQueryNode(filtersAfter, {
- source: 'saved_insights_activity_descriptions',
- })
- const trendsQuery = query as TrendsQuery
-
+function summarizeQueryChanges(query: InsightQueryNode | HogQLQuery): ChangeMapping {
return {
description: ['changed query definition'],
extendedDescription: (
<div className="ActivityDescription">
<SeriesSummary query={query} />
- <PropertiesSummary properties={query.properties} />
- {hasBreakdownFilter(trendsQuery?.breakdownFilter) && <InsightBreakdownSummary query={query} />}
+ <PropertiesSummary properties={isHogQLQuery(query) ? query.filters?.properties : query.properties} />
+ <InsightBreakdownSummary query={query} />
</div>
),
}