1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * @fileoverview Utility methods for accessing chrome.metricsPrivate API.
8 * To be included as a first script in main.html
14 * A map from interval name to interval start timestamp.
16 metrics.intervals = {};
19 * Start the named time interval.
20 * Should be followed by a call to recordInterval with the same name.
22 * @param {string} name Unique interval name.
24 metrics.startInterval = function(name) {
25 metrics.intervals[name] = Date.now();
28 metrics.startInterval('Load.Total');
29 metrics.startInterval('Load.Script');
32 * Convert a short metric name to the full format.
34 * @param {string} name Short metric name.
35 * @return {string} Full metric name.
38 metrics.convertName_ = function(name) {
39 return 'FileBrowser.' + name;
43 * Wrapper method for calling chrome.fileBrowserPrivate safely.
44 * @param {string} name Method name.
45 * @param {Array.<Object>} args Arguments.
48 metrics.call_ = function(name, args) {
50 chrome.metricsPrivate[name].apply(chrome.metricsPrivate, args);
52 console.error(e.stack);
57 * Create a decorator function that calls a chrome.metricsPrivate function
58 * with the same name and correct parameters.
60 * @param {string} name Method name.
62 metrics.decorate = function(name) {
63 metrics[name] = function() {
64 var args = Array.apply(null, arguments);
65 args[0] = metrics.convertName_(args[0]);
66 metrics.call_(name, args);
68 console.log('chrome.metricsPrivate.' + name, args);
73 metrics.decorate('recordMediumCount');
74 metrics.decorate('recordSmallCount');
75 metrics.decorate('recordTime');
76 metrics.decorate('recordUserAction');
79 * Complete the time interval recording.
81 * Should be preceded by a call to startInterval with the same name. *
83 * @param {string} name Unique interval name.
85 metrics.recordInterval = function(name) {
86 if (name in metrics.intervals) {
87 metrics.recordTime(name, Date.now() - metrics.intervals[name]);
89 console.error('Unknown interval: ' + name);
94 * Record an enum value.
96 * @param {string} name Metric name.
97 * @param {Object} value Enum value.
98 * @param {Array.<Object>|number} validValues Array of valid values
99 * or a boundary number value.
101 metrics.recordEnum = function(name, value, validValues) {
104 if (validValues.constructor.name == 'Array') {
105 index = validValues.indexOf(value);
106 boundaryValue = validValues.length;
109 boundaryValue = validValues;
111 // Collect invalid values in the overflow bucket at the end.
112 if (index < 0 || index > boundaryValue)
113 index = boundaryValue;
115 // Setting min to 1 looks strange but this is exactly the recommended way
116 // of using histograms for enum-like types. Bucket #0 works as a regular
117 // bucket AND the underflow bucket.
118 // (Source: UMA_HISTOGRAM_ENUMERATION definition in base/metrics/histogram.h)
120 'metricName': metrics.convertName_(name),
121 'type': 'histogram-linear',
123 'max': boundaryValue,
124 'buckets': boundaryValue + 1
126 metrics.call_('recordValue', [metricDescr, index]);
128 console.log('chrome.metricsPrivate.recordValue',
129 [metricDescr.metricName, index, value]);