Skip to content

Commit

Permalink
add hogQLTable to taxonomic filter's HogQL editor
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Oct 3, 2023
1 parent f4884ed commit 189ba49
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 7 deletions.
8 changes: 6 additions & 2 deletions frontend/src/lib/components/HogQLEditor/HogQLEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { hogQLEditorLogic } from './hogQLEditorLogic'
export interface HogQLEditorProps {
onChange: (value: string) => void
value: string | undefined
hogQLTable?: string
disablePersonProperties?: boolean
disableAutoFocus?: boolean
disableCmdEnter?: boolean
Expand All @@ -20,6 +21,7 @@ let uniqueNode = 0
export function HogQLEditor({
onChange,
value,
hogQLTable,
disablePersonProperties,
disableAutoFocus,
disableCmdEnter,
Expand All @@ -28,7 +30,7 @@ export function HogQLEditor({
}: HogQLEditorProps): JSX.Element {
const [key] = useState(() => `HogQLEditor.${uniqueNode++}`)
const textareaRef = useRef<HTMLTextAreaElement | null>(null)
const logic = hogQLEditorLogic({ key, value, onChange, textareaRef })
const logic = hogQLEditorLogic({ key, value, onChange, hogQLTable, textareaRef })
const { localValue, error, responseLoading } = useValues(logic)
const { setLocalValue, submit } = useActions(logic)

Expand All @@ -53,7 +55,9 @@ export function HogQLEditor({
maxRows={6}
placeholder={
placeholder ??
(disablePersonProperties
(hogQLTable === 'persons'
? "Enter HogQL expression, such as:\n- properties.$geoip_country_name\n- toInt(properties.$browser_version) * 10\n- concat(properties.name, ' <', properties.email, '>')\n- is_identified ? 'user' : 'anon'"
: disablePersonProperties
? "Enter HogQL expression, such as:\n- properties.$current_url\n- toInt(properties.`Long Field Name`) * 10\n- concat(event, ' ', distinct_id)\n- if(1 < 2, 'small', 'large')"
: "Enter HogQL Expression, such as:\n- properties.$current_url\n- person.properties.$geoip_country_name\n- toInt(properties.`Long Field Name`) * 10\n- concat(event, ' ', distinct_id)\n- if(1 < 2, 'small', 'large')")
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lib/components/HogQLEditor/hogQLEditorLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import React from 'react'
export interface HogQLEditorLogicProps {
key: string
value: string | undefined
hogQLTable?: string
onChange: (value: string) => void
textareaRef?: React.MutableRefObject<HTMLTextAreaElement | null>
}
Expand All @@ -32,6 +33,7 @@ export const hogQLEditorLogic = kea<hogQLEditorLogicType>([
const response = await query<HogQLMetadata>({
kind: NodeKind.HogQLMetadata,
expr: values.localValue,
table: props.hogQLTable || 'events',
})
breakpoint()
if (response && Array.isArray(response.errors) && response.errors.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface PropertyFiltersProps {
disablePopover?: boolean
style?: CSSProperties
taxonomicGroupTypes?: TaxonomicFilterGroupType[]
hogQLTable?: string
showNestedArrow?: boolean
eventNames?: string[]
logicalRowDivider?: boolean
Expand All @@ -36,6 +37,7 @@ export function PropertyFilters({
showConditionBadge = false,
disablePopover = false, // use bare PropertyFilter without popover
taxonomicGroupTypes,
hogQLTable,
style = {},
showNestedArrow = false,
eventNames = [],
Expand Down Expand Up @@ -88,6 +90,7 @@ export function PropertyFilters({
onComplete={onComplete}
orFiltering={orFiltering}
taxonomicGroupTypes={taxonomicGroupTypes}
hogQLTable={hogQLTable}
eventNames={eventNames}
propertyGroupType={propertyGroupType}
disablePopover={disablePopover || orFiltering}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function TaxonomicPropertyFilter({
orFiltering,
addText = 'Add filter',
hasRowOperator,
hogQLTable,
}: PropertyFilterInternalProps): JSX.Element {
const pageKey = useMemo(() => pageKeyInput || `filter-${uniqueMemoizedIndex++}`, [pageKeyInput])
const groupTypes = taxonomicGroupTypes || [
Expand Down Expand Up @@ -98,6 +99,7 @@ export function TaxonomicPropertyFilter({
value={cohortOrOtherValue}
onChange={taxonomicOnChange}
taxonomicGroupTypes={groupTypes}
hogQLTable={hogQLTable}
eventNames={eventNames}
/>
)
Expand Down
1 change: 1 addition & 0 deletions frontend/src/lib/components/PropertyFilters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ export interface PropertyFilterInternalProps {
orFiltering?: boolean
addText?: string | null
hasRowOperator?: boolean
hogQLTable?: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export function InfiniteSelectResults({
const RenderComponent = activeTaxonomicGroup?.render

const listComponent = RenderComponent ? (
<RenderComponent value={value} onChange={(newValue) => selectItem(activeTaxonomicGroup, newValue, newValue)} />
<RenderComponent
{...(activeTaxonomicGroup?.componentProps ?? {})}
value={value}
onChange={(newValue) => selectItem(activeTaxonomicGroup, newValue, newValue)}
/>
) : (
<InfiniteList />
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { HogQLEditor } from 'lib/components/HogQLEditor/HogQLEditor'
export interface InlineHogQLEditorProps {
value?: TaxonomicFilterValue
onChange: (value: TaxonomicFilterValue) => void
hogQLTable?: string
}

export function InlineHogQLEditor({ value, onChange }: InlineHogQLEditorProps): JSX.Element {
export function InlineHogQLEditor({ value, onChange, hogQLTable }: InlineHogQLEditorProps): JSX.Element {
return (
<div className="px-2">
<HogQLEditor
onChange={onChange}
value={String(value ?? '')}
hogQLTable={hogQLTable}
submitText={value ? 'Update HogQL expression' : 'Add HogQL expression'}
disableAutoFocus // :TRICKY: No autofocus here. It's controlled in the TaxonomicFilter.
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function TaxonomicFilter({
excludedProperties,
popoverEnabled = true,
selectFirstItem = true,
hogQLTable = 'events',
}: TaxonomicFilterProps): JSX.Element {
// Generate a unique key for each unique TaxonomicFilter that's rendered
const taxonomicFilterLogicKey = useMemo(
Expand All @@ -50,8 +51,8 @@ export function TaxonomicFilter({
popoverEnabled,
selectFirstItem,
excludedProperties,
hogQLTable,
}

const logic = taxonomicFilterLogic(taxonomicFilterLogicProps)
const { searchQuery, searchPlaceholder, activeTab } = useValues(logic)
const { setSearchQuery, moveUp, moveDown, tabLeft, tabRight, selectSelected } = useActions(logic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const taxonomicFilterLogic = kea<taxonomicFilterLogicType>({
(taxonomicFilterLogicKey) => taxonomicFilterLogicKey,
],
eventNames: [() => [(_, props) => props.eventNames], (eventNames) => eventNames ?? []],
hogQLTable: [() => [(_, props) => props.hogQLTable], (hogQLTable) => hogQLTable ?? 'events'],
excludedProperties: [
() => [(_, props) => props.excludedProperties],
(excludedProperties) => excludedProperties ?? {},
Expand All @@ -146,16 +147,18 @@ export const taxonomicFilterLogic = kea<taxonomicFilterLogicType>({
s.groupAnalyticsTaxonomicGroups,
s.groupAnalyticsTaxonomicGroupNames,
s.eventNames,
s.hogQLTable,
s.excludedProperties,
],
(
teamId,
groupAnalyticsTaxonomicGroups,
groupAnalyticsTaxonomicGroupNames,
eventNames,
hogQLTable,
excludedProperties
): TaxonomicFilterGroup[] => {
const groups = [
const groups: TaxonomicFilterGroup[] = [
{
name: 'Events',
searchPlaceholder: 'events',
Expand Down Expand Up @@ -429,6 +432,7 @@ export const taxonomicFilterLogic = kea<taxonomicFilterLogicType>({
type: TaxonomicFilterGroupType.HogQLExpression,
render: InlineHogQLEditor,
getPopoverHeader: () => 'HogQL',
componentProps: { hogQLTable },
},
...groupAnalyticsTaxonomicGroups,
...groupAnalyticsTaxonomicGroupNames,
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lib/components/TaxonomicFilter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface TaxonomicFilterProps {
selectFirstItem?: boolean
/** use to filter results in a group by name, currently only working for EventProperties */
excludedProperties?: { [key in TaxonomicFilterGroupType]?: TaxonomicFilterValue[] }
hogQLTable?: string
}

export interface TaxonomicFilterLogicProps extends TaxonomicFilterProps {
Expand Down Expand Up @@ -56,6 +57,7 @@ export interface TaxonomicFilterGroup {
groupTypeIndex?: number
getFullDetailUrl?: (instance: any) => string
excludedProperties?: string[]
componentProps?: Record<string, any>
}

export enum TaxonomicFilterGroupType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface TaxonomicPopoverProps<ValueType extends TaxonomicFilterValue =
allowClear?: boolean
style?: React.CSSProperties
excludedProperties?: { [key in TaxonomicFilterGroupType]?: TaxonomicFilterValue[] }
hogQLTable?: string
}

/** Like TaxonomicPopover, but convenient when you know you will only use string values */
Expand All @@ -45,6 +46,7 @@ export function TaxonomicPopover<ValueType extends TaxonomicFilterValue = Taxono
placeholderClass = 'text-muted',
allowClear = false,
excludedProperties,
hogQLTable,
...buttonPropsRest
}: TaxonomicPopoverProps<ValueType>): JSX.Element {
const [localValue, setLocalValue] = useState<ValueType>(value || ('' as ValueType))
Expand Down Expand Up @@ -85,6 +87,7 @@ export function TaxonomicPopover<ValueType extends TaxonomicFilterValue = Taxono
}}
taxonomicGroupTypes={groupTypes ?? [groupType]}
eventNames={eventNames}
hogQLTable={hogQLTable}
excludedProperties={excludedProperties}
/>
}
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/queries/nodes/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export function DataTable({ uniqueKey, query, setQuery, context, cachedResults }
? columnsInResponse ?? columnsInQuery
: columnsInQuery
const groupTypes = isPersonsQuery(query.source) ? personGroupTypes : eventGroupTypes
const hogQLTable = isPersonsQuery(query.source) ? 'persons' : 'events'

const lemonColumns: LemonTableColumn<DataTableRow, any>[] = [
...columnsInLemonTable.map((key, index) => ({
Expand Down Expand Up @@ -171,6 +172,7 @@ export function DataTable({ uniqueKey, query, setQuery, context, cachedResults }
<TaxonomicPopover
groupType={TaxonomicFilterGroupType.HogQLExpression}
groupTypes={groupTypes}
hogQLTable={hogQLTable}
value={key}
renderValue={() => <>Edit column</>}
type="tertiary"
Expand Down Expand Up @@ -249,6 +251,7 @@ export function DataTable({ uniqueKey, query, setQuery, context, cachedResults }
<TaxonomicPopover
groupType={TaxonomicFilterGroupType.HogQLExpression}
groupTypes={groupTypes}
hogQLTable={hogQLTable}
value={''}
placeholder={<span className="not-italic">Add column left</span>}
data-attr="datatable-add-column-left"
Expand Down Expand Up @@ -280,6 +283,7 @@ export function DataTable({ uniqueKey, query, setQuery, context, cachedResults }
<TaxonomicPopover
groupType={TaxonomicFilterGroupType.HogQLExpression}
groupTypes={groupTypes}
hogQLTable={hogQLTable}
value={''}
placeholder={<span className="not-italic">Add column right</span>}
data-attr="datatable-add-column-right"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export function PersonPropertyFilters({ query, setQuery }: PersonPropertyFilters
}
}}
pageKey={`PersonPropertyFilters.${id}`}
taxonomicGroupTypes={[TaxonomicFilterGroupType.PersonProperties]}
taxonomicGroupTypes={[TaxonomicFilterGroupType.PersonProperties, TaxonomicFilterGroupType.HogQLExpression]}
hogQLTable="persons"
style={{ marginBottom: 0, marginTop: 0 }}
/>
) : (
Expand Down

0 comments on commit 189ba49

Please sign in to comment.