Skip to content

Commit

Permalink
feat: onboard new destination ninetailed
Browse files Browse the repository at this point in the history
  • Loading branch information
anantjain45823 committed Feb 19, 2024
1 parent a98cabd commit bf506ea
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/cdk/v2/destinations/ninetailed/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { getMappingConfig } = require('../../../../v0/util');

const ConfigCategories = {
GENERAL: {
type: 'general',
name: 'generalPayloadMapping',
},
CONTEXT: {
type: 'context',
name: 'contextMapping',
},
};
const MAX_BATCH_SIZE = 200; // Maximum number of events to send in a single batch
const mappingConfig = getMappingConfig(ConfigCategories, __dirname);
const batchEndpoint =
'https://experience.ninetailed.co/v2/organizations/{{organisationId}}/environments/{{environment}}/events';
module.exports = { ConfigCategories, mappingConfig, batchEndpoint, MAX_BATCH_SIZE };
43 changes: 43 additions & 0 deletions src/cdk/v2/destinations/ninetailed/data/contextMapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
{
"sourceKeys": "context.app.name",
"required": true,
"destKey": "context.app.name"
},
{
"sourceKeys": "context.app.version",
"required": true,
"destKey": "context.app.version"
},
{
"sourceKeys": "context.campaign",
"destKey": "context.campaign"
},
{
"sourceKeys": "context.library.name",
"required": true,
"destKey": "context.library.name"
},
{
"sourceKeys": "context.library.version",
"required": true,
"destKey": "context.library.version"
},
{
"sourceKeys": "context.locale",
"destKey": "context.locale"
},
{
"sourceKeys": "context.page",
"destKey": "context.page"
},
{
"sourceKeys": "context.userAgent",
"destKey": "context.userAgent"
},
{
"sourceKeys": "context.location",
"required": true,
"destKey": "context.location"
}
]
35 changes: 35 additions & 0 deletions src/cdk/v2/destinations/ninetailed/data/generalPayloadMapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"sourceKeys": "anonymousId",
"required": true,
"destKey": "anonymousId"
},
{
"sourceKeys": "messageId",
"required": true,
"destKey": "messageId"
},
{
"sourceKeys": "channel",
"required": true,
"destKey": "channel"
},
{
"sourceKeys": "properties",
"destKey": "properties"
},
{
"sourceKeys": "traits",
"sourceFromGenericMap": true,
"destKey": "traits"
},
{
"sourceKeys": "userIdOnly",
"sourceFromGenericMap": true,
"destKey": "userId"
},
{
"sourceKeys": "type",
"destKey": "type"
}
]
32 changes: 32 additions & 0 deletions src/cdk/v2/destinations/ninetailed/procWorkflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
bindings:
- name: EventType
path: ../../../../constants
- path: ../../bindings/jsontemplate
- name: defaultRequestConfig
path: ../../../../v0/util
- name: removeUndefinedAndNullValues
path: ../../../../v0/util
- path: ./utils

steps:
- name: messageType
template: |
.message.type.toLowerCase();
- name: validateInput
template: |
let messageType = $.outputs.messageType;
$.assert(messageType, "message Type is not present. Aborting");
$.assert(messageType in {{$.EventType.([.TRACK,.IDENITFY,.PAGE])}}, "message type " + messageType + " is not supported");
$.assertConfig(.destination.Config.organisationId, "Organisation ID is not present. Aborting");
$.assertConfig(.destination.Config.environment, "Environment is not present. Aborting");
- name: preparePayload
template: |
const payload = $.constructFullPayload(.message);
$.context.payload = $.removeUndefinedAndNullValues(payload);
- name: buildResponse
template: |
const response = $.defaultRequestConfig();
response.body.JSON = $.context.payload;
response.method = "POST";
response
58 changes: 58 additions & 0 deletions src/cdk/v2/destinations/ninetailed/rtWorkflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
bindings:
- path: ./config
- name: handleRtTfSingleEventError
path: ../../../../v0/util/index

steps:
- name: validateInput
template: |
$.assert(Array.isArray(^) && ^.length > 0, "Invalid event array")
- name: transform
externalWorkflow:
path: ./procWorkflow.yaml
loopOverInput: true

- name: successfulEvents
template: |
$.outputs.transform#idx.output.({
"output": .,
"destination": ^[idx].destination,
"metadata": ^[idx].metadata
})[]
- name: failedEvents
template: |
$.outputs.transform#idx.error.(
$.handleRtTfSingleEventError(^[idx], .originalError ?? ., {})
)[]
- name: batchSuccessfulEvents
description: Batches the successfulEvents
template: |
let batches = $.chunk($.outputs.successfulEvents, $.MAX_BATCH_SIZE);
batches@batch.({
"batchedRequest": {
"body": {
"JSON": {"events": ~r batch.output[]},
"JSON_ARRAY": {},
"XML": {},
"FORM": {}
},
"version": "1",
"type": "REST",
"method": "POST",
"endpoint": {{$.ENDPOINT}},
"headers": batch[0].destination.Config.().({
"X-Algolia-Application-Id": .applicationId,
"X-Algolia-API-Key": .apiKey
}),
"params": {},
"files": {}
},
"metadata": ~r batch.metadata[],
"batched": true,
"statusCode": 200,
"destination": batch[0].destination
})[];
- name: finalPayload
template: |
[...$.outputs.failedEvents, ...$.outputs.batchSuccessfulEvents]
17 changes: 17 additions & 0 deletions src/cdk/v2/destinations/ninetailed/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { mappingConfig, ConfigCategories } = require('./config');
const { constructPayload } = require('../../../../v0/util');

/**
* This fucntion constructs payloads based upon mappingConfig for all calls
* We build context as it has some specific payloads with default values so just breaking them down
* @param {*} message
* @returns
*/
const constructFullPayload = (message) => {
const context = constructPayload(message, mappingConfig[ConfigCategories.CONTEXT.name]);
const payload = constructPayload(message, mappingConfig[ConfigCategories.GENERAL.name]);
payload.context = context;
return payload;
};

module.exports = { constructFullPayload };

0 comments on commit bf506ea

Please sign in to comment.