-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathduplicateKeyTracker.ts
More file actions
284 lines (252 loc) · 8.17 KB
/
duplicateKeyTracker.ts
File metadata and controls
284 lines (252 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import {
Entity,
GraphObjectStore,
IntegrationDuplicateKeyError,
KeyNormalizationFunction,
} from '@jupiterone/integration-sdk-core';
import { deepStrictEqual } from 'assert';
import { BigMap } from './utils/bigMap';
const DUPLICATE_KEY_TRACKER_DEFAULT_MAP_KEY_SPACE = 2_000_000;
export interface DuplicateKeyTracker {
getEncounteredKeys(): string[][];
registerKey(_key: string): void;
getGraphObjectMetadata(_key: string): string | undefined;
hasKey(_key: string): boolean;
}
/**
* Contains a map of every graph object key to a specific set of metadata about
* the graph object used for filtering. For example, we use the `_type` property
* on graph objects as a method of filtering data down when iterating entities
* or relationships. We store the `_type` inside the metadata for a fast lookup
* table.
*/
export class InMemoryDuplicateKeyTracker implements DuplicateKeyTracker {
private readonly graphObjectKeyMap: BigMap<string, string>;
private readonly normalizationFunction: KeyNormalizationFunction;
constructor(normalizationFunction?: KeyNormalizationFunction) {
this.normalizationFunction = normalizationFunction || ((_key) => _key);
this.graphObjectKeyMap = new BigMap<string, string>(
DUPLICATE_KEY_TRACKER_DEFAULT_MAP_KEY_SPACE,
);
}
getEncounteredKeys() {
return this.graphObjectKeyMap.keys();
}
registerKey(_key: string) {
const normalizedKey = this.normalizationFunction(_key);
if (this.graphObjectKeyMap.has(normalizedKey)) {
throw new IntegrationDuplicateKeyError(
`Duplicate _key detected (_key=${_key})`,
);
}
this.graphObjectKeyMap.set(normalizedKey, _key);
}
getGraphObjectMetadata(_key: string) {
return this.graphObjectKeyMap.get(this.normalizationFunction(_key));
}
hasKey(_key: string) {
return this.graphObjectKeyMap.has(this.normalizationFunction(_key));
}
}
type DuplicateKeyReportParams = {
duplicateEntity: Entity;
payload: Entity[];
indexOfDuplicateKey: number;
graphObjectStore: GraphObjectStore;
};
export async function createDuplicateEntityReport({
duplicateEntity,
payload,
indexOfDuplicateKey,
graphObjectStore,
}: DuplicateKeyReportParams): Promise<DuplicateEntityReport | {}> {
const originalEntityFromPayload = getOriginalEntityFromPayload(
payload,
duplicateEntity._key,
indexOfDuplicateKey,
);
if (originalEntityFromPayload) {
return compareEntities(originalEntityFromPayload, duplicateEntity);
} else {
const originalEntityFromGraphObjectStore =
await graphObjectStore.findEntity(duplicateEntity._key);
return compareEntities(originalEntityFromGraphObjectStore, duplicateEntity);
}
}
/**
* Determines if the original entity that is duplicated by the new entity with the same key
* is inside the payload or in the graphObjectStore.
*
* @param payload the payload a duplicate _key was found in
* @param _key the _key that is a duplicate
* @param duplicateFoundIndex the index of the Entity or Relationship that triggered the DUPLICATE_KEY_ERROR
* @returns the original entity or relationship if it is inside the payload, otherwise returns undefined
*/
function getOriginalEntityFromPayload(
payload: Entity[],
_key: string,
duplicateFoundIndex: number,
): Entity | undefined {
return payload.find((v, i) => {
if (i >= duplicateFoundIndex) {
return undefined;
} else if (v._key === _key) {
return v;
}
});
}
/**
* isDeepStrictEqual deeply compares two values and returns true if they are equal
* @param a any
* @param b any
* @returns boolean representing if the two values are deeply equal
*/
function isDeepStrictEqual(a: any, b: any): boolean {
try {
deepStrictEqual(a, b);
return true;
} catch {
return false;
}
}
export type DuplicateEntityReport = {
_key: string;
rawDataMatch?: boolean;
entityPropertiesMatch?: boolean;
rawDataDiff?: string;
entityPropertiesDiff?: string;
diffErrors?: { rawData?: string; entityProperties?: string };
};
type DiffType =
| 'missing_in_original'
| 'missing_in_duplicate'
| 'type_mismatch'
| 'value_mismatch'
| 'array_values_mismatch';
interface ObjectDiff {
[key: string]: {
type: DiffType;
valueTypes?: { src: string; dest: string };
};
}
/**
* Compares two objects and returns the differences between them.
*
* @param {unknown} originalObject - The source object to compare.
* @param {unknown} duplicateObject - The destination object to compare.
* @param {string} [path=''] - The base path for keys, used for tracking nested object differences.
* @returns {ObjectDiff} An object representing the differences between `original` and `duplicate`.
* Each key corresponds to a path in the objects, with details about the type of difference.
*
* @example
* const originalObj = { a: 1, b: { c: 2 } };
* const duplicateObj = { a: 1, b: { c: 3 }, d: 4 };
* const result = diffObjects(originalObj, duplicateObj);
* console.log(result);
* // Output:
* // {
* // "b.c": { type: "value_mismatch" },
* // "d": { type: "missing_in_original" }
* // }
*/
export function diffObjects(
originalObject: unknown,
duplicateObject: unknown,
path: string = '',
): ObjectDiff {
const diff = {};
// Helper to add differences
const addDiff = (
key: string,
diffType: DiffType,
valueTypes?: { original: string; duplicate: string },
) => {
diff[key] = { diffType, valueTypes };
};
// Iterate through the keys of both objects
const allKeys = new Set([
...Object.keys(originalObject || {}),
...Object.keys(duplicateObject || {}),
]);
const isObject = (val: unknown): val is Record<string, unknown> =>
typeof val === 'object' && val !== null;
for (const key of allKeys) {
const fullPath = path ? `${path}.${key}` : key;
const valOriginal = originalObject?.[key];
const valDuplicate = duplicateObject?.[key];
if (valOriginal === undefined) {
addDiff(fullPath, 'missing_in_original');
} else if (valDuplicate === undefined) {
addDiff(fullPath, 'missing_in_duplicate');
} else if (typeof valOriginal !== typeof valDuplicate) {
addDiff(fullPath, 'type_mismatch', {
original: typeof valOriginal,
duplicate: typeof valDuplicate,
});
} else if (Array.isArray(valOriginal) && Array.isArray(valDuplicate)) {
if (JSON.stringify(valOriginal) !== JSON.stringify(valDuplicate)) {
addDiff(fullPath, 'array_values_mismatch');
}
} else if (isObject(valOriginal) && isObject(valDuplicate)) {
// Recursive comparison for nested objects
const nestedDiff = diffObjects(valOriginal, valDuplicate, fullPath);
Object.assign(diff, nestedDiff);
} else if (valOriginal !== valDuplicate) {
addDiff(fullPath, 'value_mismatch');
}
}
return diff;
}
/**
* compareEntities compares two entities and produces a DuplicateEntityReport describing their
* similarities and differences.
* @param a
* @param b
* @returns
*/
function compareEntities(
a: Entity | undefined,
b: Entity,
): DuplicateEntityReport {
if (a === undefined) {
return {
_key: b._key,
};
}
const aClone = JSON.parse(JSON.stringify(a));
const bClone = JSON.parse(JSON.stringify(b));
delete aClone._rawData;
delete bClone._rawData;
const rawDataMatch = isDeepStrictEqual(a._rawData, b._rawData);
const entityPropertiesMatch = isDeepStrictEqual(aClone, bClone);
const diffErrors: { rawData?: string; entityProperties?: string } = {};
let rawDataDiff: ObjectDiff | undefined;
if (!rawDataMatch) {
try {
rawDataDiff = diffObjects(
a._rawData?.[0].rawData,
b._rawData?.[0].rawData,
);
} catch (e) {
diffErrors.rawData = e.message;
}
}
let entityPropertiesDiff: ObjectDiff | undefined;
if (!entityPropertiesMatch) {
try {
entityPropertiesDiff = diffObjects(aClone, bClone);
} catch (e) {
diffErrors.entityProperties = e.message;
}
}
return {
_key: a._key,
rawDataMatch,
entityPropertiesMatch,
...(rawDataDiff && { rawDataDiff: JSON.stringify(rawDataDiff) }),
...(entityPropertiesDiff && {
entityPropertiesDiff: JSON.stringify(entityPropertiesDiff),
}),
...(Object.keys(diffErrors).length > 0 && { diffErrors }),
};
}