This repository has been archived by the owner on Nov 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
In-memory action definitions synced with Django #403
Merged
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
42eb852
Add ActionManager
Twixes bb45010
Refactor ActionManager
Twixes 580ffc1
Remove hello
Twixes 755e5dd
Adjust ActionManager method names and use single PubSub
Twixes 54f254a
Touch tests up
Twixes 797d56f
Merge branch 'master' into 235-action-matching
Twixes a87a0a0
Make some adjustments
Twixes ff0013e
Disable `status` stdout logs in test mode
Twixes e12d648
Fix `status`
Twixes f3415c8
Fix test problems
Twixes a42f6c8
Merge branch 'master' into 235-action-matching
Twixes 3f5553a
Fix dropAction typo
Twixes 63785f1
Reload all ActionManager caches every 5 min
Twixes e27c039
Merge branch 'master' into 235-action-matching
Twixes 1590d3c
Fix duplicate RawAction
Twixes 7f0dc18
Don't stringify JSONB column for `insertRow`
Twixes d768f11
It's a hub now
Twixes a5be2e4
Filter by Action.deleted
Twixes 5dde8df
Enhance ActionManager tests
Twixes 4af1ba7
Add Action-syncing task runner tests
Twixes bb606db
Merge branch 'master' into 235-action-matching
Twixes 8304827
Use `LOG_LEVEL=warn` in tests
Twixes c132a53
Don't `throw` error on unassociated channel pubsub
Twixes 4082003
Don't use defaultConfig in Status.buildMethod due to circular import
Twixes 6212419
Fix actions reload job var name
Twixes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ import { Pool, PoolClient, QueryConfig, QueryResult, QueryResultRow } from 'pg' | |
|
||
import { KAFKA_PERSON, KAFKA_PERSON_UNIQUE_ID, KAFKA_PLUGIN_LOG_ENTRIES } from '../../config/kafka-topics' | ||
import { | ||
Action, | ||
ActionStep, | ||
ClickHouseEvent, | ||
ClickHousePerson, | ||
ClickHousePersonDistinctId, | ||
|
@@ -26,6 +28,7 @@ import { | |
PluginLogEntryType, | ||
PostgresSessionRecordingEvent, | ||
PropertyDefinitionType, | ||
RawAction, | ||
RawOrganization, | ||
RawPerson, | ||
SessionRecordingEvent, | ||
|
@@ -705,14 +708,63 @@ export class DB { | |
return entry | ||
} | ||
|
||
// EventDefinition | ||
|
||
public async fetchEventDefinitions(): Promise<EventDefinitionType[]> { | ||
return (await this.postgresQuery('SELECT * FROM posthog_eventdefinition', undefined, 'fetchEventDefinitions')) | ||
.rows as EventDefinitionType[] | ||
} | ||
|
||
// PropertyDefinition | ||
|
||
public async fetchPropertyDefinitions(): Promise<PropertyDefinitionType[]> { | ||
return ( | ||
await this.postgresQuery('SELECT * FROM posthog_propertydefinition', undefined, 'fetchPropertyDefinitions') | ||
).rows as PropertyDefinitionType[] | ||
} | ||
|
||
// Action & ActionStep | ||
|
||
public async fetchAllActionsMap(): Promise<Record<Action['id'], Action>> { | ||
const rawActions: RawAction[] = ( | ||
await this.postgresQuery(`SELECT * FROM posthog_action WHERE deleted = FALSE`, undefined, 'fetchActions') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catch! |
||
).rows | ||
const actionSteps: ActionStep[] = ( | ||
await this.postgresQuery( | ||
`SELECT posthog_actionstep.*, posthog_action.deleted FROM posthog_actionstep | ||
JOIN posthog_action ON (posthog_action.id = posthog_actionstep.action_id) | ||
WHERE posthog_action.deleted = FALSE`, | ||
undefined, | ||
'fetchActionSteps' | ||
) | ||
).rows | ||
const actionsMap: Record<Action['id'], Action> = {} | ||
for (const rawAction of rawActions) { | ||
actionsMap[rawAction.id] = { ...rawAction, steps: [] } | ||
} | ||
for (const actionStep of actionSteps) { | ||
if (actionStep.action_id in actionsMap) { | ||
actionsMap[actionStep.action_id].steps.push(actionStep) | ||
} | ||
} | ||
return actionsMap | ||
} | ||
|
||
public async fetchAction(id: Action['id']): Promise<Action | null> { | ||
const rawActions: RawAction[] = ( | ||
await this.postgresQuery( | ||
`SELECT * FROM posthog_action WHERE id = $1 AND deleted = FALSE`, | ||
[id], | ||
'fetchActions' | ||
) | ||
).rows | ||
if (!rawActions.length) { | ||
return null | ||
} | ||
const steps: ActionStep[] = ( | ||
await this.postgresQuery(`SELECT * FROM posthog_actionstep WHERE action_id = $1`, [id], 'fetchActionSteps') | ||
).rows | ||
const action: Action = { ...rawActions[0], steps } | ||
Twixes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return action | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { captureException } from '@sentry/node' | ||
import { Redis } from 'ioredis' | ||
|
||
import { PluginsServerConfig } from '../types' | ||
import { status } from './status' | ||
import { createRedis } from './utils' | ||
|
||
export type PubSubTask = ((message: string) => void) | ((message: string) => Promise<void>) | ||
|
||
export interface PubSubTaskMap { | ||
[channel: string]: PubSubTask | ||
} | ||
|
||
export class PubSub { | ||
private serverConfig: PluginsServerConfig | ||
private redis: Redis | null | ||
public taskMap: PubSubTaskMap | ||
|
||
constructor(serverConfig: PluginsServerConfig, taskMap: PubSubTaskMap = {}) { | ||
this.serverConfig = serverConfig | ||
this.redis = null | ||
this.taskMap = taskMap | ||
} | ||
|
||
public async start(): Promise<void> { | ||
if (this.redis) { | ||
throw new Error('Started PubSub cannot be started again!') | ||
} | ||
this.redis = await createRedis(this.serverConfig) | ||
const channels = Object.keys(this.taskMap) | ||
await this.redis.subscribe(channels) | ||
this.redis.on('message', (channel: string, message: string) => { | ||
const task: PubSubTask | undefined = this.taskMap[channel] | ||
if (!task) { | ||
captureException( | ||
new Error( | ||
`Received a pubsub message for unassociated channel ${channel}! Associated channels are: ${Object.keys( | ||
this.taskMap | ||
).join(', ')}` | ||
) | ||
) | ||
} | ||
void task(message) | ||
}) | ||
status.info('👀', `Pub-sub started for channels: ${channels.join(', ')}`) | ||
} | ||
|
||
public async stop(): Promise<void> { | ||
if (!this.redis) { | ||
throw new Error('Unstarted PubSub cannot be stopped!') | ||
} | ||
await this.redis.unsubscribe() | ||
this.redis.disconnect() | ||
this.redis = null | ||
status.info('🛑', `Pub-sub stopped for channels: ${Object.keys(this.taskMap).join(', ')}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think this is overridden by the
pingJob
below?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch