Skip to content

Commit

Permalink
allow omitting batch timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
gmhewett committed Nov 6, 2024
1 parent 6b15343 commit 9c3d2f9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/mockBatchCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@ import { convertEvents } from './sdkToEventsApiConverter';
import * as EventsApi from '@mparticle/event-models';
import { Batch } from '@mparticle/event-models';
import { IMPSideloadedKit } from './sideloadedKit';
import { IStore, SDKConfig } from './store';

const mockFunction = function() {
return null;
};
export default class _BatchValidator {
private configOverride?: Partial<Pick<SDKConfig, 'omitBatchTimestamp'>>;
private storeOverride?: Partial<Pick<IStore, 'batchTimestampUnixtimeMsOverride'>>;

constructor({
configOverride = {},
storeOverride = {}
}: {
configOverride?: Partial<Pick<SDKConfig, 'omitBatchTimestamp'>>,
storeOverride?: Partial<Pick<IStore, 'batchTimestampUnixtimeMsOverride'>>
} = {}) {
this.configOverride = configOverride
this.storeOverride = storeOverride
}

private getMPInstance() {
return ({
// Certain Helper, Store, and Identity properties need to be mocked to be used in the `returnBatch` method
Expand Down Expand Up @@ -87,7 +102,9 @@ export default class _BatchValidator {
SDKConfig: {
isDevelopmentMode: false,
onCreateBatch: mockFunction,
omitBatchTimestamp: this.configOverride?.omitBatchTimestamp,
},
batchTimestampUnixtimeMsOverride: this.storeOverride?.batchTimestampUnixtimeMsOverride
},
config: null,
eCommerce: null,
Expand Down
1 change: 1 addition & 0 deletions src/sdkRuntimeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export interface SDKInitConfig

workspaceToken?: string;
isDevelopmentMode?: boolean;
omitBatchTimestamp?: boolean;

// https://go.mparticle.com/work/SQDSDKS-6460
identityCallback?: IdentityCallback;
Expand Down
17 changes: 16 additions & 1 deletion src/sdkToEventsApiConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,25 @@ export function convertEvents(
currentConsentState = user.getConsentState();
}

// determine what timestamp, if any, to use for the batch
const { omitBatchTimestamp } = mpInstance._Store.SDKConfig;
const { batchTimestampUnixtimeMsOverride } = mpInstance._Store;

let timestamp_unixtime_ms: number | null

if (batchTimestampUnixtimeMsOverride) {
timestamp_unixtime_ms = batchTimestampUnixtimeMsOverride;
} else if (omitBatchTimestamp) {
timestamp_unixtime_ms = null;
} else {
timestamp_unixtime_ms = new Date().getTime();
}


const upload: EventsApi.Batch = {
source_request_id: mpInstance._Helpers.generateUniqueId(),
mpid,
timestamp_unixtime_ms: new Date().getTime(),
timestamp_unixtime_ms,
environment: lastEvent.Debug
? EventsApi.BatchEnvironmentEnum.development
: EventsApi.BatchEnvironmentEnum.production,
Expand Down
6 changes: 6 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface SDKConfig {
webviewBridgeName?: string;
workspaceToken?: string;
requiredWebviewBridgeName?: string;
omitBatchTimestamp?: boolean;
}

function createSDKConfig(config: SDKInitConfig): SDKConfig {
Expand Down Expand Up @@ -182,6 +183,7 @@ export interface IStore {
integrationDelayTimeoutStart: number; // UNIX Timestamp
webviewBridgeEnabled?: boolean;
wrapperSDKInfo: WrapperSDKInfo;
batchTimestampUnixtimeMsOverride?: number

persistenceData?: IPersistenceMinified;

Expand Down Expand Up @@ -477,6 +479,10 @@ export default function Store(
this.SDKConfig.onCreateBatch = undefined;
}
}

if (config.hasOwnProperty('omitBatchTimestamp')) {
this.SDKConfig.omitBatchTimestamp = config.omitBatchTimestamp;
}
}

this._getFromPersistence = <T>(mpid: MPID, key: string): T | null => {
Expand Down
54 changes: 50 additions & 4 deletions test/src/tests-mockBatchCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import { BaseEvent } from '../../src/sdkRuntimeModels';
import { expect } from 'chai';

describe('Create a batch from a base event', () => {
const batchValidator = new _BatchValidator();
const baseEvent: BaseEvent = {
messageType: 4,
name: 'testEvent'
}

it('creates a batch with base event ', done => {
const now = new Date().getTime();
const batchValidator = new _BatchValidator()

let batch = batchValidator.returnBatch(baseEvent);

expect(batch).to.have.property('environment').equal('production');
expect(batch).to.have.property('source_request_id').equal('mockId');
expect(batch).to.have.property('mpid').equal('0');
expect(batch).to.have.property('timestamp_unixtime_ms')
expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(now);
expect(batch).to.have.property('mp_deviceid');
expect(batch).to.have.property('sdk_version')
expect(batch).to.have.property('application_info');
Expand Down Expand Up @@ -47,12 +49,56 @@ describe('Create a batch from a base event', () => {
batch = batchValidator.returnBatch(baseEvent);
expect(batch.events[0].data).to.have.property('custom_attributes');
expect(batch.events[0].data.custom_attributes).to.have.property('attrFoo', 'attrBar');

baseEvent.customFlags = { flagFoo: 'flagBar' }
batch = batchValidator.returnBatch(baseEvent);
expect(batch.events[0].data).to.have.property('custom_flags');
expect(batch.events[0].data.custom_flags).to.have.property('flagFoo', 'flagBar');

done();
});

[undefined, null, false, true].forEach(omitBatchTimestamp => {
it(`respects an omitBatchTimestamp config value of ${omitBatchTimestamp}`, done => {
const now = new Date().getTime();
const batchValidator = new _BatchValidator({
configOverride: {omitBatchTimestamp}
});

const batch = batchValidator.returnBatch(baseEvent);

if (omitBatchTimestamp) {
expect(batch).to.have.property('timestamp_unixtime_ms', null);
} else {
expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(now);
}

done();
});
})

it(`can use a batch timestamp override value`, done => {
const oneDayAgo = new Date().getTime() - (24 * 3600 * 1000);
const batchValidator = new _BatchValidator({
storeOverride: {batchTimestampUnixtimeMsOverride: oneDayAgo}
});
const batch = batchValidator.returnBatch(baseEvent);

expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(oneDayAgo);

done();
});

it(`a batch timestamp override takes precedence over the sdk config`, done => {
const oneDayAgo = new Date().getTime() - (24 * 3600 * 1000);
const batchValidator = new _BatchValidator({
configOverride: {omitBatchTimestamp: true},
storeOverride: {batchTimestampUnixtimeMsOverride: oneDayAgo}}
);
const batch = batchValidator.returnBatch(baseEvent);

expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(oneDayAgo);

done();
});
});

0 comments on commit 9c3d2f9

Please sign in to comment.