Skip to content

Commit

Permalink
make separate property type in popup
Browse files Browse the repository at this point in the history
  • Loading branch information
EDsCODE committed Mar 26, 2024
1 parent ff681c7 commit 7244ee2
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { propertyFilterLogic } from 'lib/components/PropertyFilters/propertyFilt
import { PropertyFilterInternalProps } from 'lib/components/PropertyFilters/types'
import {
isGroupPropertyFilter,
isPersonPropertyFilter,
isPropertyFilterWithOperator,
PROPERTY_FILTER_TYPE_TO_TAXONOMIC_FILTER_GROUP_TYPE,
propertyFilterTypeToTaxonomicFilterType,
Expand Down Expand Up @@ -63,7 +64,7 @@ export function TaxonomicPropertyFilter({
value,
item
) => {
selectItem(taxonomicGroup, value, item?.propertyFilterType)
selectItem(taxonomicGroup, value, item)
if (
taxonomicGroup.type === TaxonomicFilterGroupType.Cohorts ||
taxonomicGroup.type === TaxonomicFilterGroupType.HogQLExpression
Expand Down Expand Up @@ -217,6 +218,7 @@ export function TaxonomicPropertyFilter({
...(isGroupPropertyFilter(filter)
? { group_type_index: filter.group_type_index }
: {}),
...(isPersonPropertyFilter(filter) ? { table: filter?.table } : {}),
} as AnyPropertyFilter)
}
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@ export const taxonomicPropertyFilterLogic = kea<taxonomicPropertyFilterLogicType
],
})),
actions({
selectItem: (
taxonomicGroup: TaxonomicFilterGroup,
propertyKey?: TaxonomicFilterValue,
itemPropertyFilterType?: PropertyFilterType
) => ({
selectItem: (taxonomicGroup: TaxonomicFilterGroup, propertyKey?: TaxonomicFilterValue, item?: any) => ({
taxonomicGroup,
propertyKey,
itemPropertyFilterType,
item,
}),
openDropdown: true,
closeDropdown: true,
Expand Down Expand Up @@ -93,7 +89,8 @@ export const taxonomicPropertyFilterLogic = kea<taxonomicPropertyFilterLogicType
],
}),
listeners(({ actions, values, props }) => ({
selectItem: ({ taxonomicGroup, propertyKey, itemPropertyFilterType }) => {
selectItem: ({ taxonomicGroup, propertyKey, item }) => {
const itemPropertyFilterType = item?.propertyFilterType as PropertyFilterType
const propertyType = itemPropertyFilterType ?? taxonomicFilterTypeToPropertyFilterType(taxonomicGroup.type)
if (propertyKey && propertyType) {
if (propertyType === PropertyFilterType.Cohort) {
Expand Down Expand Up @@ -142,6 +139,7 @@ export const taxonomicPropertyFilterLogic = kea<taxonomicPropertyFilterLogicType
operator,
type: propertyType as AnyPropertyFilter['type'] as any, // bad | pipe chain :(
group_type_index: taxonomicGroup.groupTypeIndex,
table: item.table,
}
props.propertyFilterLogic.actions.setFilter(props.filterIndex, property)
}
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/lib/components/PropertyFilters/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ export function taxonomicFilterTypeToPropertyFilterType(
return PropertyFilterType.DataWarehouse
}

if (filterType == TaxonomicFilterGroupType.DataWarehousePersonProperties) {
return PropertyFilterType.Person
}

return Object.entries(propertyFilterMapping).find(([, v]) => v === filterType)?.[0] as
| PropertyFilterType
| undefined
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/lib/components/TaxonomicFilter/InfiniteList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ export function InfiniteList({ popupAnchorElement }: InfiniteListProps): JSX.Ele
<div
{...commonDivProps}
data-attr={`prop-filter-${listGroupType}-${rowIndex}`}
onClick={() => canSelectItem(listGroupType) && selectItem(group, itemValue ?? null, item)}
onClick={() => {
return canSelectItem(listGroupType) && selectItem(group, itemValue ?? null, item)
}}
>
{renderItemContents({
item,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { IconCohort } from 'lib/lemon-ui/icons'
import { CORE_FILTER_DEFINITIONS_BY_GROUP } from 'lib/taxonomy'
import { capitalizeFirstLetter, pluralize, toParams } from 'lib/utils'
import { getEventDefinitionIcon, getPropertyDefinitionIcon } from 'scenes/data-management/events/DefinitionHeader'
import { dataWarehouseJoinsLogic } from 'scenes/data-warehouse/external/dataWarehouseJoinsLogic'
import { dataWarehouseSceneLogic } from 'scenes/data-warehouse/external/dataWarehouseSceneLogic'
import { DataWarehouseTableType } from 'scenes/data-warehouse/types'
import { experimentsLogic } from 'scenes/experiments/experimentsLogic'
Expand Down Expand Up @@ -86,6 +87,8 @@ export const taxonomicFilterLogic = kea<taxonomicFilterLogicType>([
['allGroupProperties'],
dataWarehouseSceneLogic,
['externalTables'],
dataWarehouseJoinsLogic,
['columnsJoinedToPersons'],
],
}),
actions(() => ({
Expand Down Expand Up @@ -225,6 +228,16 @@ export const taxonomicFilterLogic = kea<taxonomicFilterLogicType>([
getPopoverHeader: () => 'Data Warehouse Column',
getIcon: () => <IconServer />,
},
{
name: 'Data Warehouse Person Properties',
searchPlaceholder: 'person properties from data warehouse tables',
type: TaxonomicFilterGroupType.DataWarehousePersonProperties,
logic: dataWarehouseJoinsLogic,
value: 'columnsJoinedToPersons',
getName: (personProperty: PersonProperty) => personProperty.name,
getValue: (personProperty: PersonProperty) => personProperty.id,
getPopoverHeader: () => 'Data Warehouse Person Property',
},
{
name: 'Autocapture elements',
searchPlaceholder: 'autocapture elements',
Expand Down
1 change: 1 addition & 0 deletions frontend/src/lib/components/TaxonomicFilter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export enum TaxonomicFilterGroupType {
CohortsWithAllUsers = 'cohorts_with_all',
DataWarehouse = 'data_warehouse',
DataWarehouseProperties = 'data_warehouse_properties',
DataWarehousePersonProperties = 'data_warehouse_person_properties',
Elements = 'elements',
Events = 'events',
EventProperties = 'event_properties',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function GlobalAndOrFilters({ insightProps }: EditorFilterProps): JSX.Ele
TaxonomicFilterGroupType.Elements,
...(isTrends ? [TaxonomicFilterGroupType.Sessions] : []),
TaxonomicFilterGroupType.HogQLExpression,
TaxonomicFilterGroupType.DataWarehousePersonProperties,
]

return (
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3460,6 +3460,9 @@
"operator": {
"$ref": "#/definitions/PropertyOperator"
},
"table": {
"type": "string"
},
"type": {
"const": "person",
"description": "Person properties",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { afterMount, kea, path } from 'kea'
import { afterMount, connect, kea, path, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
import { capitalizeFirstLetter } from 'lib/utils'

import { DataWarehouseViewLink } from '~/types'
import { DatabaseSchemaQueryResponseField } from '~/queries/schema'
import { DataWarehouseViewLink, PropertyDefinition, PropertyType } from '~/types'

import type { dataWarehouseJoinsLogicType } from './dataWarehouseJoinsLogicType'
import { dataWarehouseSceneLogic } from './dataWarehouseSceneLogic'

export const dataWarehouseJoinsLogic = kea<dataWarehouseJoinsLogicType>([
path(['scenes', 'data-warehouse', 'external', 'dataWarehouseJoinsLogic']),
Expand All @@ -19,6 +22,43 @@ export const dataWarehouseJoinsLogic = kea<dataWarehouseJoinsLogicType>([
},
],
}),
connect(() => ({
values: [dataWarehouseSceneLogic, ['externalTablesMap']],
})),
selectors({
personTableJoins: [(s) => [s.joins], (joins) => joins.filter((join) => join.source_table_name === 'persons')],
tablesJoinedToPersons: [
(s) => [s.externalTablesMap, s.personTableJoins],
(externalTablesMap, personTableJoins) => {
return personTableJoins.map((join: DataWarehouseViewLink) => {
// valid join should have a joining table name
const table = externalTablesMap[join.joining_table_name as string]
return {
table,
join,
}
})
},
],
columnsJoinedToPersons: [
(s) => [s.tablesJoinedToPersons],
(tablesJoinedToPersons) => {
return tablesJoinedToPersons.reduce((acc, { table, join }) => {
if (table) {
acc.push(
...table.columns.map((column: DatabaseSchemaQueryResponseField) => ({
id: column.key,
name: join.field_name + ': ' + column.key,
table: join.field_name,
property_type: capitalizeFirstLetter(column.type) as PropertyType,
}))
)
}
return acc
}, [] as PropertyDefinition[])
},
],
}),
afterMount(({ actions }) => {
actions.loadJoins()
}),
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ export interface EventPropertyFilter extends BasePropertyFilter {
export interface PersonPropertyFilter extends BasePropertyFilter {
type: PropertyFilterType.Person
operator: PropertyOperator
table?: string
}

export interface DataWarehousePropertyFilter extends BasePropertyFilter {
Expand Down Expand Up @@ -2812,6 +2813,9 @@ export interface PropertyDefinition {
verified?: boolean
verified_at?: string
verified_by?: string

// For Data warehouse person properties
table?: string
}

export enum PropertyDefinitionState {
Expand Down
1 change: 1 addition & 0 deletions plugin-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ export interface EventPropertyFilter extends PropertyFilterWithOperator {
/** Sync with posthog/frontend/src/types.ts */
export interface PersonPropertyFilter extends PropertyFilterWithOperator {
type: 'person'
table?: string
}

/** Sync with posthog/frontend/src/types.ts */
Expand Down
5 changes: 4 additions & 1 deletion posthog/hogql/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ def property_to_expr(
value = property.value

if property.type == "person" and scope != "person":
chain = ["person", "properties"]
if property.table:
chain = ["person", property.table]
else:
chain = ["person", "properties"]
elif property.type == "group":
chain = [f"group_{property.group_type_index}", "properties"]
elif property.type == "data_warehouse":
Expand Down
3 changes: 3 additions & 0 deletions posthog/models/property/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class Property:
total_periods: Optional[int]
min_periods: Optional[int]
negation: Optional[bool] = False
table: Optional[str]
_data: Dict

def __init__(
Expand All @@ -224,6 +225,7 @@ def __init__(
seq_time_value: Optional[int] = None,
seq_time_interval: Optional[OperatorInterval] = None,
negation: Optional[bool] = None,
table: Optional[str] = None,
**kwargs,
) -> None:
self.key = key
Expand All @@ -241,6 +243,7 @@ def __init__(
self.seq_time_value = seq_time_value
self.seq_time_interval = seq_time_interval
self.negation = None if negation is None else str_to_bool(negation)
self.table = table

if value is None and self.operator in ["is_set", "is_not_set"]:
self.value = self.operator
Expand Down
1 change: 1 addition & 0 deletions posthog/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,7 @@ class PersonPropertyFilter(BaseModel):
key: str
label: Optional[str] = None
operator: PropertyOperator
table: Optional[str] = None
type: Literal["person"] = Field(default="person", description="Person properties")
value: Optional[Union[str, float, List[Union[str, float]]]] = None

Expand Down

0 comments on commit 7244ee2

Please sign in to comment.