Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Feb 25, 2025
1 parent 2e74d54 commit e2b81dd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
3 changes: 2 additions & 1 deletion frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2008,7 +2008,8 @@ const api = {
data: {
configuration: Record<string, any>
mock_async_functions: boolean
globals: any
globals?: any
clickhouse_event?: any
invocation_id?: string
}
): Promise<HogFunctionTestInvocationResult> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,47 @@ import { delay } from 'lib/utils'

import { HogQLQuery, NodeKind } from '~/queries/schema/schema-general'
import { hogql } from '~/queries/utils'
import { HogFunctionInvocationGlobals, LogEntryLevel } from '~/types'
import { LogEntryLevel } from '~/types'

import type { hogFunctionLogsLogicType } from './hogFunctionLogsLogicType'
import { GroupedLogEntry, logsViewerLogic, LogsViewerLogicProps } from './logsViewerLogic'

export type RetryInvocationState = 'pending' | 'success' | 'failure'

const loadEventGlobals = async (eventId: string): Promise<Pick<HogFunctionInvocationGlobals, 'event' | 'person'>> => {
// TODO: Do we have a better type?
const loadClickhouseEvent = async (eventId: string): Promise<any> => {
const query: HogQLQuery = {
kind: NodeKind.HogQLQuery,
query: hogql`
select uuid, distinct_id, event, timestamp, properties, person.id, person.properties
select uuid, distinct_id, event, timestamp, properties, elements_chain, person.id, person.properties, person.created_at
from events
where uuid = ${eventId}
limit 1`,
}

const response = await api.query(query, undefined, undefined, true)
const [uuid, distinct_id, event, timestamp, properties, person_id, person_properties] = response.results[0]
const [
uuid,
distinct_id,
event,
timestamp,
properties,
elements_chain,
person_id,
person_properties,
person_created_at,
] = response.results[0]

return {
event: {
uuid,
distinct_id,
event,
properties: JSON.parse(properties),
timestamp,
url: '',
elements_chain: '',
},
person: {
id: person_id,
properties: JSON.parse(person_properties),
url: '',
name: '',
},
uuid,
event,
distinct_id,
person_id,
timestamp,
properties,
elements_chain,
person_created_at,
person_properties,
}
}

Expand Down Expand Up @@ -93,10 +98,10 @@ export const hogFunctionLogsLogic = kea<hogFunctionLogsLogicType>([
await delay(1000)

try {
const globals = await loadEventGlobals(eventId)
const clickhouseEvent = await loadClickhouseEvent(eventId)

const res = await api.hogFunctions.createTestInvocation(props.sourceId, {
globals,
clickhouse_event: clickhouseEvent,
mock_async_functions: false,
configuration: {
// For retries we don't care about filters
Expand Down
22 changes: 16 additions & 6 deletions plugin-server/src/cdp/cdp-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
HogFunctionType,
LogEntry,
} from './types'
import { convertToHogFunctionInvocationGlobals } from './utils'

export class CdpApi {
private hogExecutor: HogExecutorService
Expand Down Expand Up @@ -117,15 +118,11 @@ export class CdpApi {
private postFunctionInvocation = async (req: express.Request, res: express.Response): Promise<any> => {
try {
const { id, team_id } = req.params
const { globals, mock_async_functions, configuration, invocation_id } = req.body
const { clickhouse_event, mock_async_functions, configuration, invocation_id } = req.body
let { globals } = req.body

status.info('⚡️', 'Received invocation', { id, team_id, body: req.body })

if (!globals || !globals.event) {
res.status(400).json({ error: 'Missing event' })
return
}

const invocationID = invocation_id ?? new UUIDT().toString()

// Check the invocationId is a valid UUID
Expand All @@ -145,6 +142,19 @@ export class CdpApi {
return res.status(404).json({ error: 'Team not found' })
}

globals = clickhouse_event
? convertToHogFunctionInvocationGlobals(
clickhouse_event,
team,
this.hub.SITE_URL ?? 'http://localhost:8000'
)
: globals

if (!globals || !globals.event) {
res.status(400).json({ error: 'Missing event' })
return
}

// NOTE: We allow the hog function to be null if it is a "new" hog function
// The real security happens at the django layer so this is more of a sanity check
if (!isNewFunction && (!hogFunction || hogFunction.team_id !== team.id)) {
Expand Down
3 changes: 2 additions & 1 deletion posthog/api/hog_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ def update(self, instance: HogFunction, validated_data: dict, *args, **kwargs) -

class HogFunctionInvocationSerializer(serializers.Serializer):
configuration = HogFunctionSerializer(write_only=True)
globals = serializers.DictField(write_only=True)
globals = serializers.DictField(write_only=True, required=False)
clickhouse_event = serializers.DictField(write_only=True, required=False)
mock_async_functions = serializers.BooleanField(default=True, write_only=True)
status = serializers.CharField(read_only=True)
logs = serializers.ListField(read_only=True)
Expand Down

0 comments on commit e2b81dd

Please sign in to comment.