summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/file_manager/js/metrics.js
blob: 3f34e51a299a8c58af7d5631de154c9334864e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Utility methods for accessing chrome.metricsPrivate API.
 *
 * To be included as a first script in main.html
 */

var metrics = {};

/**
 * A map from interval name to interval start timestamp.
 */
metrics.intervals = {};

/**
 * Start the named time interval.
 * Should be followed by a call to recordInterval with the same name.
 *
 * @param {string} name Unique interval name.
 */
metrics.startInterval = function(name) {
  metrics.intervals[name] = Date.now();
};

metrics.startInterval('Load.Total');
metrics.startInterval('Load.Script');

/**
 * Convert a short metric name to the full format.
 *
 * @param {string} name Short metric name.
 * @return {string} Full metric name.
 * @private
 */
metrics.convertName_ = function(name) {
  return 'FileBrowser.' + name;
};

/**
 * Wrapper method for calling chrome.fileBrowserPrivate safely.
 * @param {string} name Method name.
 * @param {Array.<Object>} args Arguments.
 * @private
 */
metrics.call_ = function(name, args) {
  try {
    chrome.metricsPrivate[name].apply(chrome.metricsPrivate, args);
  } catch (e) {
    console.error(e.stack);
  }
};

/**
 * Create a decorator function that calls a chrome.metricsPrivate function
 * with the same name and correct parameters.
 *
 * @param {string} name Method name.
 */
metrics.decorate = function(name) {
  metrics[name] = function() {
    var args = Array.apply(null, arguments);
    args[0] = metrics.convertName_(args[0]);
    metrics.call_(name, args);
    if (metrics.log) {
      console.log('chrome.metricsPrivate.' + name, args);
    }
  };
};

metrics.decorate('recordMediumCount');
metrics.decorate('recordSmallCount');
metrics.decorate('recordTime');
metrics.decorate('recordUserAction');

/**
 * Complete the time interval recording.
 *
 * Should be preceded by a call to startInterval with the same name. *
 *
 * @param {string} name Unique interval name.
 */
metrics.recordInterval = function(name) {
  if (name in metrics.intervals) {
    metrics.recordTime(name, Date.now() - metrics.intervals[name]);
  } else {
    console.error('Unknown interval: ' + name);
  }
};

/**
 * Record an enum value.
 *
 * @param {string} name Metric name.
 * @param {Object} value Enum value.
 * @param {Array.<Object>|number} validValues Array of valid values
 *                                            or a boundary number value.
 */
metrics.recordEnum = function(name, value, validValues) {
  var boundaryValue;
  var index;
  if (validValues.constructor.name == 'Array') {
    index = validValues.indexOf(value);
    boundaryValue = validValues.length;
  } else {
    index = value;
    boundaryValue = validValues;
  }
  // Collect invalid values in the overflow bucket at the end.
  if (index < 0 || index > boundaryValue)
    index = boundaryValue;

  // Setting min to 1 looks strange but this is exactly the recommended way
  // of using histograms for enum-like types. Bucket #0 works as a regular
  // bucket AND the underflow bucket.
  // (Source: UMA_HISTOGRAM_ENUMERATION definition in base/metrics/histogram.h)
  var metricDescr = {
    'metricName': metrics.convertName_(name),
    'type': 'histogram-linear',
    'min': 1,
    'max': boundaryValue,
    'buckets': boundaryValue + 1
  };
  metrics.call_('recordValue', [metricDescr, index]);
  if (metrics.log) {
    console.log('chrome.metricsPrivate.recordValue',
        [metricDescr.metricName, index, value]);
  }
};