From 18b3a3ad636a24cd5e722feeb884d8c528d5a69e Mon Sep 17 00:00:00 2001 From: groovecoder Date: Thu, 9 Feb 2017 10:59:29 -0600 Subject: [PATCH] for #117: start telemetry with testpilot-metrics and first sort-tabs event --- .eslintignore | 1 + index.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++- package.json | 3 ++- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..9b27377 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +testpilot-metrics.js diff --git a/index.js b/index.js index 1cb0345..cb2b939 100644 --- a/index.js +++ b/index.js @@ -34,6 +34,7 @@ const PREFS = [ const { attachTo, detachFrom } = require("sdk/content/mod"); const { ContextualIdentityService } = require("resource://gre/modules/ContextualIdentityService.jsm"); const { getFavicon } = require("sdk/places/favicon"); +const Metrics = require("./testpilot-metrics"); const { modelFor } = require("sdk/model/core"); const prefService = require("sdk/preferences/service"); const self = require("sdk/self"); @@ -41,11 +42,13 @@ const ss = require("sdk/simple-storage"); const { Style } = require("sdk/stylesheet/style"); const tabs = require("sdk/tabs"); const tabsUtils = require("sdk/tabs/utils"); +const uuid = require("sdk/util/uuid"); const { viewFor } = require("sdk/view/core"); const webExtension = require("sdk/webextension"); const windows = require("sdk/windows"); const windowUtils = require("sdk/window/utils"); + // ---------------------------------------------------------------------------- // ContainerService @@ -60,13 +63,16 @@ const ContainerService = { if (installation) { const object = { version: 1, - prefs: {} + prefs: {}, + metricsUUID: null }; PREFS.forEach(pref => { object.prefs[pref[0]] = prefService.get(pref[0]); }); + object.metricsUUID = uuid.uuid().toString(); + ss.storage.savedConfiguration = object; } @@ -76,6 +82,15 @@ const ContainerService = { prefService.set(pref[0], pref[1]); }); + if (ss.storage.savedConfiguration.hasOwnProperty("metricsUUID")) { + this._metricsUUID = ss.storage.savedConfiguration.metricsUUID; + } else { + // The add-on was installed before metricsUUID was added, create one + this._metricsUUID = uuid.uuid().toString(); + ss.storage.savedConfiguration["metricsUUID"] = this._metricsUUID; + } + + // Message routing // only these methods are allowed. We have a 1:1 mapping between messages @@ -158,10 +173,55 @@ const ContainerService = { }).catch(() => { throw new Error("WebExtension startup failed. Unable to continue."); }); + + this._sendEvent = new Metrics({ + type: "sdk", + id: self.id, + version: self.version + }).sendEvent; + + this._sendTelemetryPayload = function(params = {}) { + let payload = { // eslint-disable-line prefer-const + "uuid": this._metricsUUID + }; + Object.assign(payload, params); + + this._sendEvent(payload); + }; + }, // utility methods + _totalContainerTabsCount() { + let totalContainerTabsCount = 0; + for (const userContextId in this._identitiesState) { + totalContainerTabsCount += this._identitiesState[userContextId].openTabs; + } + return totalContainerTabsCount; + }, + + _totalNonContainerTabsCount() { + let totalNonContainerTabsCount = 0; + for (const tab of tabs) { + if (this._getUserContextIdFromTab(tab) === 0) { + ++totalNonContainerTabsCount; + } + } + return totalNonContainerTabsCount; + }, + + _shownContainersCount() { + let shownContainersCount = 0; + for (const userContextId in this._identitiesState) { + if (this._identitiesState[userContextId].openTabs > 0) { + ++shownContainersCount; + continue; + } + } + return shownContainersCount; + }, + _convert(identity) { // Let's convert the known colors to their color names. return { @@ -350,6 +410,12 @@ const ContainerService = { }, sortTabs() { + this._sendTelemetryPayload({ + "event": "sort-tabs", + "shownContainersCount": this._shownContainersCount(), + "totalContainerTabsCount": this._totalContainerTabsCount(), + "totalNonContainerTabsCount": this._totalNonContainerTabsCount() + }); return new Promise(resolve => { for (let window of windows.browserWindows) { // eslint-disable-line prefer-const // First the pinned tabs, then the normal ones. diff --git a/package.json b/package.json index 3f7fb24..55aadfc 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "jpm": "^1.2.2", "npm-run-all": "^4.0.0", "stylelint": "^7.7.1", - "stylelint-config-standard": "^15.0.1" + "stylelint-config-standard": "^15.0.1", + "testpilot-metrics": "^2.1.0" }, "engines": { "firefox": ">=50.0"