Skip to content

Commit

Permalink
fix(cohort): Fix creating static cohort from SQL (#27676)
Browse files Browse the repository at this point in the history
  • Loading branch information
timgl authored Jan 20, 2025
1 parent 29d4034 commit 95798a2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 49 deletions.
91 changes: 45 additions & 46 deletions frontend/src/scenes/insights/InsightPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,11 @@ export function InsightPageHeader({ insightLogicProps }: { insightLogicProps: In
label="Debug panel"
/>
) : null}
{hogQL && (
<>
<LemonDivider />

{(hogQL || showCohortButton) && <LemonDivider />}
{hogQL &&
!isHogQLQuery(query) &&
!(isDataVisualizationNode(query) && isHogQLQuery(query.source)) && (
<LemonButton
data-attr="edit-insight-sql"
onClick={() => {
Expand All @@ -263,50 +265,47 @@ export function InsightPageHeader({ insightLogicProps }: { insightLogicProps: In
>
Edit SQL directly
</LemonButton>
{showCohortButton && (
<LemonButton
data-attr="edit-insight-sql"
onClick={() => {
LemonDialog.openForm({
title: 'Save as static cohort',
description: (
<div className="mt-2">
Your query must export a <code>person_id</code>,{' '}
<code>actor_id</code> or <code>id</code> column,
which must match the <code>id</code> of the{' '}
<code>persons</code> table
</div>
),
initialValues: {
name: '',
},
content: (
<LemonField name="name">
<LemonInput
data-attr="insight-name"
placeholder="Name of the new cohort"
autoFocus
/>
</LemonField>
),
errors: {
name: (name) =>
!name ? 'You must enter a name' : undefined,
},
onSubmit: async ({ name }) => {
createStaticCohort(name, {
kind: NodeKind.HogQLQuery,
query: hogQL,
})
},
)}
{hogQL && showCohortButton && (
<LemonButton
data-attr="edit-insight-sql"
onClick={() => {
LemonDialog.openForm({
title: 'Save as static cohort',
description: (
<div className="mt-2">
Your query must export a <code>person_id</code>,{' '}
<code>actor_id</code> or <code>id</code> column, which must
match the <code>id</code> of the <code>persons</code> table
</div>
),
initialValues: {
name: '',
},
content: (
<LemonField name="name">
<LemonInput
data-attr="insight-name"
placeholder="Name of the new cohort"
autoFocus
/>
</LemonField>
),
errors: {
name: (name) => (!name ? 'You must enter a name' : undefined),
},
onSubmit: async ({ name }) => {
createStaticCohort(name, {
kind: NodeKind.HogQLQuery,
query: hogQL,
})
}}
fullWidth
>
Save as static cohort
</LemonButton>
)}
</>
},
})
}}
fullWidth
>
Save as static cohort
</LemonButton>
)}
{hasDashboardItemId && (
<>
Expand Down
14 changes: 11 additions & 3 deletions frontend/src/scenes/insights/insightDataLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { insightVizDataNodeKey } from '~/queries/nodes/InsightViz/InsightViz'
import { getDefaultQuery, queryFromKind } from '~/queries/nodes/InsightViz/utils'
import { queryExportContext } from '~/queries/query'
import { DataVisualizationNode, InsightVizNode, Node, NodeKind } from '~/queries/schema/schema-general'
import { isDataTableNode, isDataVisualizationNode, isHogQuery, isInsightVizNode } from '~/queries/utils'
import { isDataTableNode, isDataVisualizationNode, isHogQLQuery, isHogQuery, isInsightVizNode } from '~/queries/utils'
import { ExportContext, InsightLogicProps, InsightType } from '~/types'

import { teamLogic } from '../teamLogic'
Expand Down Expand Up @@ -171,8 +171,16 @@ export const insightDataLogic = kea<insightDataLogicType>([
],

hogQL: [
(s) => [s.insightData],
(insightData): string | null => {
(s) => [s.insightData, s.query],
(insightData, query): string | null => {
// Try to get it from the query itself, so we don't have to wait for the response
if (isDataVisualizationNode(query) && isHogQLQuery(query.source)) {
return query.source.query
}
if (isHogQLQuery(query)) {
return query.query
}
// Otherwise, get it from the response
if (insightData && 'hogql' in insightData && insightData.hogql !== '') {
return insightData.hogql
}
Expand Down

0 comments on commit 95798a2

Please sign in to comment.