Skip to content

Commit

Permalink
fix: format change
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Sep 12, 2023
1 parent 9b6138b commit ba8ebb4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/v0/destinations/bqstream/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const processRouterDest = (inputs) => {
let eachUserBatchedResponse = [];
orderedEventsList.forEach((eventList) => {
// no error event list will have more than one items in the list
if(eventList[0].error) {
if (eventList[0].error) {
finalResp.push([...eventList]);
} else {
// batch the successful events
Expand Down
82 changes: 41 additions & 41 deletions src/v0/destinations/bqstream/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,69 +172,69 @@ const filterAndSplitEvents = (sortedEvents) => {
// if error is present, then push the previous successfulEventsChunk
// and then push the error event
if (isDefinedAndNotNull(item.error)) {
if(successfulEventsChunk.length > 0) {
if (successfulEventsChunk.length > 0) {
resultArray.push(successfulEventsChunk);
successfulEventsChunk = [];
}
resultArray.push([item]);
} else {
// if error is not present, then push the event to successfulEventsChunk
successfulEventsChunk.push(item);
}
}
}
// Push the last successfulEventsChunk to resultArray
// Push the last successfulEventsChunk to resultArray
if (successfulEventsChunk.length > 0) {
resultArray.push(successfulEventsChunk);
}
return resultArray;
};


const convertMetadataToArray = (eventList ) => {
const convertMetadataToArray = (eventList) => {
const processedEvents = eventList.map((event) => ({
...event,
metadata: Array.isArray(event.metadata) ? event.metadata : [event.metadata],
}));
return processedEvents;
}

/**
* Takes in two arrays, eachUserSuccessEventslist and eachUserErrorEventsList, and returns an ordered array of events.
* If there are no error events, it returns the array of transformed events.
* If there are no successful responses, it returns the error events.
* If there are both successful and erroneous events, it orders them based on the jobId property of the events' metadata array.
* considering error responses are built with @handleRtTfSingleEventError
*
* @param {Array} eachUserSuccessEventslist - An array of events representing successful responses for a user.
* @param {Array} eachUserErrorEventsList - An array of events representing error responses for a user.
* @returns {Array} - An ordered array of events.
*
* @example
* const eachUserSuccessEventslist = [{track, jobId: 1}, {track, jobId: 2}, {track, jobId: 5}];
* const eachUserErrorEventsList = [{identify, jobId: 3}, {identify, jobId: 4}];
* Output: [[{track, jobId: 1}, {track, jobId: 2}],[{identify, jobId: 3}],[{identify, jobId: 4}], {track, jobId: 5}]]
*/
const HandleEventOrdering = (eachUserSuccessEventslist, eachUserErrorEventsList) => {
// Convert 'metadata' to an array if it's not already
const processedSuccessfulEvents = convertMetadataToArray(eachUserSuccessEventslist);
const processedErrorEvents = convertMetadataToArray(eachUserErrorEventsList);

// if there are no error events, then return the batched response
if (eachUserErrorEventsList.length === 0) {
return [processedSuccessfulEvents];
}
// if there are no batched response, then return the error events
if (eachUserSuccessEventslist.length === 0) {
return [processedErrorEvents];
}
/**
* Takes in two arrays, eachUserSuccessEventslist and eachUserErrorEventsList, and returns an ordered array of events.
* If there are no error events, it returns the array of transformed events.
* If there are no successful responses, it returns the error events.
* If there are both successful and erroneous events, it orders them based on the jobId property of the events' metadata array.
* considering error responses are built with @handleRtTfSingleEventError
*
* @param {Array} eachUserSuccessEventslist - An array of events representing successful responses for a user.
* @param {Array} eachUserErrorEventsList - An array of events representing error responses for a user.
* @returns {Array} - An ordered array of events.
*
* @example
* const eachUserSuccessEventslist = [{track, jobId: 1}, {track, jobId: 2}, {track, jobId: 5}];
* const eachUserErrorEventsList = [{identify, jobId: 3}, {identify, jobId: 4}];
* Output: [[{track, jobId: 1}, {track, jobId: 2}],[{identify, jobId: 3}],[{identify, jobId: 4}], {track, jobId: 5}]]
*/
const HandleEventOrdering = (eachUserSuccessEventslist, eachUserErrorEventsList) => {
// Convert 'metadata' to an array if it's not already
const processedSuccessfulEvents = convertMetadataToArray(eachUserSuccessEventslist);
const processedErrorEvents = convertMetadataToArray(eachUserErrorEventsList);

// if there are no error events, then return the batched response
if (eachUserErrorEventsList.length === 0) {
return [processedSuccessfulEvents];
}
// if there are no batched response, then return the error events
if (eachUserSuccessEventslist.length === 0) {
return [processedErrorEvents];
}

// if there are both batched response and error events, then order them
const combinedTransformedEventList = [...processedSuccessfulEvents, ...processedErrorEvents].flat();

const sortedEvents = _.sortBy(combinedTransformedEventList, (event) => event.metadata[0].jobId);
const finalResp = filterAndSplitEvents(sortedEvents);
// if there are both batched response and error events, then order them
const combinedTransformedEventList = [...processedSuccessfulEvents, ...processedErrorEvents].flat();

return finalResp;
}
const sortedEvents = _.sortBy(combinedTransformedEventList, (event) => event.metadata[0].jobId);
const finalResp = filterAndSplitEvents(sortedEvents);

return finalResp;
}

module.exports = { networkHandler, generateUserJourneys, HandleEventOrdering };
module.exports = { networkHandler, generateUserJourneys, HandleEventOrdering };
68 changes: 33 additions & 35 deletions src/v0/destinations/bqstream/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
const {HandleEventOrdering} = require('./util')
const { HandleEventOrdering } = require('./util')

describe('HandleEventOrdering', () => {

// Tests that the function returns an array of transformed events when there are no error events
it('should return an array of transformed events when there are no error events', () => {
const eachUserSuccessEventslist = [{message: { type: "track"}, metadata: {jobId: 1}}, {message: { type: "track"}, metadata: {jobId: 3}}, {message: { type: "track"}, metadata: {jobId: 5}}];
const eachUserErrorEventsList = [];
const expected = [[{"message":{"type":"track"},"metadata":[{"jobId":1}]},{"message":{"type":"track"},"metadata":[{"jobId":3}]},{"message":{"type":"track"},"metadata":[{"jobId":5}]}]];
const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList);
console.log(JSON.stringify(result))
expect(result).toEqual(expected);
});
// Tests that the function returns an array of transformed events when there are no error events
it('should return an array of transformed events when there are no error events', () => {
const eachUserSuccessEventslist = [{ message: { type: "track" }, metadata: { jobId: 1 } }, { message: { type: "track" }, metadata: { jobId: 3 } }, { message: { type: "track" }, metadata: { jobId: 5 } }];
const eachUserErrorEventsList = [];
const expected = [[{ "message": { "type": "track" }, "metadata": [{ "jobId": 1 }] }, { "message": { "type": "track" }, "metadata": [{ "jobId": 3 }] }, { "message": { "type": "track" }, "metadata": [{ "jobId": 5 }] }]];
const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList);
expect(result).toEqual(expected);
});

// Tests that the function returns an empty array when both input arrays are empty
it('should return an empty array when both input arrays are empty', () => {
const eachUserSuccessEventslist = [];
const eachUserErrorEventsList = [];
const expected = [[]];
const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList);
expect(result).toEqual(expected);
});
// Tests that the function returns an empty array when both input arrays are empty
it('should return an empty array when both input arrays are empty', () => {
const eachUserSuccessEventslist = [];
const eachUserErrorEventsList = [];
const expected = [[]];
const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList);
expect(result).toEqual(expected);
});

// Tests that the function returns an array with only error events when all events are erroneous
it('should return an array with only error events when all events are erroneous', () => {
const eachUserSuccessEventslist = [];
const eachUserErrorEventsList = [{batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 3,userId: 'user12345'}]}, {batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 4,userId: 'user12345'}]}];
const expected = [[{batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 3,userId: 'user12345'}]}, {batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 4,userId: 'user12345'}]}]];
const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList);
expect(result).toEqual(expected);
});
// Tests that the function returns an array with only error events when all events are erroneous
it('should return an array with only error events when all events are erroneous', () => {
const eachUserSuccessEventslist = [];
const eachUserErrorEventsList = [{ batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 3, userId: 'user12345' }] }, { batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 4, userId: 'user12345' }] }];
const expected = [[{ batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 3, userId: 'user12345' }] }, { batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 4, userId: 'user12345' }] }]];
const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList);
expect(result).toEqual(expected);
});

// Tests that the function returns an ordered array of events with both successful and erroneous events, ordered based on the jobId property of the events' metadata array
it('should return an ordered array of events with both successful and erroneous events', () => {
const eachUserSuccessEventslist = [{batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 3,userId: 'user12345'}]}, {batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 4,userId: 'user12345'}]}];
const eachUserErrorEventsList = [{message: { type: "track"}, metadata: {jobId: 1}}, {message: { type: "track"}, metadata: {jobId: 2}}, {message: { type: "track"}, metadata: {jobId: 5}}];
const expected = [[{"message":{"type":"track"},"metadata":[{"jobId":1}]},{"message":{"type":"track"},"metadata":[{"jobId":2}]}],[{"batched":false,"destination":{},"error":"Message Type not supported: identify","metadata":[{"jobId":3,"userId":"user12345"}]}],[{"batched":false,"destination":{},"error":"Message Type not supported: identify","metadata":[{"jobId":4,"userId":"user12345"}]}],[{"message":{"type":"track"},"metadata":[{"jobId":5}]}]];
const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList);
console.log(JSON.stringify(result))
expect(result).toEqual(expected);
});
// Tests that the function returns an ordered array of events with both successful and erroneous events, ordered based on the jobId property of the events' metadata array
it('should return an ordered array of events with both successful and erroneous events', () => {
const eachUserSuccessEventslist = [{ batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 3, userId: 'user12345' }] }, { batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 4, userId: 'user12345' }] }];
const eachUserErrorEventsList = [{ message: { type: "track" }, metadata: { jobId: 1 } }, { message: { type: "track" }, metadata: { jobId: 2 } }, { message: { type: "track" }, metadata: { jobId: 5 } }];
const expected = [[{ "message": { "type": "track" }, "metadata": [{ "jobId": 1 }] }, { "message": { "type": "track" }, "metadata": [{ "jobId": 2 }] }], [{ "batched": false, "destination": {}, "error": "Message Type not supported: identify", "metadata": [{ "jobId": 3, "userId": "user12345" }] }], [{ "batched": false, "destination": {}, "error": "Message Type not supported: identify", "metadata": [{ "jobId": 4, "userId": "user12345" }] }], [{ "message": { "type": "track" }, "metadata": [{ "jobId": 5 }] }]];
const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList);
expect(result).toEqual(expected);
});
});

0 comments on commit ba8ebb4

Please sign in to comment.