Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue SB-11205 fix: adding ets fixes error correction from server ts #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion js/core/telemetryV3Interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ var Telemetry = (function() {
}
}

var calculateTsDifference = function (tsObj) {
var tsDiff = 0;
if (tsObj && tsObj.serverTime) {
var serverTime = tsObj.serverTime;
var localTime = tsObj.localTime;
tsDiff = Math.round((serverTime.getTime() - localTime.getTime()));
}
return tsDiff;
}

/**
* Which is used to initialize the telemetry in globally.
* @param {object} config [Telemetry configurations]
Expand All @@ -337,6 +347,7 @@ var Telemetry = (function() {
}
config.batchsize = config.batchsize ? (config.batchsize < 10 ? 10 : (config.batchsize > 1000 ? 1000 : config.batchsize)) : _defaultValue.batchsize;
Telemetry.config = Object.assign(_defaultValue, config);
Telemetry.config.tsDiff = calculateTsDifference(Telemetry.config.timeStampData);
Telemetry.initialized = true;
telemetryInstance.dispatcher = Telemetry.config.dispatcher ? Telemetry.config.dispatcher : libraryDispatcher;
instance.updateConfigurations(config);
Expand Down Expand Up @@ -385,7 +396,7 @@ var Telemetry = (function() {
*/
instance.getEvent = function(eventId, data) {
telemetryInstance.telemetryEnvelop.eid = eventId;
telemetryInstance.telemetryEnvelop.ets = (new Date()).getTime();
telemetryInstance.telemetryEnvelop.ets = new Number(new Date().getTime()) + Telemetry.config.tsDiff;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the cast to Number necessary?

telemetryInstance.telemetryEnvelop.ver = Telemetry._version;
telemetryInstance.telemetryEnvelop.mid = '';
telemetryInstance.telemetryEnvelop.actor = Object.assign({}, { "id": Telemetry.config.uid || 'anonymous', "type": 'User' }, instance.getUpdatedValue('actor'));
Expand Down
17 changes: 16 additions & 1 deletion js/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2187,6 +2187,16 @@ var Telemetry = (function() {
}
}

var calculateTsDifference = function (tsObj) {
var tsDiff = 0;
if (tsObj && tsObj.serverTime) {
var serverTime = tsObj.serverTime;
var localTime = tsObj.localTime;
tsDiff = Math.round((serverTime.getTime() - localTime.getTime()));
}
return tsDiff;
}

/**
* Which is used to initialize the telemetry in globally.
* @param {object} config [Telemetry configurations]
Expand All @@ -2207,6 +2217,7 @@ var Telemetry = (function() {
}
config.batchsize = config.batchsize ? (config.batchsize < 10 ? 10 : (config.batchsize > 1000 ? 1000 : config.batchsize)) : _defaultValue.batchsize;
Telemetry.config = Object.assign(_defaultValue, config);
Telemetry.config.tsDiff = calculateTsDifference(Telemetry.config.timeStampData);
Telemetry.initialized = true;
telemetryInstance.dispatcher = Telemetry.config.dispatcher ? Telemetry.config.dispatcher : libraryDispatcher;
instance.updateConfigurations(config);
Expand Down Expand Up @@ -2255,7 +2266,7 @@ var Telemetry = (function() {
*/
instance.getEvent = function(eventId, data) {
telemetryInstance.telemetryEnvelop.eid = eventId;
telemetryInstance.telemetryEnvelop.ets = (new Date()).getTime();
telemetryInstance.telemetryEnvelop.ets = new Number(new Date().getTime()) + Telemetry.config.tsDiff;
telemetryInstance.telemetryEnvelop.ver = Telemetry._version;
telemetryInstance.telemetryEnvelop.mid = '';
telemetryInstance.telemetryEnvelop.actor = Object.assign({}, { "id": Telemetry.config.uid || 'anonymous', "type": 'User' }, instance.getUpdatedValue('actor'));
Expand Down Expand Up @@ -2373,6 +2384,9 @@ var Telemetry = (function() {
},
audio: {
timeout: 1000,
// On iOS 11, audio context can only be used in response to user interaction.
// We require users to explicitly enable audio fingerprinting on iOS 11.
// See https://stackoverflow.com/questions/46363048/onaudioprocess-not-called-on-ios11#46534088
excludeIOS11: true
},
fonts: {
Expand All @@ -2382,6 +2396,7 @@ var Telemetry = (function() {
extendedJsFonts: false
},
screen: {
// To ensure consistent fingerprints when users rotate their mobile devices
detectScreenOrientation: true
},
plugins: {
Expand Down
32 changes: 32 additions & 0 deletions js/test/spec/telemetryV3Interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,5 +563,37 @@ describe("Telemetry tests", function() {
telemetryObj.initialize(config);
expect(telemetryObj.initialize).toHaveBeenCalled();
})
it("It should calculate and add timestamp difference to config when valid timestamp object is present", function() {
var localTime = new Date();
localTime.setSeconds(localTime.getSeconds()+10);
EkTelemetry.initialized = false;
spyOn(telemetryObj, 'initialize').and.callThrough();
module = 'undefined'
console.log("Module", module)
config.pdata = { "id": "in.ekstep", "ver": "1.0", "pid": "" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the reference to "in.ekstep" use "org.sunbird" instead

config.runningEnv = 'client';
config.batchsize = 9;
config.uid = '343242';
config.did = '4335345435'
config.channel = 'in.ekstep.'
config.timeStampData = {serverTime: new Date(),localTime: localTime}
telemetryObj.initialize(config);
expect(EkTelemetry.config.tsDiff).not.toEqual(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not enough assertions. Assert for the exact value by setting the server time to a fixed value as well. Also assert for the right event timestamp by firing an event.

})
it("It should consider 0 as timestamp difference when timestamp object is not present", function() {
EkTelemetry.initialized = false;
spyOn(telemetryObj, 'initialize').and.callThrough();
module = 'undefined'
console.log("Module", module)
config.pdata = { "id": "in.ekstep", "ver": "1.0", "pid": "" }
config.runningEnv = 'client';
config.batchsize = 9;
config.uid = '343242';
config.did = '4335345435'
config.channel = 'in.ekstep.'
config.timeStampData = undefined
telemetryObj.initialize(config);
expect(EkTelemetry.config.tsDiff).toEqual(0)
})
})
});