Skip to content

Commit

Permalink
refactor: reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauravudia committed Mar 22, 2024
1 parent 3a8fcf2 commit d68ad1b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
34 changes: 12 additions & 22 deletions src/v0/destinations/mp/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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);
};

Expand Down
28 changes: 28 additions & 0 deletions src/v0/destinations/mp/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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,
Expand All @@ -351,4 +378,5 @@ module.exports = {
batchEvents,
trimTraits,
generatePageOrScreenCustomEventName,
recordBatchSizeMetrics,
};

0 comments on commit d68ad1b

Please sign in to comment.