Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / file_manager / file_manager / foreground / js / metrics.js
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.
4
5 /**
6  * @fileoverview Utility methods for accessing chrome.metricsPrivate API.
7  *
8  * To be included as a first script in main.html
9  */
10
11 var metrics = {};
12
13 /**
14  * A map from interval name to interval start timestamp.
15  */
16 metrics.intervals = {};
17
18 /**
19  * Start the named time interval.
20  * Should be followed by a call to recordInterval with the same name.
21  *
22  * @param {string} name Unique interval name.
23  */
24 metrics.startInterval = function(name) {
25   metrics.intervals[name] = Date.now();
26 };
27
28 metrics.startInterval('Load.Total');
29 metrics.startInterval('Load.Script');
30
31 /**
32  * Convert a short metric name to the full format.
33  *
34  * @param {string} name Short metric name.
35  * @return {string} Full metric name.
36  * @private
37  */
38 metrics.convertName_ = function(name) {
39   return 'FileBrowser.' + name;
40 };
41
42 /**
43  * Wrapper method for calling chrome.fileManagerPrivate safely.
44  * @param {string} methodName Method name.
45  * @param {Array.<Object>} args Arguments.
46  * @private
47  */
48 metrics.call_ = function(methodName, args) {
49   try {
50     chrome.metricsPrivate[methodName].apply(chrome.metricsPrivate, args);
51   } catch (e) {
52     console.error(e.stack);
53   }
54   if (metrics.log)
55     console.log('chrome.metricsPrivate.' + methodName, args);
56 };
57
58 /**
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.
62  */
63 metrics.recordMediumCount = function(name, value) {
64   metrics.call_('recordMediumCount', [metrics.convertName_(name), value]);
65 };
66
67 /**
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.
71  */
72 metrics.recordSmallCount = function(name, value) {
73   metrics.call_('recordSmallCount', [metrics.convertName_(name), value]);
74 };
75
76 /**
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.
80  */
81 metrics.recordTime = function(name, time) {
82   metrics.call_('recordTime', [metrics.convertName_(name), time]);
83 };
84
85 /**
86  * Records an action performed by the user.
87  * @param {string} name Short metric name.
88  */
89 metrics.recordUserAction = function(name) {
90   metrics.call_('recordUserAction', [metrics.convertName_(name)]);
91 };
92
93 /**
94  * Complete the time interval recording.
95  *
96  * Should be preceded by a call to startInterval with the same name. *
97  *
98  * @param {string} name Unique interval name.
99  */
100 metrics.recordInterval = function(name) {
101   if (name in metrics.intervals) {
102     metrics.recordTime(name, Date.now() - metrics.intervals[name]);
103   } else {
104     console.error('Unknown interval: ' + name);
105   }
106 };
107
108 /**
109  * Record an enum value.
110  *
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.
115  */
116 metrics.recordEnum = function(name, value, validValues) {
117   var boundaryValue;
118   var index;
119   if (validValues.constructor.name == 'Array') {
120     index = validValues.indexOf(value);
121     boundaryValue = validValues.length;
122   } else {
123     index = /** @type {number} */ (value);
124     boundaryValue = validValues;
125   }
126   // Collect invalid values in the overflow bucket at the end.
127   if (index < 0 || index > boundaryValue)
128     index = boundaryValue;
129
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)
134   var metricDescr = {
135     'metricName': metrics.convertName_(name),
136     'type': 'histogram-linear',
137     'min': 1,
138     'max': boundaryValue,
139     'buckets': boundaryValue + 1
140   };
141   metrics.call_('recordValue', [metricDescr, index]);
142 };