From d68ad1b64952adf7a9eff200645719755fb7929c Mon Sep 17 00:00:00 2001 From: Gauravudia Date: Fri, 22 Mar 2024 11:02:39 +0530 Subject: [PATCH] refactor: reduce duplicate code --- src/v0/destinations/mp/transform.js | 34 ++++++++++------------------- src/v0/destinations/mp/util.js | 28 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/v0/destinations/mp/transform.js b/src/v0/destinations/mp/transform.js index a574804e18..42920bb625 100644 --- a/src/v0/destinations/mp/transform.js +++ b/src/v0/destinations/mp/transform.js @@ -37,9 +37,9 @@ const { batchEvents, trimTraits, generatePageOrScreenCustomEventName, + recordBatchSizeMetrics, } = require('./util'); const { CommonUtils } = require('../../../util/common'); -const stats = require('../../../util/stats'); // ref: https://help.mixpanel.com/hc/en-us/articles/115004613766-Default-Properties-Collected-by-Mixpanel const mPEventPropertiesConfigJson = mappingConfig[ConfigCategory.EVENT_PROPERTIES.name]; @@ -480,10 +480,12 @@ const process = (event) => processSingleMessage(event.message, event.destination // Ref: https://help.mixpanel.com/hc/en-us/articles/115004613766-Default-Properties-Collected-by-Mixpanel // Ref: https://help.mixpanel.com/hc/en-us/articles/115004561786-Track-UTM-Tags const processRouterDest = async (inputs, reqMetadata) => { - let engageBatchSize = 0; - let groupsBatchSize = 0; - let trackBatchSize = 0; - let importBatchSize = 0; + const batchSize = { + engage: 0, + groups: 0, + track: 0, + import: 0, + }; const groupedEvents = groupEventsByType(inputs); const response = await Promise.all( @@ -516,10 +518,10 @@ const processRouterDest = async (inputs, reqMetadata) => { const { engageEvents, groupsEvents, trackEvents, importEvents, batchErrorRespList } = groupEventsByEndpoint(transformedPayloads); - engageBatchSize += engageEvents.length; - groupsBatchSize += groupsEvents.length; - trackBatchSize += trackEvents.length; - importBatchSize += importEvents.length; + batchSize.engage += engageEvents.length; + batchSize.groups += groupsEvents.length; + batchSize.track += trackEvents.length; + batchSize.import += importEvents.length; const engageRespList = batchEvents(engageEvents, ENGAGE_MAX_BATCH_SIZE, reqMetadata); const groupsRespList = batchEvents(groupsEvents, GROUPS_MAX_BATCH_SIZE, reqMetadata); @@ -540,19 +542,7 @@ const processRouterDest = async (inputs, reqMetadata) => { const allBatchedEvents = lodash.flatMap(response); const { destination } = allBatchedEvents[0]; - stats.gauge('mixpanel_batch_engage_pack_size', engageBatchSize, { - destination_id: destination.ID, - }); - stats.gauge('mixpanel_batch_group_pack_size', groupsBatchSize, { - destination_id: destination.ID, - }); - stats.gauge('mixpanel_batch_track_pack_size', trackBatchSize, { - destination_id: destination.ID, - }); - stats.gauge('mixpanel_batch_import_pack_size', importBatchSize, { - destination_id: destination.ID, - }); - + recordBatchSizeMetrics(batchSize, destination.ID); return combineBatchRequestsWithSameJobIds(allBatchedEvents); }; diff --git a/src/v0/destinations/mp/util.js b/src/v0/destinations/mp/util.js index f56242d88b..d564e805ad 100644 --- a/src/v0/destinations/mp/util.js +++ b/src/v0/destinations/mp/util.js @@ -25,6 +25,7 @@ const { mappingConfig, } = require('./config'); const { CommonUtils } = require('../../../util/common'); +const stats = require('../../../util/stats'); const mPIdentifyConfigJson = mappingConfig[ConfigCategory.IDENTIFY.name]; const mPProfileAndroidConfigJson = mappingConfig[ConfigCategory.PROFILE_ANDROID.name]; @@ -342,6 +343,32 @@ const generatePageOrScreenCustomEventName = (message, userDefinedEventTemplate) return eventName; }; +/** + * Records the batch size metrics for different endpoints. + * + * @param {Object} batchSize - The object containing the batch size for different endpoints. + * @param {number} batchSize.engage - The batch size for engage endpoint. + * @param {number} batchSize.groups - The batch size for group endpoint. + * @param {number} batchSize.track - The batch size for track endpoint. + * @param {number} batchSize.import - The batch size for import endpoint. + * @param {string} destinationId - The ID of the destination. + * @returns {void} + */ +const recordBatchSizeMetrics = (batchSize, destinationId) => { + stats.gauge('mixpanel_batch_engage_pack_size', batchSize.engage, { + destination_id: destinationId, + }); + stats.gauge('mixpanel_batch_group_pack_size', batchSize.groups, { + destination_id: destinationId, + }); + stats.gauge('mixpanel_batch_track_pack_size', batchSize.track, { + destination_id: destinationId, + }); + stats.gauge('mixpanel_batch_import_pack_size', batchSize.import, { + destination_id: destinationId, + }); +}; + module.exports = { createIdentifyResponse, isImportAuthCredentialsAvailable, @@ -351,4 +378,5 @@ module.exports = { batchEvents, trimTraits, generatePageOrScreenCustomEventName, + recordBatchSizeMetrics, };