From 31bd05aebf8e3c2049284d26a622179e33bb4f39 Mon Sep 17 00:00:00 2001 From: Krishna Chaitanya Date: Wed, 13 Sep 2023 20:10:24 +0530 Subject: [PATCH 1/2] chore: fix sonar and lint issues (#2607) --- src/middleware.js | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/middleware.js b/src/middleware.js index 4387dbd05c..b8f6a2ac1b 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -1,31 +1,18 @@ -const stats = require('./util/stats'); const Pyroscope = require('@pyroscope/nodejs'); +const stats = require('./util/stats'); +const logger = require('./logger'); Pyroscope.init({ appName: 'rudder-transformer', }); -function pyroscopeMiddleware(ctx, next) { - Pyroscope.startHeapCollecting(); - return (ctx, next) => { - if (ctx.method === 'GET' && ctx.path === '/debug/pprof/profile') { - return handlerCpu(ctx).then(() => next()); - } - if (ctx.method === 'GET' && ctx.path === '/debug/pprof/heap') { - return handlerHeap(ctx).then(() => next()); - } - next(); - }; -} - async function handlerCpu(ctx) { try { const p = await Pyroscope.collectCpu(Number(ctx.query.seconds)); ctx.body = p; ctx.status = 200; - } - catch (e) { - console.log(e); + } catch (e) { + logger.error(e); ctx.status = 500; } } @@ -35,15 +22,27 @@ async function handlerHeap(ctx) { const p = await Pyroscope.collectHeap(); ctx.body = p; ctx.status = 200; - } - catch (e) { - console.log(e); + } catch (e) { + logger.error(e); ctx.status = 500; } } +function pyroscopeMiddleware() { + Pyroscope.startHeapCollecting(); + return (ctx, next) => { + if (ctx.method === 'GET' && ctx.path === '/debug/pprof/profile') { + return handlerCpu(ctx).then(() => next()); + } + if (ctx.method === 'GET' && ctx.path === '/debug/pprof/heap') { + return handlerHeap(ctx).then(() => next()); + } + return next(); + }; +} + function addPyroscopeMiddleware(app) { - app.use(pyroscopeMiddleware()) + app.use(pyroscopeMiddleware()); } function durationMiddleware() { From e98ea84265ee37bfc6d3e99104af7bb4a3d78007 Mon Sep 17 00:00:00 2001 From: Gauravudia <60897972+Gauravudia@users.noreply.github.com> Date: Thu, 14 Sep 2023 09:56:38 +0530 Subject: [PATCH 2/2] feat(mixpanel): add incremental properties support (#2550) * feat(mixpanel): add incremental properties support * docs: add comment * test: update testcase --- src/v0/destinations/mp/transform.js | 39 +++++++++++++++++++++++++++++ src/v0/destinations/mp/util.test.js | 5 +--- test/__tests__/data/mp_input.json | 25 +++++++++++++++--- test/__tests__/data/mp_output.json | 20 ++++++++++++++- 4 files changed, 81 insertions(+), 8 deletions(-) diff --git a/src/v0/destinations/mp/transform.js b/src/v0/destinations/mp/transform.js index 0cb06aad93..425b7a8249 100644 --- a/src/v0/destinations/mp/transform.js +++ b/src/v0/destinations/mp/transform.js @@ -130,6 +130,37 @@ const processRevenueEvents = (message, destination, revenueValue) => { return responseBuilderSimple(payload, message, 'revenue', destination.Config); }; +/** + * This function is used to process the incremental properties + * ref :- https://developer.mixpanel.com/reference/profile-numerical-add + * @param {*} message + * @param {*} destination + * @param {*} propIncrements + * @returns + */ +const processIncrementalProperties = (message, destination, propIncrements) => { + const payload = { + $add: {}, + $token: destination.Config.token, + $distinct_id: message.userId || message.anonymousId, + }; + + if (destination?.Config.identityMergeApi === 'simplified') { + payload.$distinct_id = message.userId || `$device:${message.anonymousId}`; + } + + Object.keys(message.properties).forEach((prop) => { + const value = message.properties[prop]; + if (value && propIncrements.includes(prop)) { + payload.$add[prop] = value; + } + }); + + return Object.keys(payload.$add).length > 0 + ? responseBuilderSimple(payload, message, 'incremental_properties', destination.Config) + : null; +}; + const getEventValueForTrackEvent = (message, destination) => { const mappedProperties = constructPayload(message, mPEventPropertiesConfigJson); // This is to conform with SDKs sending timestamp component with messageId @@ -178,6 +209,14 @@ const processTrack = (message, destination) => { if (revenue) { returnValue.push(processRevenueEvents(message, destination, revenue)); } + + if (Array.isArray(destination.Config.propIncrements)) { + const propIncrements = destination.Config.propIncrements.map((item) => item.property); + const response = processIncrementalProperties(message, destination, propIncrements); + if (response) { + returnValue.push(response); + } + } returnValue.push(getEventValueForTrackEvent(message, destination)); return returnValue; }; diff --git a/src/v0/destinations/mp/util.test.js b/src/v0/destinations/mp/util.test.js index 2434564699..c5cd5b61e8 100644 --- a/src/v0/destinations/mp/util.test.js +++ b/src/v0/destinations/mp/util.test.js @@ -1,7 +1,4 @@ -const { - combineBatchRequestsWithSameJobIds, - combineBatchRequestsWithSameJobIds2, -} = require('./util'); +const { combineBatchRequestsWithSameJobIds } = require('./util'); const destinationMock = { Config: { diff --git a/test/__tests__/data/mp_input.json b/test/__tests__/data/mp_input.json index 394fc49b61..6d456858c9 100644 --- a/test/__tests__/data/mp_input.json +++ b/test/__tests__/data/mp_input.json @@ -432,7 +432,18 @@ "apiKey": "dummyApiKey", "token": "dummyApiKey", "prefixProperties": true, - "useNativeSDK": false + "useNativeSDK": false, + "propIncrements": [ + { + "property": "counter" + }, + { + "property": "item_purchased" + }, + { + "property": "number_of_logins" + } + ] }, "DestinationDefinition": { "DisplayName": "Kiss Metrics", @@ -493,7 +504,10 @@ "originalTimestamp": "2020-01-24T06:29:02.364Z", "properties": { "currency": "USD", - "revenue": 45.89 + "revenue": 45.89, + "counter": 1, + "item_purchased": "2", + "number_of_logins": "" }, "receivedAt": "2020-01-24T11:59:02.403+05:30", "request_ip": "[::1]:53710", @@ -582,7 +596,12 @@ "apiKey": "dummyApiKey", "token": "dummyApiKey", "prefixProperties": true, - "useNativeSDK": false + "useNativeSDK": false, + "propIncrements": [ + { + "property": "" + } + ] }, "DestinationDefinition": { "DisplayName": "Kiss Metrics", diff --git a/test/__tests__/data/mp_output.json b/test/__tests__/data/mp_output.json index fe54474dd8..61100a1b84 100644 --- a/test/__tests__/data/mp_output.json +++ b/test/__tests__/data/mp_output.json @@ -128,6 +128,24 @@ "files": {}, "userId": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca" }, + { + "version": "1", + "type": "REST", + "method": "POST", + "endpoint": "https://api.mixpanel.com/engage/", + "headers": {}, + "params": {}, + "body": { + "JSON": {}, + "JSON_ARRAY": { + "batch": "[{\"$add\":{\"counter\":1,\"item_purchased\":\"2\"},\"$token\":\"dummyApiKey\",\"$distinct_id\":\"e6ab2c5e-2cda-44a9-a962-e2f67df78bca\"}]" + }, + "XML": {}, + "FORM": {} + }, + "files": {}, + "userId": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca" + }, { "version": "1", "type": "REST", @@ -138,7 +156,7 @@ "body": { "JSON": {}, "JSON_ARRAY": { - "batch": "[{\"event\":\"test revenue MIXPANEL\",\"properties\":{\"currency\":\"USD\",\"revenue\":45.89,\"city\":\"Disney\",\"country\":\"USA\",\"email\":\"mickey@disney.com\",\"firstName\":\"Mickey\",\"ip\":\"0.0.0.0\",\"$current_url\":\"https://docs.rudderstack.com/destinations/mixpanel\",\"$screen_dpi\":2,\"mp_lib\":\"RudderLabs JavaScript SDK\",\"$app_build_number\":\"1.0.0\",\"$app_version_string\":\"1.0.5\",\"$insert_id\":\"a6a0ad5a-bd26-4f19-8f75-38484e580fc7\",\"token\":\"dummyApiKey\",\"distinct_id\":\"e6ab2c5e-2cda-44a9-a962-e2f67df78bca\",\"time\":1579847342,\"$browser\":\"Chrome\",\"$browser_version\":\"79.0.3945.117\"}}]" + "batch": "[{\"event\":\"test revenue MIXPANEL\",\"properties\":{\"currency\":\"USD\",\"revenue\":45.89,\"counter\":1,\"item_purchased\":\"2\",\"number_of_logins\":\"\",\"city\":\"Disney\",\"country\":\"USA\",\"email\":\"mickey@disney.com\",\"firstName\":\"Mickey\",\"ip\":\"0.0.0.0\",\"$current_url\":\"https://docs.rudderstack.com/destinations/mixpanel\",\"$screen_dpi\":2,\"mp_lib\":\"RudderLabs JavaScript SDK\",\"$app_build_number\":\"1.0.0\",\"$app_version_string\":\"1.0.5\",\"$insert_id\":\"a6a0ad5a-bd26-4f19-8f75-38484e580fc7\",\"token\":\"dummyApiKey\",\"distinct_id\":\"e6ab2c5e-2cda-44a9-a962-e2f67df78bca\",\"time\":1579847342,\"$browser\":\"Chrome\",\"$browser_version\":\"79.0.3945.117\"}}]" }, "XML": {}, "FORM": {}