Skip to content

Commit

Permalink
Add export CSV button that uses HogQL query export
Browse files Browse the repository at this point in the history
  • Loading branch information
webjunkie committed Jan 8, 2024
1 parent 57a7f0c commit b0dc7d3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
8 changes: 4 additions & 4 deletions frontend/src/queries/nodes/DataTable/DataTableExport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import {
} from '~/queries/nodes/DataTable/utils'
import { getPersonsEndpoint } from '~/queries/query'
import { DataNode, DataTableNode, NodeKind } from '~/queries/schema'
import { isEventsQuery, isHogQLQuery, isPersonsNode } from '~/queries/utils'
import { isActorsQuery, isEventsQuery, isHogQLQuery, isPersonsNode } from '~/queries/utils'
import { ExporterFormat } from '~/types'

import { dataTableLogic, DataTableRow } from './dataTableLogic'

const EXPORT_MAX_LIMIT = 10000
export const EXPORT_MAX_LIMIT = 10000

async function startDownload(query: DataTableNode, onlySelectedColumns: boolean): Promise<void> {
export async function startDownload(query: DataTableNode, onlySelectedColumns: boolean): Promise<void> {
const exportContext = isPersonsNode(query.source)
? { path: getPersonsEndpoint(query.source) }
: { source: query.source }
Expand All @@ -33,7 +33,7 @@ async function startDownload(query: DataTableNode, onlySelectedColumns: boolean)

if (onlySelectedColumns) {
exportContext['columns'] = (
(isEventsQuery(query.source) ? query.source.select : null) ??
(isEventsQuery(query.source) || isActorsQuery(query.source) ? query.source.select : null) ??
query.columns ??
defaultDataTableColumns(query.source.kind)
)?.filter((c) => c !== 'person.$delete')
Expand Down
55 changes: 41 additions & 14 deletions frontend/src/scenes/retention/RetentionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { RetentionTableAppearanceType } from 'scenes/retention/types'
import { MissingPersonsAlert } from 'scenes/trends/persons-modal/PersonsModal'
import { urls } from 'scenes/urls'

import { EXPORT_MAX_LIMIT, startDownload } from '~/queries/nodes/DataTable/DataTableExport'
import { ExportWithConfirmation } from '~/queries/nodes/DataTable/ExportWithConfirmation'
import { DataTableNode, NodeKind } from '~/queries/schema'
import { ExporterFormat } from '~/types'

import { retentionLogic } from './retentionLogic'
Expand All @@ -25,9 +28,18 @@ export function RetentionModal(): JSX.Element | null {
const { results } = useValues(retentionLogic(insightProps))
const { people, peopleLoading, peopleLoadingMore } = useValues(retentionPeopleLogic(insightProps))
const { loadMorePeople } = useActions(retentionPeopleLogic(insightProps))
const { aggregationTargetLabel, selectedInterval, exploreUrl } = useValues(retentionModalLogic(insightProps))
const { aggregationTargetLabel, selectedInterval, exploreUrl, actorsQuery } = useValues(
retentionModalLogic(insightProps)
)
const { closeModal } = useActions(retentionModalLogic(insightProps))

const dataTableNodeQuery: DataTableNode | undefined = actorsQuery
? {
kind: NodeKind.DataTableNode,
source: actorsQuery,
}
: undefined

if (!results || selectedInterval === null) {
return null
}
Expand All @@ -41,19 +53,34 @@ export function RetentionModal(): JSX.Element | null {
footer={
<div className="flex justify-between gap-2 w-full">
<div className="flex gap-2">
<LemonButton
type="secondary"
onClick={() =>
void triggerExport({
export_format: ExporterFormat.CSV,
export_context: {
path: row?.people_url,
},
})
}
>
Download CSV
</LemonButton>
{!exploreUrl && (
<LemonButton
type="secondary"
onClick={() =>
void triggerExport({
export_format: ExporterFormat.CSV,
export_context: {
path: row?.people_url,
},
})
}
>
Download CSV
</LemonButton>
)}
{!!dataTableNodeQuery && (
<ExportWithConfirmation
key={1}
placement={'topRight'}
onConfirm={() => {
dataTableNodeQuery && void startDownload(dataTableNodeQuery, true)
}}
actor={'persons'}
limit={EXPORT_MAX_LIMIT}
>
<LemonButton type="secondary">Export all as CSV</LemonButton>
</ExportWithConfirmation>
)}
</div>
{exploreUrl && (
<LemonButton
Expand Down

0 comments on commit b0dc7d3

Please sign in to comment.