-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsinkMainProcessors.ts
306 lines (245 loc) · 8.99 KB
/
sinkMainProcessors.ts
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { bulkUpdateDataWrapper, ErrorType, logError, getItems } from "../libs";
import { KafkaRecord, opensearch, SeatoolRecordWithUpdatedDate } from "shared-types";
import { Document, transforms } from "shared-types/opensearch/main";
import { decodeBase64WithUtf8 } from "shared-utils";
import { isBefore } from "date-fns";
import {
deleteAdminChangeSchema,
updateValuesAdminChangeSchema,
updateIdAdminChangeSchema,
} from "./update/adminChangeSchemas";
const removeDoubleQuotesSurroundingString = (str: string) => str.replace(/^"|"$/g, "");
const adminRecordSchema = deleteAdminChangeSchema
.or(updateValuesAdminChangeSchema)
.or(updateIdAdminChangeSchema);
type OneMacRecord = {
id: string;
[key: string]: unknown | undefined;
};
type ParsedRecordFromKafka = Partial<{
event: string;
origin: string;
isAdminChange: boolean;
adminChangeType: string;
}>;
const isRecordAOneMacRecord = (
record: ParsedRecordFromKafka,
): record is { event: keyof typeof transforms } =>
typeof record === "object" &&
record?.event !== undefined &&
record.event in transforms &&
record?.origin === "mako";
const isRecordAnAdminOneMacRecord = (
record: ParsedRecordFromKafka,
): record is { adminChangeType: string; isAdminChange: boolean } =>
typeof record === "object" &&
record?.isAdminChange === true &&
record?.adminChangeType !== undefined;
const getOneMacRecordWithAllProperties = (
value: string,
topicPartition: string,
kafkaRecord: KafkaRecord,
): OneMacRecord | undefined => {
const record = JSON.parse(decodeBase64WithUtf8(value));
if (isRecordAnAdminOneMacRecord(record)) {
const safeRecord = adminRecordSchema.safeParse(record);
if (safeRecord.success === false) {
console.log(`Skipping package with invalid format for type "${record.adminChangeType}"`);
logError({
type: ErrorType.VALIDATION,
error: safeRecord.error.errors,
metadata: { topicPartition, kafkaRecord, record },
});
return;
}
const { data: oneMacAdminRecord } = safeRecord;
console.log(`admin record: ${JSON.stringify(oneMacAdminRecord, null, 2)}`);
return oneMacAdminRecord;
}
if (isRecordAOneMacRecord(record)) {
const transformForEvent = transforms[record.event];
const safeEvent = transformForEvent.transform().safeParse(record);
if (safeEvent.success === false) {
logError({
type: ErrorType.VALIDATION,
error: safeEvent.error.errors,
metadata: { topicPartition, kafkaRecord, record },
});
return;
}
const { data: oneMacRecord } = safeEvent;
console.log(`event after transformation: ${JSON.stringify(oneMacRecord, null, 2)}`);
return oneMacRecord;
} else {
console.log(`No transform found for event: ${record.event}`);
}
return;
};
/**
* Processes incoming new records from the OneMac user interface and adds them to Mako
* @param kafkaRecords records to process
* @param topicPartition kafka topic for verbose error handling
*/
export const insertOneMacRecordsFromKafkaIntoMako = async (
kafkaRecords: KafkaRecord[],
topicPartition: string,
) => {
const oneMacRecordsForMako = kafkaRecords.reduce<OneMacRecord[]>((collection, kafkaRecord) => {
console.log(`record: ${JSON.stringify(kafkaRecord, null, 2)}`);
try {
const { value } = kafkaRecord;
if (!value) {
return collection;
}
const oneMacRecordWithAllProperties = getOneMacRecordWithAllProperties(
value,
topicPartition,
kafkaRecord,
);
if (oneMacRecordWithAllProperties) {
return collection.concat(oneMacRecordWithAllProperties);
}
} catch (error) {
logError({
type: ErrorType.BADPARSE,
error,
metadata: { topicPartition, kafkaRecord },
});
}
return collection;
}, []);
await bulkUpdateDataWrapper(oneMacRecordsForMako, "main");
};
const getMakoDocTimestamps = async (kafkaRecords: KafkaRecord[]) => {
const kafkaIds = kafkaRecords.map((record) =>
removeDoubleQuotesSurroundingString(decodeBase64WithUtf8(record.key)),
);
const openSearchRecords = await getItems(kafkaIds);
return openSearchRecords.reduce<Map<string, number>>((map, item) => {
if (item.changedDate !== null) {
map.set(item.id, new Date(item.changedDate).getTime());
}
return map;
}, new Map());
};
/**
* Processes new SEATOOL records and reconciles them with existing Mako records
* @param kafkaRecords records to process
* @param topicPartition kafka topic for verbose error handling
*/
export const insertNewSeatoolRecordsFromKafkaIntoMako = async (
kafkaRecords: KafkaRecord[],
topicPartition: string,
) => {
const makoDocTimestamps = await getMakoDocTimestamps(kafkaRecords);
const seatoolRecordsForMako = kafkaRecords.reduce<{ id: string; [key: string]: unknown }[]>(
(collection, kafkaRecord) => {
try {
const { key, value } = kafkaRecord;
if (!key) {
console.log(`Record without a key property: ${value}`);
return collection;
}
const id: string = removeDoubleQuotesSurroundingString(decodeBase64WithUtf8(key));
if (!value) {
// record in seatool has been deleted
// nulls the seatool properties from the record
// seatool record would now only have mako properties
console.log(`Record without a value property: ${value}`);
return collection.concat(opensearch.main.seatool.tombstone(id));
}
const seatoolRecord: Document = {
id,
...JSON.parse(decodeBase64WithUtf8(value)),
};
const safeSeatoolRecord = opensearch.main.seatool.transform(id).safeParse(seatoolRecord);
if (safeSeatoolRecord.success === false) {
logError({
type: ErrorType.VALIDATION,
error: safeSeatoolRecord.error.errors,
metadata: { topicPartition, kafkaRecord, record: seatoolRecord },
});
return collection;
}
const { data: seatoolDocument } = safeSeatoolRecord;
const makoDocumentTimestamp = makoDocTimestamps.get(seatoolDocument.id);
console.log("--------------------");
console.log(`id: ${seatoolDocument.id}`);
console.log(`mako: ${makoDocumentTimestamp}`);
console.log(`seatool: ${seatoolDocument.changed_date}`);
const isOlderThanMako =
seatoolDocument.changed_date &&
makoDocumentTimestamp &&
isBefore(seatoolDocument.changed_date, makoDocumentTimestamp);
if (isOlderThanMako) {
console.log("SKIPPED DUE TO OUT-OF-DATE INFORMATION");
return collection;
}
if (seatoolDocument.authority && seatoolDocument.seatoolStatus !== "Unknown") {
console.log("INDEX");
console.log("--------------------");
console.log(`Status: ${seatoolDocument.seatoolStatus}`);
return collection.concat({ ...seatoolDocument });
}
} catch (error) {
logError({
type: ErrorType.BADPARSE,
error,
metadata: { topicPartition, kafkaRecord },
});
}
return collection;
},
[],
);
await bulkUpdateDataWrapper(seatoolRecordsForMako, "main");
};
/**
* Syncs date updates in SEATOOL records with Mako, offloading processing from `insertNewSeatoolRecordsFromKafkaIntoMako`
* @param kafkaRecords records with updated date payload
* @param topicPartition kafka topic for verbose error handling
*/
export const syncSeatoolRecordDatesFromKafkaWithMako = async (
kafkaRecords: KafkaRecord[],
topicPartition: string,
) => {
const recordIdsWithUpdatedDates = kafkaRecords.reduce<
{ id: string; changedDate: string | null }[]
>((collection, kafkaRecord) => {
const { value } = kafkaRecord;
try {
if (!value) {
console.log(`Record without a value property: ${value}`);
return collection;
}
const payloadWithUpdatedDate: { payload?: { after?: SeatoolRecordWithUpdatedDate | null } } =
decodeBase64WithUtf8(value) as any;
// .after could be `null` or `undefined`
if (!payloadWithUpdatedDate?.payload?.after) {
return collection;
}
const { after: recordWithUpdatedDate } = payloadWithUpdatedDate.payload;
const safeRecordWithIdAndUpdatedDate = opensearch.main.changedDate
.transform()
.safeParse(recordWithUpdatedDate);
if (safeRecordWithIdAndUpdatedDate.success === false) {
logError({
type: ErrorType.VALIDATION,
error: safeRecordWithIdAndUpdatedDate.error.errors,
metadata: { topicPartition, kafkaRecord, recordWithUpdatedDate },
});
return collection;
}
const { data: idAndUpdatedDate } = safeRecordWithIdAndUpdatedDate;
return collection.concat(idAndUpdatedDate);
} catch (error) {
logError({
type: ErrorType.BADPARSE,
error,
metadata: { topicPartition, kafkaRecord },
});
}
return collection;
}, []);
await bulkUpdateDataWrapper(recordIdsWithUpdatedDates, "main");
};