Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hog): fix elements matching #24331

Merged
merged 47 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
dc795d6
feat(hogvm): add "has"
mariusandra Aug 13, 2024
b53eec1
bump versions
mariusandra Aug 13, 2024
2a7db13
feat(elements): use "has" for simplest element text match
mariusandra Aug 13, 2024
06224e0
Update query snapshots
github-actions[bot] Aug 13, 2024
02fbbdc
add elements_chain fields to invocation context
mariusandra Aug 13, 2024
5c6f191
add elements_chain fields to frontend
mariusandra Aug 13, 2024
c83bac2
Merge branch 'master' into element-text-match
mariusandra Aug 13, 2024
dd42b1b
Merge branch 'element-text-match' of github.com:PostHog/posthog into …
mariusandra Aug 13, 2024
bf0937c
Update UI snapshots for `chromium` (1)
github-actions[bot] Aug 13, 2024
a433a51
add fields to just filters
mariusandra Aug 13, 2024
a1133f5
Merge branch 'element-text-match' of github.com:PostHog/posthog into …
mariusandra Aug 13, 2024
35278f2
Merge branch 'master' into element-text-match
mariusandra Aug 13, 2024
a4a4e14
cleanup
mariusandra Aug 13, 2024
8d590f2
cleanup
mariusandra Aug 13, 2024
fda3ee2
Revert "Update UI snapshots for `chromium` (1)"
mariusandra Aug 13, 2024
5e8c16e
simplify
mariusandra Aug 13, 2024
e7edbbb
revert
mariusandra Aug 13, 2024
2e90983
test all text matching regex
mariusandra Aug 13, 2024
e0d16de
Update query snapshots
github-actions[bot] Aug 13, 2024
1630af2
Merge branch 'master' into element-text-match
mariusandra Aug 13, 2024
2afec0b
Update query snapshots
github-actions[bot] Aug 13, 2024
3ba61a5
lazy access
mariusandra Aug 13, 2024
85102da
revert same feature as for inline filteres
mariusandra Aug 13, 2024
5fa437e
Merge branch 'element-text-match' of github.com:PostHog/posthog into …
mariusandra Aug 13, 2024
71faceb
Update query snapshots
github-actions[bot] Aug 13, 2024
003486e
blabla
mariusandra Aug 13, 2024
8eb36ea
Merge branch 'element-text-match' of github.com:PostHog/posthog into …
mariusandra Aug 13, 2024
9315954
Merge branch 'master' into element-text-match
mariusandra Aug 23, 2024
025f84c
Merge branch 'master' into element-text-match
mariusandra Aug 23, 2024
b2195b0
cleanup
mariusandra Aug 23, 2024
abc5c3b
Merge branch 'master' into element-text-match
mariusandra Aug 29, 2024
d57e086
Merge branch 'master' into element-text-match
mariusandra Aug 30, 2024
d501713
Merge branch 'master' into element-text-match
mariusandra Sep 2, 2024
150ddd7
cleanup
mariusandra Sep 4, 2024
b2bede5
add a test
mariusandra Sep 4, 2024
7a3a4bd
include global properties
mariusandra Sep 4, 2024
00a9af6
revert a thing
mariusandra Sep 4, 2024
e2e1bc7
cache calculated values
mariusandra Sep 4, 2024
904a591
comment
mariusandra Sep 4, 2024
e82219b
wip tests
mariusandra Sep 4, 2024
60b11e3
Merge branch 'master' into element-text-match
mariusandra Sep 4, 2024
418811c
more tests
mariusandra Sep 4, 2024
82a4bc7
add indexOf and position
mariusandra Sep 4, 2024
ac92ea5
collective bump
mariusandra Sep 4, 2024
77d2de8
add arrayCount
mariusandra Sep 4, 2024
d66c7e6
more bumps
mariusandra Sep 4, 2024
19da16d
Merge branch 'master' into element-text-match
mariusandra Sep 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
PipelineTab,
PropertyFilterType,
PropertyGroupFilter,
PropertyGroupFilterValue,
} from '~/types'

import { EmailTemplate } from './email-templater/emailTemplaterLogic'
Expand Down Expand Up @@ -448,10 +449,14 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy
sparklineQuery: [
(s) => [s.configuration],
(configuration): TrendsQuery => {
const properties: PropertyGroupFilter = {
const seriesProperties: PropertyGroupFilterValue = {
type: FilterLogicalOperator.Or,
values: [],
}
const properties: PropertyGroupFilter = {
type: FilterLogicalOperator.And,
values: [seriesProperties],
}
for (const event of configuration.filters?.events ?? []) {
const eventProperties: AnyPropertyFilter[] = [...(event.properties ?? [])]
if (event.id) {
Expand All @@ -466,7 +471,7 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy
key: 'true',
})
}
properties.values.push({
seriesProperties.values.push({
type: FilterLogicalOperator.And,
values: eventProperties,
})
Expand All @@ -479,11 +484,21 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy
key: hogql`matchesAction(${parseInt(action.id)})`,
})
}
properties.values.push({
seriesProperties.values.push({
type: FilterLogicalOperator.And,
values: actionProperties,
})
}
if ((configuration.filters?.properties?.length ?? 0) > 0) {
const globalProperties: PropertyGroupFilterValue = {
type: FilterLogicalOperator.And,
values: [],
}
for (const property of configuration.filters?.properties ?? []) {
globalProperties.values.push(property as AnyPropertyFilter)
}
properties.values.push(globalProperties)
}

return {
kind: NodeKind.TrendsQuery,
Expand Down
18 changes: 4 additions & 14 deletions plugin-server/src/cdp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,6 @@ export interface HogFunctionFilters {
bytecode?: HogBytecode
}

// We have a "parsed" clickhous event type to make it easier to work with calls from kafka as well as those from the frontend
export interface ParsedClickhouseEvent {
uuid: string
event: string
team_id: number
distinct_id: string
person_id?: string
timestamp: string
created_at: string
properties: Record<string, any>
person_created_at?: string
person_properties: Record<string, any>
}

export type GroupType = {
id: string // the "key" of the group
type: string
Expand Down Expand Up @@ -109,6 +95,10 @@ export type HogFunctionFilterGlobals = {
event: string
timestamp: string
elements_chain: string
elements_chain_href: string
elements_chain_texts: string[]
elements_chain_ids: string[]
elements_chain_elements: string[]
properties: Record<string, any>

person?: {
Expand Down
88 changes: 65 additions & 23 deletions plugin-server/src/cdp/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// NOTE: PostIngestionEvent is our context event - it should never be sent directly to an output, but rather transformed into a lightweight schema

import { DateTime } from 'luxon'
import RE2 from 're2'
import { gunzip, gzip } from 'zlib'

import { RawClickHouseEvent, Team, TimestampFormat } from '../types'
Expand All @@ -12,7 +13,6 @@ import {
HogFunctionInvocationGlobals,
HogFunctionInvocationResult,
HogFunctionLogEntrySerialized,
ParsedClickhouseEvent,
} from './types'

export const PERSON_DEFAULT_DISPLAY_NAME_PROPERTIES = [
Expand All @@ -36,26 +36,6 @@ const getPersonDisplayName = (team: Team, distinctId: string, properties: Record
return (customIdentifier || distinctId)?.trim()
}

export function convertToParsedClickhouseEvent(event: RawClickHouseEvent): ParsedClickhouseEvent {
const properties = event.properties ? JSON.parse(event.properties) : {}
if (event.elements_chain) {
properties['$elements_chain'] = event.elements_chain
}

return {
uuid: event.uuid,
event: event.event,
team_id: event.team_id,
distinct_id: event.distinct_id,
person_id: event.person_id,
timestamp: clickHouseTimestampToISO(event.timestamp),
created_at: clickHouseTimestampToISO(event.created_at),
properties: properties,
person_created_at: event.person_created_at ? clickHouseTimestampToISO(event.person_created_at) : undefined,
person_properties: event.person_properties ? JSON.parse(event.person_properties) : {},
}
}

// that we can keep to as a contract
export function convertToHogFunctionInvocationGlobals(
event: RawClickHouseEvent,
Expand Down Expand Up @@ -105,6 +85,46 @@ export function convertToHogFunctionInvocationGlobals(
return context
}

function getElementsChainHref(elementsChain: string): string {
// Adapted from SQL: extract(elements_chain, '(?::|\")href="(.*?)"'),
const hrefRegex = new RE2(/(?::|")href="(.*?)"/)
const hrefMatch = hrefRegex.exec(elementsChain)
return hrefMatch ? hrefMatch[1] : ''
}

function getElementsChainTexts(elementsChain: string): string[] {
// Adapted from SQL: arrayDistinct(extractAll(elements_chain, '(?::|\")text="(.*?)"')),
const textRegex = new RE2(/(?::|")text="(.*?)"/g)
const textMatches = new Set<string>()
let textMatch
while ((textMatch = textRegex.exec(elementsChain)) !== null) {
textMatches.add(textMatch[1])
}
return Array.from(textMatches)
}

function getElementsChainIds(elementsChain: string): string[] {
// Adapted from SQL: arrayDistinct(extractAll(elements_chain, '(?::|\")id="(.*?)"')),
const idRegex = new RE2(/(?::|")id="(.*?)"/g)
const idMatches = new Set<string>()
let idMatch
while ((idMatch = idRegex.exec(elementsChain)) !== null) {
idMatches.add(idMatch[1])
}
return Array.from(idMatches)
}

function getElementsChainElements(elementsChain: string): string[] {
// Adapted from SQL: arrayDistinct(extractAll(elements_chain, '(?:^|;)(a|button|form|input|select|textarea|label)(?:\\.|$|:)'))
const elementRegex = new RE2(/(?:^|;)(a|button|form|input|select|textarea|label)(?:\.|$|:)/g)
const elementMatches = new Set<string>()
let elementMatch
while ((elementMatch = elementRegex.exec(elementsChain)) !== null) {
elementMatches.add(elementMatch[1])
}
return Array.from(elementMatches)
}

export function convertToHogFunctionFilterGlobal(globals: HogFunctionInvocationGlobals): HogFunctionFilterGlobals {
const groups: Record<string, any> = {}

Expand All @@ -114,14 +134,36 @@ export function convertToHogFunctionFilterGlobal(globals: HogFunctionInvocationG
}
}

return {
const elementsChain = globals.event.properties['$elements_chain']
const response = {
event: globals.event.name,
elements_chain: globals.event.properties['$elements_chain'],
elements_chain: elementsChain,
elements_chain_href: '',
elements_chain_texts: [],
elements_chain_ids: [],
elements_chain_elements: [],
timestamp: globals.event.timestamp,
properties: globals.event.properties,
person: globals.person ? { properties: globals.person.properties } : undefined,
...groups,
}
if (elementsChain) {
Object.defineProperties(response, {
elements_chain_href: {
get: () => getElementsChainHref(elementsChain),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this solution. I'm wondering though if we should also cache these values? Maybe not worth it for now...

Also a comment here would be helpful for future travellers to understand what this is all for (I barely get it right now)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also its not awesome that there is only a test for one path...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I found a rather neat way to cache this: e2e1bc7

More tests coming soon...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wooohhhhhh self destructing function :o

},
elements_chain_texts: {
get: () => getElementsChainTexts(elementsChain),
},
elements_chain_ids: {
get: () => getElementsChainIds(elementsChain),
},
elements_chain_elements: {
get: () => getElementsChainElements(elementsChain),
},
})
}
return response
}

export const convertToCaptureEvent = (event: HogFunctionCapturedEvent, team: Team): any => {
Expand Down
50 changes: 50 additions & 0 deletions plugin-server/tests/cdp/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,56 @@ export const HOG_FILTERS_EXAMPLES: Record<string, Pick<HogFunctionType, 'filters
],
},
},
elements_text_filter: {
filters: {
actions: [],
bytecode: [
'_H',
1,
32,
'$autocapture',
32,
'event',
1,
1,
11,
52,
'lambda',
1,
0,
6,
32,
'%reload%',
36,
0,
18,
38,
53,
0,
32,
'elements_chain_texts',
1,
1,
2,
'arrayExists',
2,
3,
2,
4,
1,
],
events: [
{
id: '$autocapture',
name: '$autocapture',
order: 0,
properties: [{ key: 'text', operator: 'icontains', type: 'element', value: 'reload' }],
type: 'events',
},
],
filter_test_accounts: false,
},
},
}

export const HOG_MASK_EXAMPLES: Record<string, Pick<HogFunctionType, 'masking'>> = {
Expand Down
50 changes: 50 additions & 0 deletions plugin-server/tests/cdp/hog-executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,56 @@ describe('Hog Executor', () => {
expect(resultsShouldMatch.matchingFunctions).toHaveLength(1)
expect(resultsShouldMatch.nonMatchingFunctions).toHaveLength(0)
})

it('can use elements_chain materialized fields', () => {
const fn = createHogFunction({
...HOG_EXAMPLES.simple_fetch,
...HOG_INPUTS_EXAMPLES.simple_fetch,
...HOG_FILTERS_EXAMPLES.elements_text_filter,
})

mockFunctionManager.getTeamHogFunctions.mockReturnValue([fn])
const elementsChain = (buttonText: string) =>
`span.LemonButton__content:attr__class="LemonButton__content"nth-child="2"nth-of-type="2"text="${buttonText}";span.LemonButton__chrome:attr__class="LemonButton__chrome"nth-child="1"nth-of-type="1";button.LemonButton.LemonButton--has-icon.LemonButton--secondary.LemonButton--status-default:attr__class="LemonButton LemonButton--secondary LemonButton--status-default LemonButton--has-icon"attr__type="button"nth-child="1"nth-of-type="1"text="${buttonText}";div.flex.gap-4.items-center:attr__class="flex gap-4 items-center"nth-child="1"nth-of-type="1";div.flex.flex-wrap.gap-4.justify-between:attr__class="flex gap-4 justify-between flex-wrap"nth-child="3"nth-of-type="3";div.flex.flex-1.flex-col.gap-4.h-full.relative.w-full:attr__class="relative w-full flex flex-col gap-4 flex-1 h-full"nth-child="1"nth-of-type="1";div.LemonTabs__content:attr__class="LemonTabs__content"nth-child="2"nth-of-type="1";div.LemonTabs.LemonTabs--medium:attr__class="LemonTabs LemonTabs--medium"attr__style="--lemon-tabs-slider-width: 48px; --lemon-tabs-slider-offset: 0px;"nth-child="1"nth-of-type="1";div.Navigation3000__scene:attr__class="Navigation3000__scene"nth-child="2"nth-of-type="2";main:nth-child="2"nth-of-type="1";div.Navigation3000:attr__class="Navigation3000"nth-child="1"nth-of-type="1";div:attr__id="root"attr_id="root"nth-child="3"nth-of-type="1";body.overflow-hidden:attr__class="overflow-hidden"attr__theme="light"nth-child="2"nth-of-type="1"`

const hogGlobals1 = createHogExecutionGlobals({
groups: {},
event: {
uuid: 'uuid',
name: '$autocapture',
distinct_id: 'distinct_id',
url: 'http://localhost:8000/events/1',
properties: {
$lib_version: '1.2.3',
$elements_chain: elementsChain('Not our text'),
},
timestamp: new Date().toISOString(),
},
})

const resultsShouldntMatch = executor.findMatchingFunctions(hogGlobals1)
expect(resultsShouldntMatch.matchingFunctions).toHaveLength(0)
expect(resultsShouldntMatch.nonMatchingFunctions).toHaveLength(1)

const hogGlobals2 = createHogExecutionGlobals({
groups: {},
event: {
uuid: 'uuid',
name: '$autocapture',
distinct_id: 'distinct_id',
url: 'http://localhost:8000/events/1',
properties: {
$lib_version: '1.2.3',
$elements_chain: elementsChain('Reload'),
},
timestamp: new Date().toISOString(),
},
})

const resultsShouldMatch = executor.findMatchingFunctions(hogGlobals2)
expect(resultsShouldMatch.matchingFunctions).toHaveLength(1)
expect(resultsShouldMatch.nonMatchingFunctions).toHaveLength(0)
})
})

describe('async function responses', () => {
Expand Down
31 changes: 17 additions & 14 deletions posthog/hogql/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,23 +550,26 @@ def action_to_expr(action: Action) -> ast.Expr:
if step.text is not None:
value = step.text
if step.text_matching == "regex":
match = ast.CompareOperationOp.Regex
exprs.append(
parse_expr(
"arrayExists(x -> x =~ {value}, elements_chain_texts)",
{"value": ast.Constant(value=value)},
)
)
elif step.text_matching == "contains":
match = ast.CompareOperationOp.ILike
value = f"%{value}%"
exprs.append(
parse_expr(
"arrayExists(x -> x ilike {value}, elements_chain_texts)",
{"value": ast.Constant(value=f"%{value}%")},
)
)
else:
match = ast.CompareOperationOp.Eq

exprs.append(
parse_expr(
"arrayExists(x -> {match}, elements_chain_texts)",
{
"match": ast.CompareOperation(
op=match, left=ast.Field(chain=["x"]), right=ast.Constant(value=value)
)
},
exprs.append(
parse_expr(
"arrayExists(x -> x = {value}, elements_chain_texts)",
{"value": ast.Constant(value=value)},
)
)
)
if step.url:
if step.url_matching == "exact":
expr = parse_expr(
Expand Down
18 changes: 18 additions & 0 deletions posthog/hogql/test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,24 @@ def test_action_to_expr(self):
self._parse_expr("event = '$autocapture' and arrayExists(x -> x =~ 'blabla', elements_chain_texts)"),
)

action7 = Action.objects.create(
team=self.team,
steps_json=[{"event": "$autocapture", "text": "blabla", "text_matching": "contains"}],
)
self.assertEqual(
clear_locations(action_to_expr(action7)),
self._parse_expr("event = '$autocapture' and arrayExists(x -> x ilike '%blabla%', elements_chain_texts)"),
)

action8 = Action.objects.create(
team=self.team,
steps_json=[{"event": "$autocapture", "text": "blabla", "text_matching": "exact"}],
)
self.assertEqual(
clear_locations(action_to_expr(action8)),
self._parse_expr("event = '$autocapture' and arrayExists(x -> x = 'blabla', elements_chain_texts)"),
)

def test_cohort_filter_static(self):
cohort = Cohort.objects.create(
team=self.team,
Expand Down
Loading