Skip to content

Commit 9c3d2f9

Browse files
committed
allow omitting batch timestamp
1 parent 6b15343 commit 9c3d2f9

File tree

5 files changed

+90
-5
lines changed

5 files changed

+90
-5
lines changed

src/mockBatchCreator.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,26 @@ import { convertEvents } from './sdkToEventsApiConverter';
66
import * as EventsApi from '@mparticle/event-models';
77
import { Batch } from '@mparticle/event-models';
88
import { IMPSideloadedKit } from './sideloadedKit';
9+
import { IStore, SDKConfig } from './store';
910

1011
const mockFunction = function() {
1112
return null;
1213
};
1314
export default class _BatchValidator {
15+
private configOverride?: Partial<Pick<SDKConfig, 'omitBatchTimestamp'>>;
16+
private storeOverride?: Partial<Pick<IStore, 'batchTimestampUnixtimeMsOverride'>>;
17+
18+
constructor({
19+
configOverride = {},
20+
storeOverride = {}
21+
}: {
22+
configOverride?: Partial<Pick<SDKConfig, 'omitBatchTimestamp'>>,
23+
storeOverride?: Partial<Pick<IStore, 'batchTimestampUnixtimeMsOverride'>>
24+
} = {}) {
25+
this.configOverride = configOverride
26+
this.storeOverride = storeOverride
27+
}
28+
1429
private getMPInstance() {
1530
return ({
1631
// Certain Helper, Store, and Identity properties need to be mocked to be used in the `returnBatch` method
@@ -87,7 +102,9 @@ export default class _BatchValidator {
87102
SDKConfig: {
88103
isDevelopmentMode: false,
89104
onCreateBatch: mockFunction,
105+
omitBatchTimestamp: this.configOverride?.omitBatchTimestamp,
90106
},
107+
batchTimestampUnixtimeMsOverride: this.storeOverride?.batchTimestampUnixtimeMsOverride
91108
},
92109
config: null,
93110
eCommerce: null,

src/sdkRuntimeModels.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ export interface SDKInitConfig
250250

251251
workspaceToken?: string;
252252
isDevelopmentMode?: boolean;
253+
omitBatchTimestamp?: boolean;
253254

254255
// https://go.mparticle.com/work/SQDSDKS-6460
255256
identityCallback?: IdentityCallback;

src/sdkToEventsApiConverter.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,25 @@ export function convertEvents(
5656
currentConsentState = user.getConsentState();
5757
}
5858

59+
// determine what timestamp, if any, to use for the batch
60+
const { omitBatchTimestamp } = mpInstance._Store.SDKConfig;
61+
const { batchTimestampUnixtimeMsOverride } = mpInstance._Store;
62+
63+
let timestamp_unixtime_ms: number | null
64+
65+
if (batchTimestampUnixtimeMsOverride) {
66+
timestamp_unixtime_ms = batchTimestampUnixtimeMsOverride;
67+
} else if (omitBatchTimestamp) {
68+
timestamp_unixtime_ms = null;
69+
} else {
70+
timestamp_unixtime_ms = new Date().getTime();
71+
}
72+
73+
5974
const upload: EventsApi.Batch = {
6075
source_request_id: mpInstance._Helpers.generateUniqueId(),
6176
mpid,
62-
timestamp_unixtime_ms: new Date().getTime(),
77+
timestamp_unixtime_ms,
6378
environment: lastEvent.Debug
6479
? EventsApi.BatchEnvironmentEnum.development
6580
: EventsApi.BatchEnvironmentEnum.production,

src/store.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export interface SDKConfig {
8989
webviewBridgeName?: string;
9090
workspaceToken?: string;
9191
requiredWebviewBridgeName?: string;
92+
omitBatchTimestamp?: boolean;
9293
}
9394

9495
function createSDKConfig(config: SDKInitConfig): SDKConfig {
@@ -182,6 +183,7 @@ export interface IStore {
182183
integrationDelayTimeoutStart: number; // UNIX Timestamp
183184
webviewBridgeEnabled?: boolean;
184185
wrapperSDKInfo: WrapperSDKInfo;
186+
batchTimestampUnixtimeMsOverride?: number
185187

186188
persistenceData?: IPersistenceMinified;
187189

@@ -477,6 +479,10 @@ export default function Store(
477479
this.SDKConfig.onCreateBatch = undefined;
478480
}
479481
}
482+
483+
if (config.hasOwnProperty('omitBatchTimestamp')) {
484+
this.SDKConfig.omitBatchTimestamp = config.omitBatchTimestamp;
485+
}
480486
}
481487

482488
this._getFromPersistence = <T>(mpid: MPID, key: string): T | null => {

test/src/tests-mockBatchCreator.ts

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ import { BaseEvent } from '../../src/sdkRuntimeModels';
33
import { expect } from 'chai';
44

55
describe('Create a batch from a base event', () => {
6-
const batchValidator = new _BatchValidator();
76
const baseEvent: BaseEvent = {
87
messageType: 4,
98
name: 'testEvent'
109
}
11-
10+
1211
it('creates a batch with base event ', done => {
12+
const now = new Date().getTime();
13+
const batchValidator = new _BatchValidator()
14+
1315
let batch = batchValidator.returnBatch(baseEvent);
1416

1517
expect(batch).to.have.property('environment').equal('production');
1618
expect(batch).to.have.property('source_request_id').equal('mockId');
1719
expect(batch).to.have.property('mpid').equal('0');
18-
expect(batch).to.have.property('timestamp_unixtime_ms')
20+
expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(now);
1921
expect(batch).to.have.property('mp_deviceid');
2022
expect(batch).to.have.property('sdk_version')
2123
expect(batch).to.have.property('application_info');
@@ -47,12 +49,56 @@ describe('Create a batch from a base event', () => {
4749
batch = batchValidator.returnBatch(baseEvent);
4850
expect(batch.events[0].data).to.have.property('custom_attributes');
4951
expect(batch.events[0].data.custom_attributes).to.have.property('attrFoo', 'attrBar');
50-
52+
5153
baseEvent.customFlags = { flagFoo: 'flagBar' }
5254
batch = batchValidator.returnBatch(baseEvent);
5355
expect(batch.events[0].data).to.have.property('custom_flags');
5456
expect(batch.events[0].data.custom_flags).to.have.property('flagFoo', 'flagBar');
5557

5658
done();
5759
});
60+
61+
[undefined, null, false, true].forEach(omitBatchTimestamp => {
62+
it(`respects an omitBatchTimestamp config value of ${omitBatchTimestamp}`, done => {
63+
const now = new Date().getTime();
64+
const batchValidator = new _BatchValidator({
65+
configOverride: {omitBatchTimestamp}
66+
});
67+
68+
const batch = batchValidator.returnBatch(baseEvent);
69+
70+
if (omitBatchTimestamp) {
71+
expect(batch).to.have.property('timestamp_unixtime_ms', null);
72+
} else {
73+
expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(now);
74+
}
75+
76+
done();
77+
});
78+
})
79+
80+
it(`can use a batch timestamp override value`, done => {
81+
const oneDayAgo = new Date().getTime() - (24 * 3600 * 1000);
82+
const batchValidator = new _BatchValidator({
83+
storeOverride: {batchTimestampUnixtimeMsOverride: oneDayAgo}
84+
});
85+
const batch = batchValidator.returnBatch(baseEvent);
86+
87+
expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(oneDayAgo);
88+
89+
done();
90+
});
91+
92+
it(`a batch timestamp override takes precedence over the sdk config`, done => {
93+
const oneDayAgo = new Date().getTime() - (24 * 3600 * 1000);
94+
const batchValidator = new _BatchValidator({
95+
configOverride: {omitBatchTimestamp: true},
96+
storeOverride: {batchTimestampUnixtimeMsOverride: oneDayAgo}}
97+
);
98+
const batch = batchValidator.returnBatch(baseEvent);
99+
100+
expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(oneDayAgo);
101+
102+
done();
103+
});
58104
});

0 commit comments

Comments
 (0)