browser/components/tabview/telemetry.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/tabview/telemetry.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,63 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/**
     1.9 + * Collects telemetry data for Tabview.
    1.10 + */
    1.11 +let Telemetry = {
    1.12 +  TOPIC_GATHER_TELEMETRY: "gather-telemetry",
    1.13 +
    1.14 +  /**
    1.15 +   * Initializes the object.
    1.16 +   */
    1.17 +  init: function Telemetry_init() {
    1.18 +    Services.obs.addObserver(this, this.TOPIC_GATHER_TELEMETRY, false);
    1.19 +  }, 
    1.20 +
    1.21 +  /**
    1.22 +   * Uninitializes the object.
    1.23 +   */
    1.24 +  uninit: function Telemetry_uninit() {
    1.25 +    Services.obs.removeObserver(this, this.TOPIC_GATHER_TELEMETRY);
    1.26 +  },
    1.27 +
    1.28 +  /**
    1.29 +   * Adds telemetry values to gather usage statistics.
    1.30 +   */
    1.31 +  _collect: function Telemetry_collect() {
    1.32 +    let stackedGroupsCount = 0;
    1.33 +    let childCounts = [];
    1.34 +
    1.35 +    GroupItems.groupItems.forEach(function (groupItem) {
    1.36 +      if (!groupItem.isEmpty()) {
    1.37 +        childCounts.push(groupItem.getChildren().length);
    1.38 +
    1.39 +        if (groupItem.isStacked())
    1.40 +          stackedGroupsCount++;
    1.41 +      }
    1.42 +    });
    1.43 +
    1.44 +    function addTelemetryValue(aId, aValue) {
    1.45 +      Services.telemetry.getHistogramById("PANORAMA_" + aId).add(aValue);
    1.46 +    }
    1.47 +    function median(aChildCounts) {
    1.48 +      aChildCounts.sort(function(x, y) { return x - y; });
    1.49 +      let middle = Math.floor(aChildCounts.length / 2);
    1.50 +      return aChildCounts[middle];
    1.51 +    }
    1.52 +
    1.53 +    addTelemetryValue("GROUPS_COUNT", GroupItems.groupItems.length);
    1.54 +    addTelemetryValue("STACKED_GROUPS_COUNT", stackedGroupsCount);
    1.55 +    addTelemetryValue("MEDIAN_TABS_IN_GROUPS_COUNT", median(childCounts));
    1.56 +  },
    1.57 +
    1.58 +  /**
    1.59 +   * Observes for gather telemetry topic.
    1.60 +   */
    1.61 +  observe: function Telemetry_observe(aSubject, aTopic, aData) {
    1.62 +    if (!gWindow.PrivateBrowsingUtils.isWindowPrivate(gWindow))
    1.63 +      this._collect();
    1.64 +  }
    1.65 +}
    1.66 +

mercurial