Skip to content

Commit

Permalink
refactor: deprecate mixpanel /track endpoint (#2833)
Browse files Browse the repository at this point in the history
* refactor: deprecate mixpanel /track endpoint

* fix: used token in place of apiSecret for merge call

* fix: no-case-declarations
  • Loading branch information
Gauravudia committed May 21, 2024
1 parent 0196f20 commit 2250af8
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 234 deletions.
6 changes: 0 additions & 6 deletions src/util/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,12 +587,6 @@ class Prometheus {
type: 'gauge',
labelNames: ['destination_id'],
},
{
name: 'mixpanel_batch_track_pack_size',
help: 'mixpanel_batch_track_pack_size',
type: 'gauge',
labelNames: ['destination_id'],
},
{
name: 'mixpanel_batch_import_pack_size',
help: 'mixpanel_batch_import_pack_size',
Expand Down
2 changes: 0 additions & 2 deletions src/v0/destinations/mp/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const MP_IDENTIFY_EXCLUSION_LIST = [
];

const GEO_SOURCE_ALLOWED_VALUES = [null, 'reverse_geocoding'];
const TRACK_MAX_BATCH_SIZE = 50;
const IMPORT_MAX_BATCH_SIZE = 2000;
const ENGAGE_MAX_BATCH_SIZE = 2000;
const GROUPS_MAX_BATCH_SIZE = 200;
Expand All @@ -68,7 +67,6 @@ module.exports = {
MP_IDENTIFY_EXCLUSION_LIST,
getCreateDeletionTaskEndpoint,
DISTINCT_ID_MAX_BATCH_SIZE,
TRACK_MAX_BATCH_SIZE,
IMPORT_MAX_BATCH_SIZE,
ENGAGE_MAX_BATCH_SIZE,
GROUPS_MAX_BATCH_SIZE,
Expand Down
70 changes: 24 additions & 46 deletions src/v0/destinations/mp/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const {
mappingConfig,
BASE_ENDPOINT,
BASE_ENDPOINT_EU,
TRACK_MAX_BATCH_SIZE,
IMPORT_MAX_BATCH_SIZE,
ENGAGE_MAX_BATCH_SIZE,
GROUPS_MAX_BATCH_SIZE,
Expand All @@ -47,21 +46,19 @@ const mPEventPropertiesConfigJson = mappingConfig[ConfigCategory.EVENT_PROPERTIE
const setImportCredentials = (destConfig) => {
const endpoint =
destConfig.dataResidency === 'eu' ? `${BASE_ENDPOINT_EU}/import/` : `${BASE_ENDPOINT}/import/`;
const headers = { 'Content-Type': 'application/json' };
const params = { strict: destConfig.strictMode ? 1 : 0 };
const { apiSecret, serviceAccountUserName, serviceAccountSecret, projectId } = destConfig;
if (apiSecret) {
headers.Authorization = `Basic ${base64Convertor(`${apiSecret}:`)}`;
const { serviceAccountUserName, serviceAccountSecret, projectId, token } = destConfig;
let credentials;
if (token) {
credentials = `${token}:`;
} else if (serviceAccountUserName && serviceAccountSecret && projectId) {
headers.Authorization = `Basic ${base64Convertor(
`${serviceAccountUserName}:${serviceAccountSecret}`,
)}`;
credentials = `${serviceAccountUserName}:${serviceAccountSecret}`;

Check warning on line 55 in src/v0/destinations/mp/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/mp/transform.js#L55

Added line #L55 was not covered by tests
params.projectId = projectId;
} else {
throw new InstrumentationError(
'Event timestamp is older than 5 days and no API secret or service account credentials (i.e. username, secret and projectId) are provided in destination configuration',
);
}
const headers = {
'Content-Type': 'application/json',
Authorization: `Basic ${base64Convertor(credentials)}`,
};
return { endpoint, headers, params };
};

Expand All @@ -70,37 +67,26 @@ const responseBuilderSimple = (payload, message, eventType, destConfig) => {
response.method = defaultPostRequestConfig.requestMethod;
response.userId = message.userId || message.anonymousId;
response.body.JSON_ARRAY = { batch: JSON.stringify([removeUndefinedValues(payload)]) };
const { apiSecret, serviceAccountUserName, serviceAccountSecret, projectId, dataResidency } =
destConfig;
const { dataResidency } = destConfig;
const duration = getTimeDifference(message.timestamp);
switch (eventType) {
case EventType.ALIAS:
case EventType.TRACK:
case EventType.SCREEN:
case EventType.PAGE:
if (
!apiSecret &&
!(serviceAccountUserName && serviceAccountSecret && projectId) &&
duration.days <= 5
) {
response.endpoint =
dataResidency === 'eu' ? `${BASE_ENDPOINT_EU}/track/` : `${BASE_ENDPOINT}/track/`;
response.headers = {};
} else if (duration.years > 5) {
case EventType.PAGE: {
if (duration.years > 5) {
throw new InstrumentationError('Event timestamp should be within last 5 years');
} else {
const credentials = setImportCredentials(destConfig);
response.endpoint = credentials.endpoint;
response.headers = credentials.headers;
response.params = {
project_id: credentials.params?.projectId,
strict: credentials.params.strict,
};
break;
}
const credentials = setImportCredentials(destConfig);
response.endpoint = credentials.endpoint;
response.headers = credentials.headers;
response.params = {
project_id: credentials.params?.projectId,
strict: credentials.params.strict,
};
break;
case 'merge':
// eslint-disable-next-line no-case-declarations
}
case 'merge': {
const credentials = setImportCredentials(destConfig);
response.endpoint = credentials.endpoint;
response.headers = credentials.headers;
Expand All @@ -109,7 +95,7 @@ const responseBuilderSimple = (payload, message, eventType, destConfig) => {
strict: credentials.params.strict,
};
break;

}
default:
response.endpoint =
dataResidency === 'eu' ? `${BASE_ENDPOINT_EU}/engage/` : `${BASE_ENDPOINT}/engage/`;
Expand Down Expand Up @@ -484,7 +470,6 @@ const processRouterDest = async (inputs, reqMetadata) => {
const batchSize = {
engage: 0,
groups: 0,
track: 0,
import: 0,
};

Expand Down Expand Up @@ -516,23 +501,16 @@ const processRouterDest = async (inputs, reqMetadata) => {
);

transformedPayloads = lodash.flatMap(transformedPayloads);
const { engageEvents, groupsEvents, trackEvents, importEvents, batchErrorRespList } =
const { engageEvents, groupsEvents, importEvents, batchErrorRespList } =
groupEventsByEndpoint(transformedPayloads);

const engageRespList = batchEvents(engageEvents, ENGAGE_MAX_BATCH_SIZE, reqMetadata);
const groupsRespList = batchEvents(groupsEvents, GROUPS_MAX_BATCH_SIZE, reqMetadata);
const trackRespList = batchEvents(trackEvents, TRACK_MAX_BATCH_SIZE, reqMetadata);
const importRespList = batchEvents(importEvents, IMPORT_MAX_BATCH_SIZE, reqMetadata);
const batchSuccessRespList = [
...engageRespList,
...groupsRespList,
...trackRespList,
...importRespList,
];
const batchSuccessRespList = [...engageRespList, ...groupsRespList, ...importRespList];

batchSize.engage += engageRespList.length;
batchSize.groups += groupsRespList.length;
batchSize.track += trackRespList.length;
batchSize.import += importRespList.length;

return [...batchSuccessRespList, ...batchErrorRespList];
Expand Down
8 changes: 1 addition & 7 deletions src/v0/destinations/mp/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const createIdentifyResponse = (message, type, destination, responseBuilderSimpl
* @returns
*/
const isImportAuthCredentialsAvailable = (destination) =>
destination.Config.apiSecret ||
destination.Config.token ||
(destination.Config.serviceAccountSecret &&
destination.Config.serviceAccountUserName &&
destination.Config.projectId);
Expand Down Expand Up @@ -179,7 +179,6 @@ const groupEventsByEndpoint = (events) => {
const eventMap = {
engage: [],
groups: [],
track: [],
import: [],
};
const batchErrorRespList = [];
Expand All @@ -204,7 +203,6 @@ const groupEventsByEndpoint = (events) => {
return {
engageEvents: eventMap.engage,
groupsEvents: eventMap.groups,
trackEvents: eventMap.track,
importEvents: eventMap.import,
batchErrorRespList,
};
Expand Down Expand Up @@ -349,7 +347,6 @@ const generatePageOrScreenCustomEventName = (message, userDefinedEventTemplate)
* @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}
Expand All @@ -361,9 +358,6 @@ const recordBatchSizeMetrics = (batchSize, 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,
});
Expand Down
14 changes: 0 additions & 14 deletions src/v0/destinations/mp/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe('Unit test cases for groupEventsByEndpoint', () => {
expect(result).toEqual({
engageEvents: [],
groupsEvents: [],
trackEvents: [],
importEvents: [],
batchErrorRespList: [],
});
Expand Down Expand Up @@ -122,19 +121,6 @@ describe('Unit test cases for groupEventsByEndpoint', () => {
},
},
],
trackEvents: [
{
message: {
endpoint: '/track',
body: {
JSON_ARRAY: {
batch: '[{prop:4}]',
},
},
userId: 'user1',
},
},
],
importEvents: [
{
message: {
Expand Down
2 changes: 1 addition & 1 deletion test/integrations/destinations/mp/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const defaultMockFns = () => {
const sampleDestination: Destination = {
Config: {
apiKey: 'dummyApiKey',
token: 'dummyApiKey',
token: 'test_api_token',
prefixProperties: true,
useNativeSDK: false,
},
Expand Down
Loading

0 comments on commit 2250af8

Please sign in to comment.