forked from rudderlabs/rudder-transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversionedRouter.js
930 lines (871 loc) · 28.5 KB
/
versionedRouter.js
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/* eslint-disable import/no-dynamic-require */
/* eslint-disable global-require */
const Router = require("koa-router");
const _ = require("lodash");
const fs = require("fs");
const path = require("path");
const { ConfigFactory, Executor } = require("rudder-transformer-cdk");
const logger = require("./logger");
const stats = require("./util/stats");
const { SUPPORTED_VERSIONS, API_VERSION } = require("./routes/utils/constants");
const {
isNonFuncObject,
getMetadata,
generateErrorObject,
CustomError,
isHttpStatusSuccess
} = require("./v0/util");
const { processDynamicConfig } = require("./util/dynamicConfig");
const { DestHandlerMap } = require("./constants/destinationCanonicalNames");
const { userTransformHandler } = require("./routerUtils");
const { TRANSFORMER_METRIC } = require("./v0/util/constant");
const networkHandlerFactory = require("./adapters/networkHandlerFactory");
const profilingRouter = require("./routes/profiling");
const destProxyRoutes = require("./routes/destinationProxy");
const { isCdkDestination } = require("./v0/util");
require("dotenv").config();
const eventValidator = require("./util/eventValidation");
const { prometheusRegistry } = require("./middleware");
const { compileUserLibrary } = require("./util/ivmFactory");
const { getIntegrations } = require("./routes/utils");
const { RespStatusError } = require("./util/utils");
const CDK_DEST_PATH = "cdk";
const basePath = path.resolve(__dirname, `./${CDK_DEST_PATH}`);
ConfigFactory.init({ basePath, loggingMode: "production" });
const transformerMode = process.env.TRANSFORMER_MODE;
const startDestTransformer =
transformerMode === "destination" || !transformerMode;
const startSourceTransformer = transformerMode === "source" || !transformerMode;
const transformerProxy = process.env.TRANSFORMER_PROXY || true;
const proxyTestModeEnabled =
process.env.TRANSFORMER_PROXY_TEST_ENABLED?.toLowerCase() === "true" || false;
const transformerTestModeEnabled = process.env.TRANSFORMER_TEST_MODE
? process.env.TRANSFORMER_TEST_MODE.toLowerCase() === "true"
: false;
const router = new Router();
// Router for assistance in profiling
router.use(profilingRouter);
const getDestHandler = (version, dest) => {
if (DestHandlerMap.hasOwnProperty(dest)) {
return require(`./${version}/destinations/${DestHandlerMap[dest]}/transform`);
}
return require(`./${version}/destinations/${dest}/transform`);
};
const getDestFileUploadHandler = (version, dest) => {
return require(`./${version}/destinations/${dest}/fileUpload`);
};
const getPollStatusHandler = (version, dest) => {
return require(`./${version}/destinations/${dest}/poll`);
};
const getJobStatusHandler = (version, dest) => {
return require(`./${version}/destinations/${dest}/fetchJobStatus`);
};
const getDeletionUserHandler = (version, dest) => {
return require(`./${version}/destinations/${dest}/deleteUsers`);
};
const getSourceHandler = (version, source) => {
return require(`./${version}/sources/${source}/transform`);
};
let areFunctionsEnabled = -1;
const functionsEnabled = () => {
if (areFunctionsEnabled === -1) {
areFunctionsEnabled = process.env.ENABLE_FUNCTIONS === "false" ? 0 : 1;
}
return areFunctionsEnabled === 1;
};
async function handleDest(ctx, version, destination) {
const events = ctx.request.body;
if (!Array.isArray(events) || events.length === 0) {
throw new CustomError("Event is missing or in inappropriate format", 400);
}
const reqParams = ctx.request.query;
logger.debug(`[DT] Input events: ${JSON.stringify(events)}`);
const metaTags =
events && events.length && events[0].metadata
? getMetadata(events[0].metadata)
: {};
stats.increment("dest_transform_input_events", events.length, {
destination,
version,
...metaTags
});
const respList = [];
const executeStartTime = new Date();
let destHandler;
// Getting destination handler for non-cdk destination(s)
if (!isCdkDestination(events[0])) {
destHandler = getDestHandler(version, destination);
}
await Promise.all(
events.map(async event => {
try {
let parsedEvent = event;
parsedEvent.request = { query: reqParams };
parsedEvent = processDynamicConfig(parsedEvent);
let respEvents;
if (isCdkDestination(parsedEvent)) {
respEvents = await Executor.execute(
parsedEvent,
ConfigFactory.getConfig(destination)
);
} else {
respEvents = await destHandler.process(parsedEvent);
}
if (respEvents) {
if (!Array.isArray(respEvents)) {
respEvents = [respEvents];
}
respList.push(
...respEvents.map(ev => {
let { userId } = ev;
// Set the user ID to an empty string for
// all the falsy values (including 0 and false)
// Otherwise, server panics while un-marshalling the response
// while expecting only strings.
if (!userId) {
userId = "";
}
if (ev.statusCode !== 400 && userId) {
userId = `${userId}`;
}
return {
output: { ...ev, userId },
metadata: event.metadata,
statusCode: 200
};
})
);
}
} catch (error) {
logger.error(error);
const errObj = generateErrorObject(
error,
destination,
TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM
);
respList.push({
metadata: event.metadata,
statusCode: errObj.status,
error: errObj.message || "Error occurred while processing payload.",
statTags: errObj.statTags
});
}
})
);
stats.timing("cdk_events_latency", executeStartTime, {
destination,
...metaTags
});
logger.debug(`[DT] Output events: ${JSON.stringify(respList)}`);
stats.increment("dest_transform_output_events", respList.length, {
destination,
version,
...metaTags
});
ctx.body = respList;
return ctx.body;
}
async function handleValidation(ctx) {
const requestStartTime = new Date();
const events = ctx.request.body;
const requestSize = ctx.request.get("content-length");
const reqParams = ctx.request.query;
const respList = [];
const metaTags = events[0].metadata ? getMetadata(events[0].metadata) : {};
for (let i = 0; i < events.length; i++) {
const event = events[i];
const eventStartTime = new Date();
try {
const parsedEvent = event;
parsedEvent.request = { query: reqParams };
const hv = await eventValidator.handleValidation(parsedEvent);
if (hv.dropEvent) {
const errMessage = `Error occurred while validating because : ${hv.violationType}`;
respList.push({
output: event.message,
metadata: event.metadata,
statusCode: 400,
validationErrors: hv.validationErrors,
errors: errMessage
});
stats.counter("hv_violation_type", 1, {
violationType: hv.violationType,
...metaTags
});
} else {
respList.push({
output: event.message,
metadata: event.metadata,
statusCode: 200,
validationErrors: hv.validationErrors
});
stats.counter("hv_errors", 1, {
...metaTags
});
}
} catch (error) {
const errMessage = `Error occurred while validating : ${error}`;
logger.error(errMessage);
respList.push({
output: event.message,
metadata: event.metadata,
statusCode: 200,
validationErrors: [],
error: errMessage
});
stats.counter("hv_errors", 1, {
...metaTags
});
} finally {
stats.timing("hv_event_latency", eventStartTime, {
...metaTags
});
}
}
ctx.body = respList;
ctx.set("apiVersion", API_VERSION);
stats.counter("hv_events_count", events.length, {
...metaTags
});
stats.counter("hv_request_size", requestSize, {
...metaTags
});
stats.timing("hv_request_latency", requestStartTime, {
...metaTags
});
}
async function routerHandleDest(ctx) {
const { destType, input } = ctx.request.body;
const routerDestHandler = getDestHandler("v0", destType);
if (!routerDestHandler || !routerDestHandler.processRouterDest) {
ctx.status = 404;
ctx.body = `${destType} doesn't support router transform`;
return null;
}
const respEvents = [];
const allDestEvents = _.groupBy(input, event => event.destination.ID);
await Promise.all(
Object.entries(allDestEvents).map(async ([destID, desInput]) => {
desInput = processDynamicConfig(desInput, "router");
const listOutput = await routerDestHandler.processRouterDest(desInput);
respEvents.push(...listOutput);
})
);
ctx.body = { output: respEvents };
return ctx.body;
}
if (startDestTransformer) {
SUPPORTED_VERSIONS.forEach(version => {
const destinations = getIntegrations(`${version}/destinations`);
destinations.push(...getIntegrations(CDK_DEST_PATH));
destinations.forEach(destination => {
// eg. v0/destinations/ga
router.post(`/${version}/destinations/${destination}`, async ctx => {
const startTime = new Date();
await handleDest(ctx, version, destination);
ctx.set("apiVersion", API_VERSION);
// Assuming that events are from one single source
const metaTags =
ctx.request.body &&
ctx.request.body.length &&
ctx.request.body[0].metadata
? getMetadata(ctx.request.body[0].metadata)
: {};
stats.timing("dest_transform_request_latency", startTime, {
destination,
version,
...metaTags
});
stats.increment("dest_transform_requests", 1, {
destination,
version,
...metaTags
});
});
// eg. v0/ga. will be deprecated in favor of v0/destinations/ga format
router.post(`/${version}/${destination}`, async ctx => {
const startTime = new Date();
await handleDest(ctx, version, destination);
ctx.set("apiVersion", API_VERSION);
// Assuming that events are from one single source
const metaTags =
ctx.request.body &&
ctx.request.body.length &&
ctx.request.body[0].metadata
? getMetadata(ctx.request.body[0].metadata)
: {};
stats.timing("dest_transform_request_latency", startTime, {
destination,
...metaTags
});
stats.increment("dest_transform_requests", 1, {
destination,
version,
...metaTags
});
});
router.post("/routerTransform", async ctx => {
ctx.set("apiVersion", API_VERSION);
await routerHandleDest(ctx);
});
});
});
if (functionsEnabled()) {
router.post("/customTransform", async ctx => {
const startTime = new Date();
const events = ctx.request.body;
const { processSessions } = ctx.query;
logger.debug(`[CT] Input events: ${JSON.stringify(events)}`);
stats.counter("user_transform_input_events", events.length, {
processSessions
});
let groupedEvents;
if (processSessions) {
groupedEvents = _.groupBy(events, event => {
// to have the backward-compatibility and being extra careful. We need to remove this (message.anonymousId) in next release.
const rudderId = event.metadata.rudderId || event.message.anonymousId;
return `${event.destination.ID}_${event.metadata.sourceId}_${rudderId}`;
});
} else {
groupedEvents = _.groupBy(
events,
event => `${event.metadata.destinationId}_${event.metadata.sourceId}`
);
}
stats.counter(
"user_transform_function_group_size",
Object.entries(groupedEvents).length,
{ processSessions }
);
const transformedEvents = [];
let librariesVersionIDs = [];
if (events[0].libraries) {
librariesVersionIDs = events[0].libraries.map(
library => library.VersionID
);
}
await Promise.all(
Object.entries(groupedEvents).map(async ([dest, destEvents]) => {
logger.debug(`dest: ${dest}`);
const transformationVersionId =
destEvents[0] &&
destEvents[0].destination &&
destEvents[0].destination.Transformations &&
destEvents[0].destination.Transformations[0] &&
destEvents[0].destination.Transformations[0].VersionID;
const messageIds = destEvents.map(
ev => ev.metadata && ev.metadata.messageId
);
const commonMetadata = {
sourceId: destEvents[0].metadata && destEvents[0].metadata.sourceId,
destinationId:
destEvents[0].metadata && destEvents[0].metadata.destinationId,
destinationType:
destEvents[0].metadata && destEvents[0].metadata.destinationType,
messageIds
};
const metaTags =
destEvents.length && destEvents[0].metadata
? getMetadata(destEvents[0].metadata)
: {};
const userFuncStartTime = new Date();
if (transformationVersionId) {
let destTransformedEvents;
try {
stats.counter(
"user_transform_function_input_events",
destEvents.length,
{
processSessions,
...metaTags
}
);
destTransformedEvents = await userTransformHandler()(
destEvents,
transformationVersionId,
librariesVersionIDs
);
transformedEvents.push(
...destTransformedEvents.map(ev => {
if (ev.error) {
return {
statusCode: 400,
error: ev.error,
metadata: _.isEmpty(ev.metadata)
? commonMetadata
: ev.metadata
};
}
if (!isNonFuncObject(ev.transformedEvent)) {
return {
statusCode: 400,
error: `returned event in events from user transformation is not an object. transformationVersionId:${transformationVersionId} and returned event: ${JSON.stringify(
ev.transformedEvent
)}`,
metadata: _.isEmpty(ev.metadata)
? commonMetadata
: ev.metadata
};
}
return {
output: ev.transformedEvent,
metadata: _.isEmpty(ev.metadata)
? commonMetadata
: ev.metadata,
statusCode: 200
};
})
);
} catch (error) {
logger.error(error);
let status = 400;
const errorString = error.toString();
if (error instanceof RespStatusError) {
status = error.statusCode;
}
destTransformedEvents = destEvents.map(e => {
return {
statusCode: status,
metadata: e.metadata,
error: errorString
};
});
transformedEvents.push(...destTransformedEvents);
stats.counter("user_transform_errors", destEvents.length, {
transformationVersionId,
processSessions,
...metaTags
});
} finally {
stats.timing(
"user_transform_function_latency",
userFuncStartTime,
{ transformationVersionId, processSessions, ...metaTags }
);
}
} else {
const errorMessage = "Transformation VersionID not found";
logger.error(`[CT] ${errorMessage}`);
transformedEvents.push({
statusCode: 400,
error: errorMessage,
metadata: commonMetadata
});
stats.counter("user_transform_errors", destEvents.length, {
transformationVersionId,
processSessions,
...metaTags
});
}
})
);
logger.debug(`[CT] Output events: ${JSON.stringify(transformedEvents)}`);
ctx.body = transformedEvents;
ctx.set("apiVersion", API_VERSION);
stats.timing("user_transform_request_latency", startTime, {
processSessions
});
stats.increment("user_transform_requests", 1, { processSessions });
stats.counter("user_transform_output_events", transformedEvents.length, {
processSessions
});
});
}
}
if (transformerTestModeEnabled) {
router.post("/transformation/test", async ctx => {
try {
const { events, trRevCode, libraryVersionIDs = [] } = ctx.request.body;
if (!trRevCode || !trRevCode.code || !trRevCode.codeVersion) {
throw new Error(
"Invalid Request. Missing parameters in transformation code block"
);
}
if (!events || events.length === 0) {
throw new Error("Invalid request. Missing events");
}
logger.debug(`[CT] Test Input Events: ${JSON.stringify(events)}`);
trRevCode.versionId = "testVersionId";
const res = await userTransformHandler()(
events,
trRevCode.versionId,
libraryVersionIDs,
trRevCode,
true
);
logger.debug(
`[CT] Test Output Events: ${JSON.stringify(res.transformedEvents)}`
);
ctx.body = res;
} catch (error) {
ctx.status = 400;
ctx.body = { error: error.message };
}
});
router.post("/transformationLibrary/test", async ctx => {
try {
const { code } = ctx.request.body;
if (!code) {
throw new Error("Invalid request. Missing code");
}
const res = await compileUserLibrary(code);
ctx.body = res;
} catch (error) {
ctx.body = { error: error.message };
ctx.status = 400;
}
});
}
async function handleSource(ctx, version, source) {
const sourceHandler = getSourceHandler(version, source);
const events = ctx.request.body;
logger.debug(`[ST] Input source events: ${JSON.stringify(events)}`);
stats.increment("source_transform_input_events", events.length, {
source,
version
});
const respList = [];
await Promise.all(
events.map(async event => {
try {
const respEvents = await sourceHandler.process(event);
// We send response back to the source
// through outputToSource. This is not sent to gateway
if (
Object.prototype.hasOwnProperty.call(respEvents, "outputToSource")
) {
respList.push(respEvents);
return;
}
if (Array.isArray(respEvents)) {
respList.push({ output: { batch: respEvents } });
} else {
respList.push({ output: { batch: [respEvents] } });
}
} catch (error) {
logger.error(error);
respList.push({
statusCode: 400,
error: error.message || "Error occurred while processing payload."
});
stats.counter("source_transform_errors", events.length, {
source,
version
});
}
})
);
logger.debug(`[ST] Output source events: ${JSON.stringify(respList)}`);
stats.increment("source_transform_output_events", respList.length, {
source,
version
});
ctx.body = respList;
ctx.set("apiVersion", API_VERSION);
}
if (startSourceTransformer) {
SUPPORTED_VERSIONS.forEach(version => {
const sources = getIntegrations(`${version}/sources`);
sources.forEach(source => {
// eg. v0/sources/customerio
router.post(`/${version}/sources/${source}`, async ctx => {
const startTime = new Date();
await handleSource(ctx, version, source);
stats.timing("source_transform_request_latency", startTime, {
source,
version
});
stats.increment("source_transform_requests", 1, { source, version });
});
});
});
}
async function handleProxyRequest(destination, ctx) {
const destinationRequest = ctx.request.body;
const destNetworkHandler = networkHandlerFactory.getNetworkHandler(
destination
);
let response;
try {
stats.counter("tf_proxy_dest_req_count", 1, {
destination
});
const startTime = new Date();
const rawProxyResponse = await destNetworkHandler.proxy(destinationRequest);
stats.timing("transformer_proxy_time", startTime, {
destination
});
stats.counter("tf_proxy_dest_resp_count", 1, {
destination,
success: rawProxyResponse.success
});
const processedProxyResponse = destNetworkHandler.processAxiosResponse(
rawProxyResponse
);
stats.counter("tf_proxy_proc_ax_response_count", 1, {
destination
});
response = destNetworkHandler.responseHandler(
processedProxyResponse,
destination
);
stats.counter("tf_proxy_resp_handler_count", 1, {
destination
});
} catch (err) {
logger.error("Error occurred while completing proxy request:");
logger.error(err);
response = generateErrorObject(
err,
destination,
TRANSFORMER_METRIC.TRANSFORMER_STAGE.RESPONSE_TRANSFORM
);
response = { ...response };
if (!err.responseTransformFailure) {
response.message = `[Error occurred while processing response for destination ${destination}]: ${err.message}`;
}
stats.counter("tf_proxy_err_count", 1, {
destination
});
}
ctx.body = { output: response };
// Sending `204` status(obtained from destination) is not working as expected
// Since this is success scenario, we'll be forcefully sending `200` status-code to server
ctx.status = isHttpStatusSuccess(response.status) ? 200 : response.status;
return ctx.body;
}
if (transformerProxy) {
SUPPORTED_VERSIONS.forEach(version => {
const destinations = getIntegrations(`${version}/destinations`);
destinations.forEach(destination => {
router.post(
`/${version}/destinations/${destination}/proxy`,
async ctx => {
const startTime = new Date();
ctx.set("apiVersion", API_VERSION);
await handleProxyRequest(destination, ctx);
stats.timing("transformer_total_proxy_latency", startTime, {
destination,
version
});
}
);
});
});
}
if (proxyTestModeEnabled) {
router.use(destProxyRoutes);
}
router.get("/version", ctx => {
ctx.body = process.env.npm_package_version || "Version Info not found";
});
router.get("/transformerBuildVersion", ctx => {
ctx.body = process.env.transformer_build_version || "Version Info not found";
});
router.get("/health", ctx => {
ctx.body = "OK";
});
router.get("/features", ctx => {
const obj = JSON.parse(fs.readFileSync("features.json", "utf8"));
ctx.body = JSON.stringify(obj);
});
const batchHandler = ctx => {
const { destType, input } = ctx.request.body;
const destHandler = getDestHandler("v0", destType);
if (!destHandler || !destHandler.batch) {
ctx.status = 404;
ctx.body = `${destType} doesn't support batching`;
return null;
}
const allDestEvents = _.groupBy(input, event => event.destination.ID);
const response = { batchedRequests: [], errors: [] };
Object.entries(allDestEvents).map(async ([destID, destEvents]) => {
// TODO: check await needed?
try {
destEvents = processDynamicConfig(destEvents, "batch");
const destBatchedRequests = destHandler.batch(destEvents);
response.batchedRequests.push(...destBatchedRequests);
} catch (error) {
response.errors.push(
error.message || "Error occurred while processing payload."
);
}
});
if (response.errors.length > 0) {
ctx.status = 500;
ctx.body = response.errors;
return null;
}
ctx.body = response.batchedRequests;
return ctx.body;
};
router.post("/batch", ctx => {
ctx.set("apiVersion", API_VERSION);
batchHandler(ctx);
});
const fileUpload = async ctx => {
const { destType } = ctx.request.body;
const destFileUploadHandler = getDestFileUploadHandler(
"v0",
destType.toLowerCase()
);
if (!destFileUploadHandler || !destFileUploadHandler.processFileData) {
ctx.status = 404;
ctx.body = `${destType} doesn't support bulk upload`;
return null;
}
let response;
try {
response = await destFileUploadHandler.processFileData(ctx.request.body);
} catch (error) {
response = {
statusCode: error.response ? error.response.status : 400,
error: error.message || "Error occurred while processing payload.",
metadata: error.response ? error.response.metadata : null
};
}
ctx.body = response;
return ctx.body;
};
const pollStatus = async ctx => {
const { destType } = ctx.request.body;
const destFileUploadHandler = getPollStatusHandler(
"v0",
destType.toLowerCase()
);
let response;
if (!destFileUploadHandler || !destFileUploadHandler.processPolling) {
ctx.status = 404;
ctx.body = `${destType} doesn't support bulk upload`;
return null;
}
try {
response = await destFileUploadHandler.processPolling(ctx.request.body);
} catch (error) {
response = {
statusCode: error.response ? error.response.status : 400,
error: error.message || "Error occurred while processing payload."
};
}
ctx.body = response;
return ctx.body;
};
const getJobStatus = async (ctx, type) => {
const { destType } = ctx.request.body;
const destFileUploadHandler = getJobStatusHandler(
"v0",
destType.toLowerCase()
);
if (!destFileUploadHandler || !destFileUploadHandler.processJobStatus) {
ctx.status = 404;
ctx.body = `${destType} doesn't support bulk upload`;
return null;
}
let response;
try {
response = await destFileUploadHandler.processJobStatus(
ctx.request.body,
type
);
} catch (error) {
response = {
statusCode: error.response ? error.response.status : 400,
error: error.message || "Error occurred while processing payload."
};
}
ctx.body = response;
return ctx.body;
};
const handleDeletionOfUsers = async ctx => {
const { body } = ctx.request;
const respList = [];
let response;
await Promise.all(
body.map(async b => {
const { destType } = b;
const destUserDeletionHandler = getDeletionUserHandler(
"v0",
destType.toLowerCase()
);
if (
!destUserDeletionHandler ||
!destUserDeletionHandler.processDeleteUsers
) {
ctx.status = 404;
ctx.body = "Doesn't support deletion of users";
return null;
}
try {
response = await destUserDeletionHandler.processDeleteUsers(b);
if (response) {
respList.push(response);
}
} catch (error) {
// adding the status to the request
ctx.status = error.response ? error.response.status : 400;
respList.push({
statusCode: error.response ? error.response.status : 400,
error: error.message || "Error occured while processing"
});
}
})
);
ctx.body = respList;
return ctx.body;
// const { destType } = ctx.request.body;
};
const metricsController = async ctx => {
ctx.status = 200;
ctx.type = prometheusRegistry.contentType;
ctx.body = await prometheusRegistry.metrics();
return ctx.body;
};
router.post("/fileUpload", async ctx => {
await fileUpload(ctx);
});
router.post("/pollStatus", async ctx => {
await pollStatus(ctx);
});
router.post("/getFailedJobs", async ctx => {
await getJobStatus(ctx, "fail");
});
router.post("/getWarningJobs", async ctx => {
await getJobStatus(ctx, "warn");
});
// eg. v0/validate. will validate events as per respective tracking plans
router.post(`/v0/validate`, async ctx => {
await handleValidation(ctx);
});
// Api to handle deletion of users for data regulation
// {
// "destType": "dest name",
// "userAttributes": [
// {
// "userId": "user_1"
// },
// {
// "userId": "user_2"
// }
// ],
// "config": {
// "apiKey": "",
// "apiSecret": ""
// }
// }
router.post(`/deleteUsers`, async ctx => {
await handleDeletionOfUsers(ctx);
});
router.get("/metrics", async ctx => {
await metricsController(ctx);
});
module.exports = {
router,
handleDest,
routerHandleDest,
batchHandler,
handleProxyRequest,
handleDeletionOfUsers,
fileUpload,
pollStatus,
getJobStatus
};