Skip to content
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

feat: refactor router transformer - 4 #1690

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/v0/destinations/fb_custom_audience/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const {
returnArrayOfSubarrays,
isDefinedAndNotNull,
flattenMap,
generateErrorObject
generateErrorObject,
getEventReqMetadata
} = require("../../util");

const {
Expand All @@ -31,6 +32,10 @@ const {
ConfigurationError
} = require("../../util/errorTypes");

const {
client: errNotificationClient
} = require("../../../util/errorNotifier");

const getSchemaForEventMappedToDest = message => {
const mappedSchema = get(message, "context.destinationFields");
if (!mappedSchema) {
Expand Down Expand Up @@ -401,7 +406,7 @@ const process = event => {
return processEvent(event.message, event.destination);
};

const processRouterDest = inputs => {
const processRouterDest = (inputs, reqMetadata) => {
if (!Array.isArray(inputs) || inputs.length <= 0) {
const respEvents = getErrorRespEvents(null, 400, "Invalid event array");
return [respEvents];
Expand All @@ -427,12 +432,21 @@ const processRouterDest = inputs => {
return responseList;
} catch (error) {
const errObj = generateErrorObject(error);
return getErrorRespEvents(
const resp = getErrorRespEvents(
[input.metadata],
400,
error.message || "Error occurred while processing the payload.",
errObj.status,
errObj.message,
errObj.statTags
);
errNotificationClient.notify(
error,
"Router Transformation (event level)",
{
...resp,
...reqMetadata,
...getEventReqMetadata(input)
}
);
}
});
return flattenMap(respList);
Expand Down
35 changes: 7 additions & 28 deletions src/v0/destinations/googlepubsub/transform.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getSuccessRespEvents, getErrorRespEvents } = require("../../util");
const { simpleProcessRouterDest } = require("../../util");
const { ConfigurationError } = require("../../util/errorTypes");

const { getTopic, createAttributesMetadata } = require("./util");
Expand All @@ -19,34 +19,13 @@ function process(event) {
throw new ConfigurationError("No topic set for this event");
}

const processRouterDest = async inputs => {
if (!Array.isArray(inputs) || inputs.length <= 0) {
const respEvents = getErrorRespEvents(null, 400, "Invalid event array");
return [respEvents];
}

const respList = await Promise.all(
inputs.map(async input => {
try {
return getSuccessRespEvents(
await process(input),
[input.metadata],
input.destination
);
} catch (error) {
return getErrorRespEvents(
[input.metadata],
error.response
? error.response.status
: error.code
? error.code
: 400,
error.message || "Error occurred while processing payload."
);
}
})
const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(
inputs,
"GOOGLEPUBSNUB",
process,
reqMetadata
);
return respList;
};

module.exports = { process, processRouterDest };
4 changes: 2 additions & 2 deletions src/v0/destinations/googlesheets/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const process = event => {
};

// Router transform with batching by default
const processRouterDest = async inputs => {
const processRouterDest = async (inputs, reqMetadata) => {
const successRespList = [];
const errorRespList = [];
const errorRespEvents = checkInvalidRtTfEvents(inputs, "GOOGLESHEETS");
Expand All @@ -137,7 +137,7 @@ const processRouterDest = async inputs => {
const errRespEvent = handleRtTfSingleEventError(
input,
error,
"GOOGLESHEETS"
reqMetadata
);
errorRespList.push(errRespEvent);
}
Expand Down
9 changes: 7 additions & 2 deletions src/v0/destinations/heap/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ const processEvent = (message, destination) => {
const process = async event => {
return processEvent(event.message, event.destination);
};
const processRouterDest = async inputs => {
const respList = await simpleProcessRouterDest(inputs, "HEAP", process);
const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(
inputs,
"HEAP",
process,
reqMetadata
);
return respList;
};

Expand Down
4 changes: 2 additions & 2 deletions src/v0/destinations/hs/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const process = async event => {
};

// we are batching by default at routerTransform
const processRouterDest = async inputs => {
const processRouterDest = async (inputs, reqMetadata) => {
const errorRespEvents = checkInvalidRtTfEvents(inputs, DESTINATION);
if (errorRespEvents.length > 0) {
return errorRespEvents;
Expand Down Expand Up @@ -164,7 +164,7 @@ const processRouterDest = async inputs => {
const errRespEvent = handleRtTfSingleEventError(
input,
error,
DESTINATION
reqMetadata
);
errorRespList.push(errRespEvent);
}
Expand Down
47 changes: 7 additions & 40 deletions src/v0/destinations/indicative/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const {
getFieldValueFromMessage,
constructPayload,
defaultPostRequestConfig,
getSuccessRespEvents,
getErrorRespEvents,
generateErrorObject
simpleProcessRouterDest
} = require("../../util");
const {
InstrumentationError,
Expand Down Expand Up @@ -199,43 +197,12 @@ const process = event => {
return processEvent(event.message, event.destination);
};

const processRouterDest = async inputs => {
if (!Array.isArray(inputs) || inputs.length <= 0) {
const respEvents = getErrorRespEvents(null, 400, "Invalid event array");
return [respEvents];
}

const respList = await Promise.all(
inputs.map(async input => {
try {
if (input.message.statusCode) {
// already transformed event
return getSuccessRespEvents(
input.message,
[input.metadata],
input.destination
);
}
// if not transformed
return getSuccessRespEvents(
await process(input),
[input.metadata],
input.destination
);
} catch (error) {
const errObj = generateErrorObject(error);
return getErrorRespEvents(
[input.metadata],
error.response
? error.response.status
: error.code
? error.code
: 400,
error.message || "Error occurred while processing payload.",
errObj.statTags
);
}
})
const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(
inputs,
"INDICATIVE",
process,
reqMetadata
);
return respList;
};
Expand Down
52 changes: 8 additions & 44 deletions src/v0/destinations/intercom/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ const {
defaultRequestConfig,
defaultPostRequestConfig,
getFieldValueFromMessage,
getSuccessRespEvents,
getErrorRespEvents,
addExternalIdToTraits,
generateErrorObject
simpleProcessRouterDest
} = require("../../util");
const {
TransformationError,
InstrumentationError
} = require("../../util/errorTypes");
const { InstrumentationError } = require("../../util/errorTypes");

function getCompanyAttribute(company) {
const companiesList = [];
Expand Down Expand Up @@ -175,43 +170,12 @@ function process(event) {
return response;
}

const processRouterDest = async inputs => {
if (!Array.isArray(inputs) || inputs.length <= 0) {
const respEvents = getErrorRespEvents(null, 400, "Invalid event array");
return [respEvents];
}

const respList = await Promise.all(
inputs.map(async input => {
try {
if (input.message.statusCode) {
// already transformed event
return getSuccessRespEvents(
input.message,
[input.metadata],
input.destination
);
}
// if not transformed
return getSuccessRespEvents(
await process(input),
[input.metadata],
input.destination
);
} catch (error) {
const erroObj = generateErrorObject(error);
return getErrorRespEvents(
[input.metadata],
error.response
? error.response.status
: error.code
? error.code
: 400,
error.message || "Error occurred while processing payload.",
erroObj.statTags
);
}
})
const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(
inputs,
"INTERCOM",
process,
reqMetadata
);
return respList;
};
Expand Down
4 changes: 2 additions & 2 deletions src/v0/destinations/iterable/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ function getEventChunks(
}
}

const processRouterDest = async inputs => {
const processRouterDest = async (inputs, reqMetadata) => {
const errorRespEvents = checkInvalidRtTfEvents(inputs, "ITERABLE");
if (errorRespEvents.length > 0) {
return errorRespEvents;
Expand Down Expand Up @@ -528,7 +528,7 @@ const processRouterDest = async inputs => {
const errRespEvent = handleRtTfSingleEventError(
event,
error,
"ITERABLE"
reqMetadata
);
errorRespList.push(errRespEvent);
}
Expand Down
9 changes: 7 additions & 2 deletions src/v0/destinations/june/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ const process = event => {
return processEvent(event.message, event.destination);
};

const processRouterDest = async inputs => {
const respList = await simpleProcessRouterDest(inputs, "JUNE", process);
const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(
inputs,
"JUNE",
process,
reqMetadata
);
return respList;
};

Expand Down
40 changes: 7 additions & 33 deletions src/v0/destinations/keen/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const {
defaultRequestConfig,
getParsedIP,
getFieldValueFromMessage,
getSuccessRespEvents,
getErrorRespEvents
simpleProcessRouterDest
} = require("../../util");
const { InstrumentationError } = require("../../util/errorTypes");
const { ENDPOINT } = require("./config");
Expand Down Expand Up @@ -148,37 +147,12 @@ function process(event) {
return response;
}

const processRouterDest = async inputs => {
if (!Array.isArray(inputs) || inputs.length <= 0) {
const respEvents = getErrorRespEvents(null, 400, "Invalid event array");
return [respEvents];
}

const respList = await Promise.all(
inputs.map(async input => {
try {
if (input.message.statusCode) {
// already transformed event
return getSuccessRespEvents(
input.message,
[input.metadata],
input.destination
);
}
// if not transformed
return getSuccessRespEvents(
await process(input),
[input.metadata],
input.destination
);
} catch (error) {
return getErrorRespEvents(
[input.metadata],
error.response?.status || error.code || 400,
error.message || "Error occurred while processing payload."
);
}
})
const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(
inputs,
"KEEN",
process,
reqMetadata
);
return respList;
};
Expand Down
4 changes: 4 additions & 0 deletions test/__tests__/data/clickup_router_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@
"jobId": 4
}
],
"statTags": {
"errorCategory": "dataValidation",
"errorType": "instrumentation"
},
"statusCode": 400
}
]