From 8cdc67caf202003c9ce86895adf552e6f0138d5d Mon Sep 17 00:00:00 2001 From: Georg Fritzsche Date: Wed, 5 Apr 2017 00:38:10 +0700 Subject: [PATCH] Bug 1349324 - Use hourly precision for sessionStartDate & subsessionStartDate. r=dexter --- .../components/telemetry/TelemetrySession.jsm | 4 +-- .../components/telemetry/TelemetryUtils.jsm | 13 ++++++++- .../telemetry/docs/data/main-ping.rst | 4 +-- .../tests/unit/test_TelemetrySession.js | 27 +++++++------------ 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/toolkit/components/telemetry/TelemetrySession.jsm b/toolkit/components/telemetry/TelemetrySession.jsm index ad9856fcbe01..10d31ef67d05 100644 --- a/toolkit/components/telemetry/TelemetrySession.jsm +++ b/toolkit/components/telemetry/TelemetrySession.jsm @@ -1041,8 +1041,8 @@ var Impl = { * @return The metadata as a JS object */ getMetadata: function getMetadata(reason) { - const sessionStartDate = Utils.toLocalTimeISOString(Utils.truncateToDays(this._sessionStartDate)); - const subsessionStartDate = Utils.toLocalTimeISOString(Utils.truncateToDays(this._subsessionStartDate)); + const sessionStartDate = Utils.toLocalTimeISOString(Utils.truncateToHours(this._sessionStartDate)); + const subsessionStartDate = Utils.toLocalTimeISOString(Utils.truncateToHours(this._subsessionStartDate)); const monotonicNow = Policy.monotonicNow(); let ret = { diff --git a/toolkit/components/telemetry/TelemetryUtils.jsm b/toolkit/components/telemetry/TelemetryUtils.jsm index 53fbad30fd68..f76067aa0454 100644 --- a/toolkit/components/telemetry/TelemetryUtils.jsm +++ b/toolkit/components/telemetry/TelemetryUtils.jsm @@ -50,7 +50,7 @@ this.TelemetryUtils = { }, /** - * Takes a date and returns it trunctated to a date with daily precision. + * Takes a date and returns it truncated to a date with daily precision. */ truncateToDays(date) { return new Date(date.getFullYear(), @@ -59,6 +59,17 @@ this.TelemetryUtils = { 0, 0, 0, 0); }, + /** + * Takes a date and returns it truncated to a date with hourly precision. + */ + truncateToHours(date) { + return new Date(date.getFullYear(), + date.getMonth(), + date.getDate(), + date.getHours(), + 0, 0, 0); + }, + /** * Check if the difference between the times is within the provided tolerance. * @param {Number} t1 A time in milliseconds. diff --git a/toolkit/components/telemetry/docs/data/main-ping.rst b/toolkit/components/telemetry/docs/data/main-ping.rst index 09c90e25cb7b..1f2a7c3bb5a5 100644 --- a/toolkit/components/telemetry/docs/data/main-ping.rst +++ b/toolkit/components/telemetry/docs/data/main-ping.rst @@ -44,8 +44,8 @@ Structure: subsessionCounter: , // the running no. of this subsession since the start of the browser session profileSubsessionCounter: , // the running no. of all subsessions for the whole profile life time - sessionStartDate: , // daily precision - subsessionStartDate: , // daily precision, ISO date in local time + sessionStartDate: , // hourly precision, ISO date in local time + subsessionStartDate: , // hourly precision, ISO date in local time sessionLength: , // the session length until now in seconds, monotonic subsessionLength: , // the subsession length in seconds, monotonic diff --git a/toolkit/components/telemetry/tests/unit/test_TelemetrySession.js b/toolkit/components/telemetry/tests/unit/test_TelemetrySession.js index 49ae25574714..012796211f26 100644 --- a/toolkit/components/telemetry/tests/unit/test_TelemetrySession.js +++ b/toolkit/components/telemetry/tests/unit/test_TelemetrySession.js @@ -77,13 +77,6 @@ XPCOMUtils.defineLazyGetter(this, "DATAREPORTING_PATH", function() { var gClientID = null; var gMonotonicNow = 0; -function truncateDateToDays(date) { - return new Date(date.getFullYear(), - date.getMonth(), - date.getDate(), - 0, 0, 0, 0); -} - function sendPing() { TelemetrySession.gatherStartup(); if (PingServer.started) { @@ -571,8 +564,8 @@ add_task(function* test_simplePing() { PingServer.start(); Preferences.set(PREF_SERVER, "http://localhost:" + PingServer.port); - let now = new Date(2020, 1, 1, 12, 0, 0); - let expectedDate = new Date(2020, 1, 1, 0, 0, 0); + let now = new Date(2020, 1, 1, 12, 5, 6); + let expectedDate = new Date(2020, 1, 1, 12, 0, 0); fakeNow(now); gMonotonicNow = fakeMonotonicNow(gMonotonicNow + 5000); @@ -765,7 +758,7 @@ add_task(function* test_checkSubsessionHistograms() { } let now = new Date(2020, 1, 1, 12, 0, 0); - let expectedDate = new Date(2020, 1, 1, 0, 0, 0); + let expectedDate = new Date(2020, 1, 1, 12, 0, 0); fakeNow(now); yield TelemetryController.testReset(); @@ -997,7 +990,7 @@ add_task(function* test_dailyCollection() { } let now = new Date(2030, 1, 1, 12, 0, 0); - let nowDay = new Date(2030, 1, 1, 0, 0, 0); + let nowHour = new Date(2030, 1, 1, 12, 0, 0); let schedulerTickCallback = null; PingServer.clearRequests(); @@ -1026,8 +1019,8 @@ add_task(function* test_dailyCollection() { keyed.add("b", 1); // Make sure the daily ping gets triggered. - let expectedDate = nowDay; - now = futureDate(nowDay, MS_IN_ONE_DAY); + let expectedDate = nowHour; + now = futureDate(nowHour, MS_IN_ONE_DAY); fakeNow(now); Assert.ok(!!schedulerTickCallback); @@ -1236,7 +1229,7 @@ add_task(function* test_environmentChange() { // Trigger and collect environment-change ping. gMonotonicNow = fakeMonotonicNow(gMonotonicNow + 10 * MILLISECONDS_PER_MINUTE); - let startDay = truncateDateToDays(now); + let startHour = TelemetryUtils.truncateToHours(now); now = fakeNow(futureDate(now, 10 * MILLISECONDS_PER_MINUTE)); Preferences.set(PREF_TEST, 1); @@ -1247,13 +1240,13 @@ add_task(function* test_environmentChange() { Assert.equal(ping.environment.settings.userPrefs[PREF_TEST], undefined); Assert.equal(ping.payload.info.reason, REASON_ENVIRONMENT_CHANGE); let subsessionStartDate = new Date(ping.payload.info.subsessionStartDate); - Assert.equal(subsessionStartDate.toISOString(), startDay.toISOString()); + Assert.equal(subsessionStartDate.toISOString(), startHour.toISOString()); Assert.equal(ping.payload.histograms[COUNT_ID].sum, 1); Assert.equal(ping.payload.keyedHistograms[KEYED_ID]["a"].sum, 1); // Trigger and collect another ping. The histograms should be reset. - startDay = truncateDateToDays(now); + startHour = TelemetryUtils.truncateToHours(now); gMonotonicNow = fakeMonotonicNow(gMonotonicNow + 10 * MILLISECONDS_PER_MINUTE); now = fakeNow(futureDate(now, 10 * MILLISECONDS_PER_MINUTE)); @@ -1265,7 +1258,7 @@ add_task(function* test_environmentChange() { Assert.equal(ping.environment.settings.userPrefs[PREF_TEST], 1); Assert.equal(ping.payload.info.reason, REASON_ENVIRONMENT_CHANGE); subsessionStartDate = new Date(ping.payload.info.subsessionStartDate); - Assert.equal(subsessionStartDate.toISOString(), startDay.toISOString()); + Assert.equal(subsessionStartDate.toISOString(), startHour.toISOString()); Assert.equal(ping.payload.histograms[COUNT_ID].sum, 0); Assert.ok(!(KEYED_ID in ping.payload.keyedHistograms));