Skip to content

Commit

Permalink
Remove Unnecessary Delay
Browse files Browse the repository at this point in the history
This removes an unnecessary delay in the Telemetry code that caused
the code to wait an extra minute before trying to send data per page
load.

This also adds logic so that the code will wait until any previous
attempt to send telemetry causes follow-up attempts to wait until
it has completed sending (in the unlikely event that it takes
longer than a minute, we don't want to waste time / resources).
  • Loading branch information
pickypg committed Dec 27, 2018
1 parent 0113e5a commit 2d8417f
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions x-pack/plugins/xpack_main/public/hacks/telemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class Telemetry {
this._telemetryOptedIn = $injector.get('telemetryOptedIn');
this._attributes = this._storage.get(LOCALSTORAGE_KEY) || {};
this._fetchTelemetry = fetchTelemetry;
this._sending = false;
}

_set(key, value) {
Expand Down Expand Up @@ -57,11 +58,16 @@ export class Telemetry {
return false;
}

/*
/**
* Check report permission and if passes, send the report
*
* This function never throws an exception.
*/
_sendIfDue() {
if (!this._checkReportStatus()) { return Promise.resolve(null); }
if (this._sending || !this._checkReportStatus()) { return Promise.resolve(false); }

// mark that we are working so future requests are ignored until we're done
this._sending = true;

return this._fetchTelemetry()
.then(response => {
Expand All @@ -76,22 +82,22 @@ export class Telemetry {
return this._$http(req);
});
})
.then(response => {
// the response object is ignored because we do not check it
.then(() => {
// we sent a report, so we need to record and store the current time stamp
this._set('lastReport', Date.now());
this._saveToBrowser();
return response;
})
.catch(() => {
// no ajaxErrorHandlers for telemetry
return Promise.resolve(null);
});
// no ajaxErrorHandlers for telemetry
.catch(() => null)
.then(() => this._sending = false);
}

/*
/**
* Public method
*/
start() {
// continuously check if it's due time for a report
window.setInterval(() => this._sendIfDue(), 60000);
}

Expand Down

0 comments on commit 2d8417f

Please sign in to comment.