-
Notifications
You must be signed in to change notification settings - Fork 274
[Hubspot] Add de-duplication logic in Hubspot #2924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { PayloadValidationError } from '@segment/actions-core' | ||
import { Payload } from '../generated-types' | ||
import { Association } from '../types' | ||
|
||
export function validate(payloads: Payload[]): Payload[] { | ||
const length = payloads.length | ||
|
@@ -85,3 +86,114 @@ function cleanProp(str: string): string { | |
} | ||
return str | ||
} | ||
|
||
/** | ||
* Merges an array of payloads by their unique `id_field_value`, deduplicating entries and merging their properties. | ||
* | ||
* For each unique ID: | ||
* - Properties and sensitive properties are merged, preferring values from the payload with the latest timestamp. | ||
* - Associations are merged. | ||
* - The resulting payload for each ID contains merged properties, sensitive properties, and associations. | ||
* - The `timestamp` field is used only for determining recency and is removed from the final output. | ||
* | ||
* @param payloads - An array of payloads to merge and deduplicate. | ||
* @returns An array of merged and deduplicated payloads, with timestamps removed. | ||
*/ | ||
export function mergeAndDeduplicateById(payloads: Payload[]): Payload[] { | ||
const mergedMap = new Map<string, Payload>() | ||
|
||
for (const incoming of payloads) { | ||
const id = incoming.object_details?.id_field_value | ||
if (!id) continue | ||
|
||
const incomingTimestamp = incoming.timestamp ? new Date(incoming.timestamp).getTime() : 0 | ||
|
||
const existing = mergedMap.get(id) | ||
if (!existing) { | ||
mergedMap.set(id, { ...incoming }) | ||
continue | ||
} | ||
|
||
const existingTimestamp = existing.timestamp ? new Date(existing.timestamp).getTime() : 0 | ||
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. I think that we should ensure that there's always a timestamp in the payload that gets added to the mergeMap (see my comment above). This means that if there is an existing payload, you can assume it has a timestamp. |
||
|
||
// Merge properties | ||
const mergedProps: Record<string, unknown> = { ...existing.properties } | ||
for (const [key, value] of Object.entries(incoming.properties || {})) { | ||
if (!(key in mergedProps) || incomingTimestamp >= existingTimestamp) { | ||
harsh-joshi99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mergedProps[key] = value | ||
} | ||
} | ||
|
||
// Merge sensitive_properties | ||
const mergedSensProps: Record<string, unknown> = { ...existing.sensitive_properties } | ||
for (const [key, value] of Object.entries(incoming.sensitive_properties || {})) { | ||
if (!(key in mergedSensProps) || incomingTimestamp >= existingTimestamp) { | ||
mergedSensProps[key] = value | ||
} | ||
} | ||
|
||
// Merge associations | ||
const existingAssociations = existing.associations || [] | ||
const incomingAssociations = incoming.associations || [] | ||
|
||
const associationKey = (assoc: Association) => | ||
`${assoc.object_type}|${assoc.association_label}|${assoc.id_field_name}|${assoc.id_field_value}` | ||
|
||
const existingAssocMap = new Map(existingAssociations.map((a) => [associationKey(a), a])) | ||
|
||
for (const assoc of incomingAssociations) { | ||
const key = associationKey(assoc) | ||
if (!existingAssocMap.has(key)) { | ||
existingAssocMap.set(key, assoc) | ||
} | ||
} | ||
|
||
const mergedAssociations = Array.from(existingAssocMap.values()) | ||
|
||
// Save merged record with the latest timestamp | ||
mergedMap.set(id, { | ||
...existing, | ||
properties: mergedProps, | ||
sensitive_properties: mergedSensProps, | ||
associations: mergedAssociations, | ||
timestamp: incomingTimestamp >= existingTimestamp ? incoming.timestamp : existing.timestamp | ||
}) | ||
} | ||
|
||
// Final output with timestamp removed | ||
return Array.from(mergedMap.values()).map(({ timestamp, ...rest }) => rest) | ||
} | ||
|
||
/** | ||
* Ensures that each payload in the provided array has a valid timestamp. | ||
* If a payload's timestamp is invalid, it is replaced with the provided fallback timestamp. | ||
* | ||
* @param payloads - An array of payload objects, each potentially containing a `timestamp` property. | ||
* @param fallbackTimestamp - The timestamp string to use when a payload's timestamp is invalid. | ||
* @returns A new array of payloads with valid timestamps. | ||
*/ | ||
export function ensureValidTimestamps(payloads: Payload[], fallbackTimestamp?: string): Payload[] { | ||
const safeFallback = isValidTimestamp(fallbackTimestamp) ? fallbackTimestamp : new Date().toISOString() | ||
|
||
return payloads.map((p) => ({ | ||
...p, | ||
timestamp: isValidTimestamp(p.timestamp) ? p.timestamp : safeFallback | ||
})) | ||
} | ||
|
||
/** | ||
* Checks if the provided value is a valid timestamp. | ||
* | ||
* Accepts strings, numbers, or Date objects and verifies if they can be converted | ||
* to a valid date. Returns `true` if the value represents a valid timestamp, | ||
* otherwise returns `false`. | ||
* | ||
* @param ts - The value to validate as a timestamp. | ||
* @returns `true` if `ts` is a valid timestamp, otherwise `false`. | ||
*/ | ||
function isValidTimestamp(ts: unknown): ts is string | number | Date { | ||
if (typeof ts === 'string' || typeof ts === 'number' || ts instanceof Date) { | ||
return !isNaN(new Date(ts).getTime()) | ||
} | ||
return false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
If the customer misconfigures the timestamp field or sends something that isn't a timestamp, then you're falling back to creating a timestamp. However the timestamp you create might correctly order the event.
It might be better to grab the timestamp directly from the raw payload data instead. i.e not via a field. Note that there will always be a timestamp in the raw payload data.
Have a look at how to access the rawData object here.
At least this way you will always be guaranteed to order the events in the order they arrived at the Segment ingestion point.
Uh oh!
There was an error while loading. Please reload this page.
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.
So what I'm recommending is:
const incomingTimestamp = isTimestamp(incoming.timestamp) ? incoming.timestamp : getTimestampFromRawPayload()