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.fileManagerPrivate safely.
44 * @param {string} methodName Method name.
45 * @param {Array.<Object>} args Arguments.
48 metrics.call_ = function(methodName, args) {
50 chrome.metricsPrivate[methodName].apply(chrome.metricsPrivate, args);
52 console.error(e.stack);
55 console.log('chrome.metricsPrivate.' + methodName, args);
59 * Records a value than can range from 1 to 10,000.
60 * @param {string} name Short metric name.
61 * @param {number} value Value to be recorded.
63 metrics.recordMediumCount = function(name, value) {
64 metrics.call_('recordMediumCount', [metrics.convertName_(name), value]);
68 * Records a value than can range from 1 to 100.
69 * @param {string} name Short metric name.
70 * @param {number} value Value to be recorded.
72 metrics.recordSmallCount = function(name, value) {
73 metrics.call_('recordSmallCount', [metrics.convertName_(name), value]);
77 * Records an elapsed time of no more than 10 seconds.
78 * @param {string} name Short metric name.
79 * @param {number} time Time to be recorded in milliseconds.
81 metrics.recordTime = function(name, time) {
82 metrics.call_('recordTime', [metrics.convertName_(name), time]);
86 * Records an action performed by the user.
87 * @param {string} name Short metric name.
89 metrics.recordUserAction = function(name) {
90 metrics.call_('recordUserAction', [metrics.convertName_(name)]);
94 * Complete the time interval recording.
96 * Should be preceded by a call to startInterval with the same name. *
98 * @param {string} name Unique interval name.
100 metrics.recordInterval = function(name) {
101 if (name in metrics.intervals) {
102 metrics.recordTime(name, Date.now() - metrics.intervals[name]);
104 console.error('Unknown interval: ' + name);
109 * Record an enum value.
111 * @param {string} name Metric name.
112 * @param {*} value Enum value.
113 * @param {Array.<*>|number} validValues Array of valid values
114 * or a boundary number value.
116 metrics.recordEnum = function(name, value, validValues) {
119 if (validValues.constructor.name == 'Array') {
120 index = validValues.indexOf(value);
121 boundaryValue = validValues.length;
123 index = /** @type {number} */ (value);
124 boundaryValue = validValues;
126 // Collect invalid values in the overflow bucket at the end.
127 if (index < 0 || index > boundaryValue)
128 index = boundaryValue;
130 // Setting min to 1 looks strange but this is exactly the recommended way
131 // of using histograms for enum-like types. Bucket #0 works as a regular
132 // bucket AND the underflow bucket.
133 // (Source: UMA_HISTOGRAM_ENUMERATION definition in base/metrics/histogram.h)
135 'metricName': metrics.convertName_(name),
136 'type': 'histogram-linear',
138 'max': boundaryValue,
139 'buckets': boundaryValue + 1
141 metrics.call_('recordValue', [metricDescr, index]);