Skip to content

Commit

Permalink
chore(plugin-server): set 'force_upgrade' for 'person_mode' when appl…
Browse files Browse the repository at this point in the history
…icable
  • Loading branch information
bretthoerner committed Apr 25, 2024
1 parent d965575 commit db1b785
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
10 changes: 8 additions & 2 deletions plugin-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ interface BaseEvent {
export type ISOTimestamp = Brand<string, 'ISOTimestamp'>
export type ClickHouseTimestamp = Brand<string, 'ClickHouseTimestamp'>
export type ClickHouseTimestampSecondPrecision = Brand<string, 'ClickHouseTimestamp'>
export type PersonMode = 'full' | 'propertyless' | 'force_upgrade'

/** Raw event row from ClickHouse. */
export interface RawClickHouseEvent extends BaseEvent {
Expand All @@ -638,7 +639,7 @@ export interface RawClickHouseEvent extends BaseEvent {
group2_created_at?: ClickHouseTimestamp
group3_created_at?: ClickHouseTimestamp
group4_created_at?: ClickHouseTimestamp
person_mode: 'full' | 'propertyless'
person_mode: PersonMode
}

/** Parsed event row from ClickHouse. */
Expand All @@ -659,7 +660,7 @@ export interface ClickHouseEvent extends BaseEvent {
group2_created_at?: DateTime | null
group3_created_at?: DateTime | null
group4_created_at?: DateTime | null
person_mode: 'full' | 'propertyless'
person_mode: PersonMode
}

/** Event in a database-agnostic shape, AKA an ingestion event.
Expand Down Expand Up @@ -746,6 +747,11 @@ export interface Person {
properties: Properties
uuid: string
created_at: DateTime

// Set to `true` when an existing person row was found for this `distinct_id`, but the event was
// sent with `$process_person_profile=false`. This is an unexpected branch that we want to flag
// for debugging and billing purposes, and typically means a misconfigured SDK.
force_upgrade?: boolean
}

/** Clickhouse Person model. */
Expand Down
9 changes: 7 additions & 2 deletions plugin-server/src/worker/ingestion/person-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,16 @@ export class PersonState {
async update(): Promise<Person> {
if (!this.processPerson) {
if (this.lazyPersonCreation) {
const person = await this.db.fetchPerson(this.teamId, this.distinctId, { useReadReplica: true })
if (person) {
const existingPerson = await this.db.fetchPerson(this.teamId, this.distinctId, { useReadReplica: true })
if (existingPerson) {
const person = existingPerson as Person

// Ensure person properties don't propagate elsewhere, such as onto the event itself.
person.properties = {}

// See documentation on the field.
person.force_upgrade = true

return person
}

Expand Down
10 changes: 9 additions & 1 deletion plugin-server/src/worker/ingestion/process-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Hub,
ISOTimestamp,
Person,
PersonMode,
PreIngestionEvent,
RawClickHouseEvent,
Team,
Expand Down Expand Up @@ -239,6 +240,13 @@ export class EventsProcessor {

// TODO: Remove Redis caching for person that's not used anymore

let personMode: PersonMode = 'full'
if (person.force_upgrade) {
personMode = 'force_upgrade'
} else if (!processPerson) {
personMode = 'propertyless'
}

const rawEvent: RawClickHouseEvent = {
uuid,
event: safeClickhouseString(event),
Expand All @@ -251,7 +259,7 @@ export class EventsProcessor {
person_id: person.uuid,
person_properties: eventPersonProperties,
person_created_at: castTimestampOrNow(person.created_at, TimestampFormat.ClickHouseSecondPrecision),
person_mode: processPerson ? 'full' : 'propertyless',
person_mode: personMode,
...groupsColumns,
}

Expand Down

0 comments on commit db1b785

Please sign in to comment.