OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * The webapp reads the plugin's connection statistics frequently (once per | 7 * The webapp reads the plugin's connection statistics frequently (once per |
8 * second). It logs statistics to the server less frequently, to keep | 8 * second). It logs statistics to the server less frequently, to keep |
9 * bandwidth and storage costs down. This class bridges that gap, by | 9 * bandwidth and storage costs down. This class bridges that gap, by |
10 * accumulating high-frequency numeric data, and providing statistics | 10 * accumulating high-frequency numeric data, and providing statistics |
11 * summarising that data. | 11 * summarising that data. |
12 */ | 12 */ |
13 | 13 |
14 'use strict'; | 14 'use strict'; |
15 | 15 |
16 /** @suppress {duplicate} */ | 16 /** @suppress {duplicate} */ |
17 var remoting = remoting || {}; | 17 var remoting = remoting || {}; |
18 | 18 |
19 /** | 19 /** |
20 * @constructor | 20 * @constructor |
21 */ | 21 */ |
22 remoting.StatsAccumulator = function() { | 22 remoting.StatsAccumulator = function() { |
23 /** | 23 /** |
24 * A map from names to lists of values. | 24 * A map from names to lists of values. |
25 * @private | 25 * @private |
26 * @type Object.<string, Array.<number>> | 26 * @type Object<string, Array<number>> |
27 */ | 27 */ |
28 this.valueLists_ = {}; | 28 this.valueLists_ = {}; |
29 | 29 |
30 /** | 30 /** |
31 * The first time, after this object was most recently initialized or emptied, | 31 * The first time, after this object was most recently initialized or emptied, |
32 * at which a value was added to this object. | 32 * at which a value was added to this object. |
33 * @private | 33 * @private |
34 * @type {?number} | 34 * @type {?number} |
35 */ | 35 */ |
36 this.timeOfFirstValue_ = null; | 36 this.timeOfFirstValue_ = null; |
37 }; | 37 }; |
38 | 38 |
39 /** | 39 /** |
40 * Adds values to this object. | 40 * Adds values to this object. |
41 * | 41 * |
42 * @param {Object.<string, number>} newValues | 42 * @param {Object<string, number>} newValues |
43 */ | 43 */ |
44 remoting.StatsAccumulator.prototype.add = function(newValues) { | 44 remoting.StatsAccumulator.prototype.add = function(newValues) { |
45 for (var key in newValues) { | 45 for (var key in newValues) { |
46 this.getValueList(key).push(newValues[key]); | 46 this.getValueList(key).push(newValues[key]); |
47 } | 47 } |
48 if (!this.timeOfFirstValue_) { | 48 if (!this.timeOfFirstValue_) { |
49 this.timeOfFirstValue_ = new Date().getTime(); | 49 this.timeOfFirstValue_ = new Date().getTime(); |
50 } | 50 } |
51 }; | 51 }; |
52 | 52 |
(...skipping 19 matching lines...) Expand all Loading... |
72 }; | 72 }; |
73 | 73 |
74 /** | 74 /** |
75 * Calculates the mean of the values for a given key. | 75 * Calculates the mean of the values for a given key. |
76 * | 76 * |
77 * @param {string} key | 77 * @param {string} key |
78 * @return {number} the mean of the values for that key | 78 * @return {number} the mean of the values for that key |
79 */ | 79 */ |
80 remoting.StatsAccumulator.prototype.calcMean = function(key) { | 80 remoting.StatsAccumulator.prototype.calcMean = function(key) { |
81 /** | 81 /** |
82 * @param {Array.<number>} values | 82 * @param {Array<number>} values |
83 * @return {number} | 83 * @return {number} |
84 */ | 84 */ |
85 var calcMean = function(values) { | 85 var calcMean = function(values) { |
86 if (values.length == 0) { | 86 if (values.length == 0) { |
87 return 0.0; | 87 return 0.0; |
88 } | 88 } |
89 var sum = 0; | 89 var sum = 0; |
90 for (var i = 0; i < values.length; i++) { | 90 for (var i = 0; i < values.length; i++) { |
91 sum += values[i]; | 91 sum += values[i]; |
92 } | 92 } |
93 return sum / values.length; | 93 return sum / values.length; |
94 }; | 94 }; |
95 return this.map(key, calcMean); | 95 return this.map(key, calcMean); |
96 }; | 96 }; |
97 | 97 |
98 /** | 98 /** |
99 * Applies a given map to the list of values for a given key. | 99 * Applies a given map to the list of values for a given key. |
100 * | 100 * |
101 * @param {string} key | 101 * @param {string} key |
102 * @param {function(Array.<number>): number} map | 102 * @param {function(Array<number>): number} map |
103 * @return {number} the result of applying that map to the list of values for | 103 * @return {number} the result of applying that map to the list of values for |
104 * that key | 104 * that key |
105 */ | 105 */ |
106 remoting.StatsAccumulator.prototype.map = function(key, map) { | 106 remoting.StatsAccumulator.prototype.map = function(key, map) { |
107 return map(this.getValueList(key)); | 107 return map(this.getValueList(key)); |
108 }; | 108 }; |
109 | 109 |
110 /** | 110 /** |
111 * Gets the list of values for a given key. | 111 * Gets the list of values for a given key. |
112 * If this object contains no values for that key, then this routine creates | 112 * If this object contains no values for that key, then this routine creates |
113 * an empty list, stores it in this object, and returns it. | 113 * an empty list, stores it in this object, and returns it. |
114 * | 114 * |
115 * @private | 115 * @private |
116 * @param {string} key | 116 * @param {string} key |
117 * @return {Array.<number>} the list of values for that key | 117 * @return {Array<number>} the list of values for that key |
118 */ | 118 */ |
119 remoting.StatsAccumulator.prototype.getValueList = function(key) { | 119 remoting.StatsAccumulator.prototype.getValueList = function(key) { |
120 var valueList = this.valueLists_[key]; | 120 var valueList = this.valueLists_[key]; |
121 if (!valueList) { | 121 if (!valueList) { |
122 valueList = []; | 122 valueList = []; |
123 this.valueLists_[key] = valueList; | 123 this.valueLists_[key] = valueList; |
124 } | 124 } |
125 return valueList; | 125 return valueList; |
126 }; | 126 }; |
OLD | NEW |