From 76c9b564fa700f101d1069b13984a41b345791ed Mon Sep 17 00:00:00 2001 From: Brett Hoerner Date: Fri, 26 Apr 2024 08:54:27 -0600 Subject: [PATCH] fix(plugin-server): add time component to person.force_upgrade --- plugin-server/src/worker/ingestion/person-state.ts | 11 +++++++++-- .../tests/worker/ingestion/person-state.test.ts | 9 ++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/plugin-server/src/worker/ingestion/person-state.ts b/plugin-server/src/worker/ingestion/person-state.ts index 7d605bc707092..6c94de6f0ea2a 100644 --- a/plugin-server/src/worker/ingestion/person-state.ts +++ b/plugin-server/src/worker/ingestion/person-state.ts @@ -112,8 +112,15 @@ export class PersonState { // Ensure person properties don't propagate elsewhere, such as onto the event itself. person.properties = {} - // See documentation on the field. - person.force_upgrade = true + if (this.timestamp > person.created_at.plus({ minutes: 1 })) { + // See documentation on the field. + // + // Note that we account for timestamp vs person creation time (with a little + // padding for good measure) to account for ingestion lag. It's possible for + // events to be processed after person creation even if they were sent prior + // to person creation, and the user did nothing wrong in that case. + person.force_upgrade = true + } return person } diff --git a/plugin-server/tests/worker/ingestion/person-state.test.ts b/plugin-server/tests/worker/ingestion/person-state.test.ts index 0d8aa9b509c95..d3a04018e0d96 100644 --- a/plugin-server/tests/worker/ingestion/person-state.test.ts +++ b/plugin-server/tests/worker/ingestion/person-state.test.ts @@ -111,7 +111,8 @@ describe('PersonState.update()', () => { event: Partial, customHub?: Hub, processPerson = true, - lazyPersonCreation = false + lazyPersonCreation = false, + timestampParam = timestamp ) { const fullEvent = { team_id: teamId, @@ -123,7 +124,7 @@ describe('PersonState.update()', () => { fullEvent as any, teamId, event.distinct_id!, - timestamp, + timestampParam, processPerson, customHub ? customHub.db : hub.db, lazyPersonCreation, @@ -261,6 +262,7 @@ describe('PersonState.update()', () => { // `force_upgrade=true` and real Person `uuid` and `created_at` processPerson = false const event_uuid = new UUIDT().toString() + const timestampParam = timestamp.plus({ minutes: 5 }) // Event needs to happen after Person creation const fakePerson = await personState( { event: '$pageview', @@ -270,7 +272,8 @@ describe('PersonState.update()', () => { }, hubParam, processPerson, - lazyPersonCreation + lazyPersonCreation, + timestampParam ).update() await hub.db.kafkaProducer.flush()