Skip to content

Commit

Permalink
partial revert of PR23669
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverb123 committed Aug 5, 2024
1 parent dc1428c commit 24bdb6b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class PropertyDefinitionsManager {
this.syncEventDefinitions(team, event),
this.syncEventProperties(team, event, properties),
this.syncPropertyDefinitions(team, event, properties),
this.teamManager.setTeamIngestedEvent(team),
this.teamManager.setTeamIngestedEvent(team, properties),
])
} finally {
clearTimeout(timeout)
Expand Down
38 changes: 33 additions & 5 deletions plugin-server/src/worker/ingestion/team-manager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Properties } from '@posthog/plugin-scaffold'
import LRU from 'lru-cache'

import { ONE_MINUTE } from '../../config/constants'
import { TeamIDWithConfig } from '../../main/ingestion-queues/session-recording/session-recordings-consumer'
import { PipelineEvent, PluginsServerConfig, Team, TeamId } from '../../types'
import { PostgresRouter, PostgresUse } from '../../utils/db/postgres'
import { timeoutGuard } from '../../utils/db/utils'
import { posthog } from '../../utils/posthog'

export class TeamManager {
postgres: PostgresRouter
Expand Down Expand Up @@ -102,7 +104,7 @@ export class TeamManager {
}
}

public async setTeamIngestedEvent(team: Team) {
public async setTeamIngestedEvent(team: Team, properties: Properties) {
if (team && !team.ingested_event) {
await this.postgres.query(
PostgresUse.COMMON_WRITE,
Expand All @@ -111,11 +113,37 @@ export class TeamManager {
'setTeamIngestedEvent'
)

// This doesn't totally stop the first event from being spammed if a
// new team suddenly gets a lot of events, since other pods will still
// wait a while before pulling the team data from the DB, but it might
// help a bit.
// So long as team id is used as the partition key, this helps avoid
// double-firing of the first events, but it's not perfect (pod crashes
// or other rebalances, for example, can still cause double-firing). Exactly
// once is hard.
this.teamCache.set(team.id, { ...team, ingested_event: true })

// First event for the team captured - we fire this because comms and others rely on this event for onboarding flows in downstream systems (e.g. customer.io)
const organizationMembers = await this.postgres.query(
PostgresUse.COMMON_WRITE,
'SELECT distinct_id FROM posthog_user JOIN posthog_organizationmembership ON posthog_user.id = posthog_organizationmembership.user_id WHERE organization_id = $1',
[team.organization_id],
'posthog_organizationmembership'
)
const distinctIds: { distinct_id: string }[] = organizationMembers.rows
for (const { distinct_id } of distinctIds) {
posthog.capture({
distinctId: distinct_id,
event: 'first team event ingested',
properties: {
team: team.uuid,
sdk: properties.$lib,
realm: properties.realm,
host: properties.$host,
},
groups: {
project: team.uuid,
organization: team.organization_id,
instance: this.instanceSiteUrl,
},
})
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions plugin-server/tests/main/process-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,19 @@ test('capture first team event', async () => {
new UUIDT().toString()
)

expect(posthog.capture).toHaveBeenCalledWith({
distinctId: 'plugin_test_user_distinct_id_1001',
event: 'first team event ingested',
properties: {
team: team.uuid,
},
groups: {
project: team.uuid,
organization: team.organization_id,
instance: 'unknown',
},
})

team = await getFirstTeam(hub)
expect(team.ingested_event).toEqual(true)

Expand Down

0 comments on commit 24bdb6b

Please sign in to comment.