-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chandra shekar Varkala
committed
Jan 16, 2024
1 parent
d2493b0
commit 9a918d7
Showing
2 changed files
with
79 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint-disable no-restricted-syntax */ | ||
const groupBy = require('lodash/groupBy'); | ||
const cloneDeep = require('lodash/cloneDeep'); | ||
const { | ||
removeUndefinedAndNullValues, | ||
getSuccessRespEvents, | ||
getErrorRespEvents, | ||
} = require('../../util'); | ||
|
||
|
||
const batch = (destEvents) => { | ||
const respList = []; | ||
if (!Array.isArray(destEvents) || destEvents.length <= 0) { | ||
const respEvents = getErrorRespEvents(null, 400, 'Invalid event array'); | ||
return [respEvents]; | ||
} | ||
|
||
// Grouping the events by topic | ||
const groupedEvents = groupBy(destEvents, (event) => event.message.topic); | ||
|
||
// Creating a batched request for each topic | ||
// we are grouping the events based on topics | ||
// and creating a batched request for each topic | ||
// example: input events = [{event1,topic1},{event2,topic1},{event3,topic2}] | ||
// out from transformer: {batchedRequest:[{event1},{event2}]}, {batchedRequest:[{event3}]} (2 multilexed responses) | ||
for (const events of Object.values(groupedEvents)) { | ||
const response = { | ||
batchedRequest: events.map((event) => event.message), | ||
metadata: events.map((event) => event.metadata), | ||
destination: events[0].destination, | ||
}; | ||
respList.push( | ||
getSuccessRespEvents(response.batchedRequest, response.metadata, response.destination, true), | ||
); | ||
} | ||
return respList; | ||
}; | ||
|
||
const process = (event) => { | ||
console.log("called __rudder_test__ transform.js"); | ||
|
||
const { message, destination } = event; | ||
|
||
const userId = message.userId || message.anonymousId; | ||
const outputEvent = { | ||
message, | ||
userId, | ||
destination | ||
}; | ||
return removeUndefinedAndNullValues(outputEvent); | ||
}; | ||
|
||
/** | ||
* This functions takes event matadata and updates it based on the transformed and raw paylaod | ||
* the outputEvent is the transformed event which is guranateed to contain the topic | ||
* @param {*} input | ||
* @returns {*} metadata | ||
*/ | ||
const processMetadata = (input) => { | ||
const { metadata, outputEvent } = input; | ||
const clonedMetadata = cloneDeep(metadata); | ||
const { topic } = outputEvent; | ||
if (topic) { | ||
clonedMetadata.rudderId = `${clonedMetadata.rudderId}<<>>${topic}`; | ||
} | ||
return clonedMetadata; | ||
}; | ||
|
||
module.exports = { process, batch, processMetadata }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters