OLD | NEW |
---|---|
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /* convert to cr.define('PerformanceMonitor', function() {... | 7 cr.define('performance_monitor', function() { |
8 * when integrating into webui */ | |
9 | |
10 var Installer = function() { | |
11 /** | 8 /** |
12 * Enum for time ranges, giving a descriptive name, time span prior to |now|, | 9 * Enum for time ranges, giving a descriptive name, time span prior to |now|, |
13 * data point resolution, and time-label frequency and format for each. | 10 * data point resolution, and time-label frequency and format for each. |
14 * @enum {{ | 11 * @enum {{ |
15 * value: !number, | 12 * value: !number, |
16 * name: !string, | 13 * name: !string, |
17 * timeSpan: !number, | 14 * timeSpan: !number, |
18 * resolution: !number, | 15 * resolution: !number, |
19 * labelEvery: !number, | 16 * labelEvery: !number, |
20 * format: !string | 17 * format: !string |
(...skipping 15 matching lines...) Expand all Loading... | |
36 // Labels at 168 point (weekly) intervals. | 33 // Labels at 168 point (weekly) intervals. |
37 month: {value: 2, name: 'Last Month', timeSpan: 30 * 24 * 3600 * 1000, | 34 month: {value: 2, name: 'Last Month', timeSpan: 30 * 24 * 3600 * 1000, |
38 resolution: 1000 * 3600, labelEvery: 168, format: 'M/d'}, | 35 resolution: 1000 * 3600, labelEvery: 168, format: 'M/d'}, |
39 | 36 |
40 // Prior quarter (90 days), resolution of 3 hr -- at most 720 data points. | 37 // Prior quarter (90 days), resolution of 3 hr -- at most 720 data points. |
41 // Labels at 112 point (fortnightly) intervals. | 38 // Labels at 112 point (fortnightly) intervals. |
42 quarter: {value: 3, name: 'Last Quarter', timeSpan: 90 * 24 * 3600 * 1000, | 39 quarter: {value: 3, name: 'Last Quarter', timeSpan: 90 * 24 * 3600 * 1000, |
43 resolution: 1000 * 3600 * 3, labelEvery: 112, format: 'M/yy'}, | 40 resolution: 1000 * 3600 * 3, labelEvery: 112, format: 'M/yy'}, |
44 }; | 41 }; |
45 | 42 |
43 /* | |
44 * Offset, in mS, by which to subtract to convert GMT to local time | |
Evan Stade
2012/08/03 17:50:59
isn't milliseconds abbreviated to ms?
clintstaley
2012/08/06 21:06:47
I've seen mS, and even msec, but happy to adjust t
| |
45 * @type {!number} | |
46 */ | |
47 var timezoneOffset = new Date().getTimezoneOffset() * 60000; | |
48 | |
46 /** @constructor */ | 49 /** @constructor */ |
47 function PerformanceMonitor() { | 50 function PerformanceMonitor() { |
48 this.__proto__ = PerformanceMonitor.prototype; | 51 this.__proto__ = PerformanceMonitor.prototype; |
49 /** | 52 /** |
50 * All metrics have entries, but those not displayed have an empty div list. | 53 * All metrics have entries, but those not displayed have an empty div list. |
51 * If a div list is not empty, the associated data will be non-null, or | 54 * If a div list is not empty, the associated data will be non-null, or |
52 * null but about to be filled by webui response. Thus, any metric with | 55 * null but about to be filled by webui response. Thus, any metric with |
53 * non-empty div list but null data is awaiting a data response from the | 56 * non-empty div list but null data is awaiting a data response from the |
54 * webui. | 57 * webui. The webui uses numbers to uniquely identify metric and event |
Dan Beam
2012/08/03 18:40:36
should these say "webui handler" instead of webui?
clintstaley
2012/08/06 21:06:47
Done.
| |
58 * types, so we use the same numbers (in string form) for the map key, | |
59 * repeating the id number in the |id| field, as a number. | |
55 * @type {Object.<string, { | 60 * @type {Object.<string, { |
Dan Beam
2012/08/03 18:40:36
I think wrapping for these long @type expressions
clintstaley
2012/08/06 21:06:47
I've gotten different responses on this from diffe
| |
61 * id: !number, | |
62 * description: !string, | |
63 * units: !string, | |
64 * yAxis: !{max: !number, color: !string}, | |
56 * divs: !Array.<HTMLDivElement>, | 65 * divs: !Array.<HTMLDivElement>, |
57 * yAxis: !{max: !number, color: !string}, | 66 * data: ?Array.<{time: !number, value: !number}> |
58 * data: ?Array.<{time: !number, value: !number}>, | |
59 * description: !string, | |
60 * units: !string | |
61 * }>} | 67 * }>} |
62 * @private | 68 * @private |
63 */ | 69 */ |
64 this.metricMap_ = { | 70 this.metricMap_ = {}; |
65 jankiness: { | |
66 divs: [], | |
67 yAxis: {max: 100, color: 'rgb(255, 128, 128)'}, | |
68 data: null, | |
69 description: 'Jankiness', | |
70 units: 'milliJanks' | |
71 }, | |
72 oddness: { | |
73 divs: [], | |
74 yAxis: {max: 20, color: 'rgb(0, 192, 0)'}, | |
75 data: null, | |
76 description: 'Oddness', | |
77 units: 'kOdds' | |
78 } | |
79 }; | |
80 | 71 |
81 /* | 72 /* |
82 * Similar data for events, though no yAxis info is needed since events | 73 * Similar data for events, though no yAxis info is needed since events |
83 * are simply labelled markers at X locations. Rules regarding null data | 74 * are simply labelled markers at X locations. Rules regarding null data |
84 * with non-empty div list apply here as for metricMap_ above. | 75 * with non-empty div list apply here as for metricMap_ above. |
85 * @type {Object.<string, { | 76 * @type {Object.<number, { |
86 * divs: !Array.<HTMLDivElement>, | 77 * id: !number, |
87 * description: !string, | 78 * description: !string, |
88 * color: !string, | 79 * color: !string, |
80 * divs: !Array.<HTMLDivElement>, | |
89 * data: ?Array.<{time: !number, longDescription: !string}> | 81 * data: ?Array.<{time: !number, longDescription: !string}> |
90 * }>} | 82 * }>} |
91 * @private | 83 * @private |
92 */ | 84 */ |
93 this.eventMap_ = { | 85 this.eventMap_ = {}; |
94 wampusAttacks: { | |
95 divs: [], | |
96 description: 'Wampus Attack', | |
97 color: 'rgb(0, 0, 255)', | |
98 data: null | |
99 }, | |
100 solarEclipses: { | |
101 divs: [], | |
102 description: 'Solar Eclipse', | |
103 color: 'rgb(255, 0, 255)', | |
104 data: null | |
105 } | |
106 }; | |
107 | 86 |
108 /** | 87 /** |
109 * Time periods in which the browser was active and collecting metrics. | 88 * Time periods in which the browser was active and collecting metrics |
110 * and events. | 89 * and events. |
111 * @type {!Array.<{start: !number, end: !number}>} | 90 * @type {!Array.<{start: !number, end: !number}>} |
112 * @private | 91 * @private |
113 */ | 92 */ |
114 this.intervals_ = []; | 93 this.intervals_ = []; |
115 | 94 |
116 this.setupCheckboxes_( | |
117 '#chooseMetrics', this.metricMap_, this.addMetric, this.dropMetric); | |
118 this.setupCheckboxes_( | |
119 '#chooseEvents', this.eventMap_, this.addEventType, this.dropEventType); | |
120 this.setupTimeRangeChooser_(); | 95 this.setupTimeRangeChooser_(); |
96 chrome.send('getAllEventTypes'); | |
97 chrome.send('getAllMetricTypes'); | |
121 this.setupMainChart_(); | 98 this.setupMainChart_(); |
122 TimeRange_.day.element.click().call(this); | 99 TimeRange_.day.element.click(); |
123 } | 100 } |
124 | 101 |
125 PerformanceMonitor.prototype = { | 102 PerformanceMonitor.prototype = { |
126 /** | 103 /** |
104 * Receive a list of all metrics, and populate |this.metricMap_| to | |
105 * reflect said list. Reconfigure the checkbox set for metric selection. | |
106 * @param {Array.<{metricType: !String, shortDescription: !String}>} | |
107 * allMetrics All metrics from which to select. | |
Dan Beam
2012/08/03 18:40:36
nit: 4 \s instead of 2 \s before allMetrics
clintstaley
2012/08/06 21:06:47
Done.
| |
108 */ | |
109 getAllMetricTypesCallback: function(allMetrics) { | |
110 for (var i = 0; i < allMetrics.length; i++) { | |
111 var metric = allMetrics[i]; | |
112 | |
113 this.metricMap_[metric.metricType] = { | |
114 id: metric.metricType, | |
115 description: metric.shortDescription, | |
116 units: metric.units, | |
117 yAxis: {min: 0, max: metric.maxValue, color: 'rgb(0, 0, 255'}, | |
118 divs: [], | |
119 data: null | |
120 }; | |
121 } | |
122 | |
123 this.setupCheckboxes_($('#chooseMetrics')[0], | |
124 this.metricMap_, this.addMetric, this.dropMetric); | |
125 }, | |
126 | |
127 /** | |
128 * Receive a list of all event types, and populate |this.eventMap_| to | |
129 * reflect said list. Reconfigure the checkbox set for event selection. | |
130 * @param {Array.<{eventType: !String, shortDescription: !String}>} | |
131 * allEvents All events from which to select. | |
132 */ | |
133 getAllEventTypesCallback: function(allEvents) { | |
134 for (var i = 0; i < allEvents.length; i++) { | |
135 var eventInfo = allEvents[i]; | |
136 | |
137 this.eventMap_[eventInfo.eventType] = { | |
138 id: eventInfo.eventType, | |
139 color: 'rgb(255, 0, 0)', | |
140 data: null, | |
141 description: eventInfo.shortDescription, | |
142 divs: [] | |
143 }; | |
144 } | |
145 | |
146 this.setupCheckboxes_($('#chooseEvents')[0], | |
147 this.eventMap_, this.addEventType, this.dropEventType); | |
148 }, | |
149 | |
150 /** | |
127 * Set up the radio button set to choose time range. Use div#radioTemplate | 151 * Set up the radio button set to choose time range. Use div#radioTemplate |
128 * as a template. | 152 * as a template. |
129 * @private | 153 * @private |
130 */ | 154 */ |
131 setupTimeRangeChooser_: function() { | 155 setupTimeRangeChooser_: function() { |
132 var timeDiv = $('#chooseTimeRange')[0]; | 156 var timeDiv = $('#chooseTimeRange')[0]; |
133 var radioTemplate = $('#radioTemplate')[0]; | 157 var radioTemplate = $('#radioTemplate')[0]; |
134 | 158 |
135 for (var time in TimeRange_) { | 159 for (var time in TimeRange_) { |
136 var timeRange = TimeRange_[time]; | 160 var timeRange = TimeRange_[time]; |
(...skipping 10 matching lines...) Expand all Loading... | |
147 timeDiv.addEventListener('click', function(e) { | 171 timeDiv.addEventListener('click', function(e) { |
148 if (!e.target.webkitMatchesSelector('input[type="radio"]')) | 172 if (!e.target.webkitMatchesSelector('input[type="radio"]')) |
149 return; | 173 return; |
150 | 174 |
151 this.setTimeRange(e.target.timeRange); | 175 this.setTimeRange(e.target.timeRange); |
152 }.bind(this)); | 176 }.bind(this)); |
153 }, | 177 }, |
154 | 178 |
155 /** | 179 /** |
156 * Generalized function for setting up checkbox blocks for either events | 180 * Generalized function for setting up checkbox blocks for either events |
157 * or metrics. Take a div ID |divId| into which to place the checkboxes, | 181 * or metrics. Take a div |div| into which to place the checkboxes, |
158 * and a map |optionMap| with values that each include a property | 182 * and a map |optionMap| with values that each include a property |
159 * |description|. Set up one checkbox for each entry in |optionMap| | 183 * |description|. Set up one checkbox for each entry in |optionMap| |
160 * labelled with that description. Arrange callbacks to function |check| | 184 * labelled with that description. Arrange callbacks to function |check| |
161 * or |uncheck|, passing them the key of the checked or unchecked option, | 185 * or |uncheck|, passing them the key of the checked or unchecked option, |
162 * when the relevant checkbox state changes. | 186 * when the relevant checkbox state changes. |
163 * @param {!string} divId Id of division into which to put checkboxes | 187 * @param {!HTMLDivElement} div div into which to put checkboxes |
Dan Beam
2012/08/03 18:40:36
Generally we capitalize after the parameter name a
clintstaley
2012/08/06 21:06:47
Done, here and elsewhere. Of course, it's a sente
| |
164 * @param {!Object} optionMap map of metric/event entries | 188 * @param {!Object} optionMap map of metric/event entries |
165 * @param {!function(this:Controller, Object)} check | 189 * @param {!function(this:Controller, Object)} check |
166 * function to select an entry (metric or event) | 190 * function to select an entry (metric or event) |
167 * @param {!function(this:Controller, Object)} uncheck | 191 * @param {!function(this:Controller, Object)} uncheck |
168 * function to deselect an entry (metric or event) | 192 * function to deselect an entry (metric or event) |
169 * @private | 193 * @private |
170 */ | 194 */ |
171 setupCheckboxes_: function(divId, optionMap, check, uncheck) { | 195 setupCheckboxes_: function(div, optionMap, check, uncheck) { |
172 var checkboxTemplate = $('#checkboxTemplate')[0]; | 196 var checkboxTemplate = $('#checkboxTemplate')[0]; |
173 var chooseMetricsDiv = $(divId)[0]; | |
174 | 197 |
175 for (var option in optionMap) { | 198 for (var option in optionMap) { |
176 var checkbox = checkboxTemplate.cloneNode(true); | 199 var checkbox = checkboxTemplate.cloneNode(true); |
177 checkbox.querySelector('span').innerText = 'Show ' + | 200 checkbox.querySelector('span').innerText = 'Show ' + |
178 optionMap[option].description; | 201 optionMap[option].description; |
179 chooseMetricsDiv.appendChild(checkbox); | 202 div.appendChild(checkbox); |
180 | 203 |
181 var input = checkbox.querySelector('input'); | 204 var input = checkbox.querySelector('input'); |
182 input.option = option; | 205 input.option = optionMap[option].id; |
183 input.addEventListener('change', function(e) { | 206 input.addEventListener('change', function(e) { |
184 if (e.target.checked) | 207 (e.target.checked ? check : uncheck).call(this, e.target.option); |
185 check.call(this, e.target.option); | |
186 else | |
187 uncheck.call(this, e.target.option); | |
188 }.bind(this)); | 208 }.bind(this)); |
189 } | 209 } |
190 }, | 210 }, |
191 | 211 |
192 /** | 212 /** |
193 * Set up just one chart in which all metrics will be displayed | 213 * Set up just one chart in which all metrics will be displayed |
194 * initially. But, the design readily accommodates addition of | 214 * initially. But, the design readily accommodates addition of |
195 * new charts, and movement of metrics into those other charts. | 215 * new charts, and movement of metrics into those other charts. |
196 * @private | 216 * @private |
197 */ | 217 */ |
198 setupMainChart_: function() { | 218 setupMainChart_: function() { |
199 this.chartParent = $('#charts')[0]; | 219 this.chartParent = $('#charts')[0]; |
200 this.charts = [document.createElement('div')]; | 220 this.charts = [document.createElement('div')]; |
201 this.charts[0].className = 'chart'; | 221 this.charts[0].className = 'chart'; |
202 this.chartParent.appendChild(this.charts[0]); | 222 this.chartParent.appendChild(this.charts[0]); |
203 }, | 223 }, |
204 | 224 |
205 /** | 225 /** |
206 * Set the time range for which to display metrics and events. For | 226 * Set the time range for which to display metrics and events. For |
207 * now, the time range always ends at "now", but future implementations | 227 * now, the time range always ends at "now", but future implementations |
208 * may allow time ranges not so anchored. | 228 * may allow time ranges not so anchored. |
209 * @param {!{start: !number, end: !number, resolution: !number}} range | 229 * @param {!{start: !number, end: !number, resolution: !number}} range |
210 */ | 230 */ |
211 setTimeRange: function(range) { | 231 setTimeRange: function(range) { |
212 this.range = range; | 232 this.range = range; |
213 this.end = Math.floor(Date.now() / range.resolution) * | 233 this.end = Math.floor(Date.now() / range.resolution) * |
214 range.resolution; | 234 range.resolution; |
215 | 235 |
216 // Take the GMT value of this.end ("now") and subtract from it the | |
217 // number of minutes by which we lag GMT in the present timezone, | |
218 // X mS/minute. This will show time in the present timezone. | |
219 this.end -= new Date().getTimezoneOffset() * 60000; | |
220 this.start = this.end - range.timeSpan; | 236 this.start = this.end - range.timeSpan; |
221 this.requestIntervals(); | 237 this.requestIntervals(); |
222 }, | 238 }, |
223 | 239 |
224 /** | 240 /** |
225 * Return mock interval set for testing. | |
226 * @return {!Array.<{start: !number, end: !number}>} intervals | |
227 */ | |
228 getMockIntervals: function() { | |
229 var interval = this.end - this.start; | |
230 | |
231 return [ | |
232 {start: this.start + interval * .1, | |
233 end: this.start + interval * .2}, | |
234 {start: this.start + interval * .7, | |
235 end: this.start + interval} | |
236 ]; | |
237 }, | |
238 | |
239 /** | |
240 * Request activity intervals in the current time range. | 241 * Request activity intervals in the current time range. |
241 */ | 242 */ |
242 requestIntervals: function() { | 243 requestIntervals: function() { |
243 this.onReceiveIntervals(this.getMockIntervals()); | 244 chrome.send('getActiveIntervals', [this.start, this.end]); |
244 // Replace with: chrome.send('getIntervals', this.start, this.end, | |
245 // this.onReceiveIntervals); | |
246 }, | 245 }, |
247 | 246 |
248 /** | 247 /** |
249 * Webui callback delivering response from requestIntervals call. Assumes | 248 * Webui callback delivering response from requestIntervals call. Assumes |
250 * this is a new time range choice, which results in complete refresh of | 249 * this is a new time range choice, which results in complete refresh of |
251 * all metrics and event types that are currently selected. | 250 * all metrics and event types that are currently selected. |
252 * @param {!Array.<{start: !number, end: !number}>} intervals new intervals | 251 * @param {!Array.<{start: !number, end: !number}>} intervals new intervals |
253 */ | 252 */ |
254 onReceiveIntervals: function(intervals) { | 253 getActiveIntervalsCallback: function(intervals) { |
255 this.intervals_ = intervals; | 254 this.intervals_ = intervals; |
256 | 255 |
257 for (var metric in this.metricMap_) { | 256 for (var metric in this.metricMap_) { |
258 var metricValue = this.metricMap_[metric]; | 257 var metricValue = this.metricMap_[metric]; |
259 if (metricValue.divs.length > 0) // if we're displaying this metric. | 258 if (metricValue.divs.length > 0) // if we're displaying this metric. |
260 this.refreshMetric(metric); | 259 this.refreshMetric(metric); |
261 } | 260 } |
262 | 261 |
263 for (var eventType in this.eventMap_) { | 262 for (var eventType in this.eventMap_) { |
264 var eventValue = this.eventMap_[eventType]; | 263 var eventValue = this.eventMap_[eventType]; |
265 if (eventValue.divs.length > 0) | 264 if (eventValue.divs.length > 0) |
266 this.refreshEventType(eventType); | 265 this.refreshEventType(eventValue.id); |
267 } | 266 } |
268 }, | 267 }, |
269 | 268 |
270 /** | 269 /** |
271 * Add a new metric to the main (currently only) chart. | 270 * Add a new metric to the main (currently only) chart. |
272 * @param {!string} metric Metric to start displaying | 271 * @param {!string} metric Metric to start displaying |
273 */ | 272 */ |
274 addMetric: function(metric) { | 273 addMetric: function(metric) { |
275 this.metricMap_[metric].divs.push(this.charts[0]); | 274 this.metricMap_[metric].divs.push(this.charts[0]); |
276 this.refreshMetric(metric); | 275 this.refreshMetric(metric); |
277 }, | 276 }, |
278 | 277 |
279 /** | 278 /** |
280 * Remove a metric from the chart(s). | 279 * Remove a metric from the chart(s). |
281 * @param {!string} metric Metric to stop displaying | 280 * @param {!string} metric Metric to stop displaying. |
282 */ | 281 */ |
283 dropMetric: function(metric) { | 282 dropMetric: function(metric) { |
284 var metricValue = this.metricMap_[metric]; | 283 var metricValue = this.metricMap_[metric]; |
285 var affectedCharts = metricValue.divs; | 284 var affectedCharts = metricValue.divs; |
286 metricValue.divs = []; | 285 metricValue.divs = []; |
287 | 286 |
288 affectedCharts.forEach(this.drawChart, this); | 287 affectedCharts.forEach(this.drawChart, this); |
289 }, | 288 }, |
290 | 289 |
291 /** | 290 /** |
292 * Return mock metric data points for testing. Give values ranging from | |
293 * offset to max-offset. (This lets us avoid direct overlap of | |
294 * different mock data sets in an ugly way that will die in the next | |
295 * version anyway.) | |
296 * @param {!number} max Max data value to return, less offset | |
297 * @param {!number} offset Adjustment factor | |
298 * @return {!Array.<{time: !number, value: !number}>} | |
299 */ | |
300 getMockDataPoints: function(max, offset) { | |
301 var dataPoints = []; | |
302 | |
303 for (var i = 0; i < this.intervals_.length; i++) { | |
304 // Rise from low offset to high max-offset in 100 point steps. | |
305 for (var time = this.intervals_[i].start; | |
306 time <= this.intervals_[i].end; time += this.range.resolution) { | |
307 var numPoints = time / this.range.resolution; | |
308 dataPoints.push({time: time, value: offset + (numPoints % 100) * | |
309 (max - 2 * offset) / 100}); | |
310 } | |
311 } | |
312 return dataPoints; | |
313 }, | |
314 | |
315 /** | |
316 * Request new metric data, assuming the metric table and chart already | 291 * Request new metric data, assuming the metric table and chart already |
317 * exist. | 292 * exist. |
318 * @param {!string} metric Metric for which to get data | 293 * @param {!string} metric Metric for which to get data. |
319 */ | 294 */ |
320 refreshMetric: function(metric) { | 295 refreshMetric: function(metric) { |
321 var metricValue = this.metricMap_[metric]; | 296 var metricValue = this.metricMap_[metric]; |
322 | 297 |
323 metricValue.data = null; // Mark metric as awaiting response. | 298 metricValue.data = null; // Mark metric as awaiting response. |
324 this.onReceiveMetric(metric, | 299 chrome.send('getMetric', [metricValue.id, |
325 this.getMockDataPoints(metricValue.yAxis.max, 5)); | 300 this.start, this.end, this.range.resolution]); |
326 // Replace with: | |
327 // chrome.send("getMetric", this.range.start, this.range.end, | |
328 // this.range.resolution, this.onReceiveMetric); | |
329 }, | 301 }, |
330 | 302 |
331 /** | 303 /** |
332 * Receive new datapoints for |metric|, convert the data to Flot-usable | 304 * Receive new datapoints for a metric, convert the data to Flot-usable |
333 * form, and redraw all affected charts. | 305 * form, and redraw all affected charts. |
334 * @param {!string} metric Metric to which |points| applies | 306 * @param {!{ |
335 * @param {!Array.<{time: !number, value: !number}>} points | 307 * type: !number, |
336 * new data points | 308 * points: !Array.<{time: !number, value: !number}> |
309 * }} result Object giving metric ID, and time/value point pairs for | |
310 * that id. | |
337 */ | 311 */ |
338 onReceiveMetric: function(metric, points) { | 312 getMetricCallback: function(result) { |
339 var metricValue = this.metricMap_[metric]; | 313 var metricValue = this.metricMap_[result.metricType]; |
340 | |
341 // Might have been dropped while waiting for data. | 314 // Might have been dropped while waiting for data. |
342 if (metricValue.divs.length == 0) | 315 if (metricValue.divs.length == 0) |
343 return; | 316 return; |
344 | 317 |
345 var series = []; | 318 var series = []; |
346 metricValue.data = [series]; | 319 metricValue.data = [series]; // Data ends with current open series. |
347 | 320 |
348 // Traverse the points, and the intervals, in parallel. Both are in | 321 // Traverse the points, and the intervals, in parallel. Both are in |
349 // ascending time order. Create a sequence of data "series" (per Flot) | 322 // ascending time order. Create a sequence of data "series" (per Flot) |
350 // arrays, with each series comprising all points within a given interval. | 323 // arrays, with each series comprising all points within a given interval. |
351 var interval = this.intervals_[0]; | 324 var interval = this.intervals_[0]; |
352 var intervalIndex = 0; | 325 var intervalIndex = 0; |
353 var pointIndex = 0; | 326 var pointIndex = 0; |
354 while (pointIndex < points.length && | 327 while (pointIndex < result.points.length && |
355 intervalIndex < this.intervals_.length) { | 328 intervalIndex < this.intervals_.length) { |
356 var point = points[pointIndex++]; | 329 var point = result.points[pointIndex++]; |
357 while (intervalIndex < this.intervals_.length && | 330 while (intervalIndex < this.intervals_.length && |
358 point.time > interval.end) { | 331 point.time > interval.end) { |
359 interval = this.intervals_[++intervalIndex]; // Jump to new interval. | 332 interval = this.intervals_[++intervalIndex]; // Jump to new interval. |
360 if (series.length > 0) { | 333 if (series.length > 0) { |
361 series = []; // Start a new series. | 334 series = []; // Start a new series. |
362 metricValue.data.push(series); // Put it on the end of the data. | 335 metricValue.data.push(series); // Put it on the end of the data. |
363 } | 336 } |
364 } | 337 } |
365 if (intervalIndex < this.intervals_.length && | 338 if (intervalIndex < this.intervals_.length && |
366 point.time > interval.start) | 339 point.time > interval.start) { |
367 series.push([point.time, point.value]); | 340 series.push([point.time - timezoneOffset, point.value]); |
341 } | |
368 } | 342 } |
369 | |
370 metricValue.divs.forEach(this.drawChart, this); | 343 metricValue.divs.forEach(this.drawChart, this); |
371 }, | 344 }, |
372 | 345 |
373 /** | 346 /** |
374 * Add a new event to the chart(s). | 347 * Add a new event to the chart(s). |
375 * @param {!string} eventType type of event to start displaying | 348 * @param {!string} eventType type of event to start displaying. |
376 */ | 349 */ |
377 addEventType: function(eventType) { | 350 addEventType: function(eventType) { |
378 // Events show on all charts. | 351 // Events show on all charts. |
379 this.eventMap_[eventType].divs = this.charts; | 352 this.eventMap_[eventType].divs = this.charts; |
380 this.refreshEventType(eventType); | 353 this.refreshEventType(eventType); |
381 }, | 354 }, |
382 | 355 |
383 /* | 356 /* |
384 * Remove an event from the chart(s). | 357 * Remove an event from the chart(s). |
385 * @param {!string} eventType type of event to stop displaying | 358 * @param {!string} eventType type of event to stop displaying |
386 */ | 359 */ |
387 dropEventType: function(eventType) { | 360 dropEventType: function(eventType) { |
388 var eventValue = this.eventMap_[eventType]; | 361 var eventValue = this.eventMap_[eventType]; |
389 var affectedCharts = eventValue.divs; | 362 var affectedCharts = eventValue.divs; |
390 eventValue.divs = []; | 363 eventValue.divs = []; |
391 | 364 |
392 affectedCharts.forEach(this.drawChart, this); | 365 affectedCharts.forEach(this.drawChart, this); |
393 }, | 366 }, |
394 | 367 |
395 /** | 368 /** |
396 * Return mock event points for testing. | |
397 * @param {!string} eventType type of event to generate mock data for | |
398 * @return {!Array.<{time: !number, longDescription: !string}>} | |
399 */ | |
400 getMockEventValues: function(eventType) { | |
401 var mockValues = []; | |
402 for (var i = 0; i < this.intervals_.length; i++) { | |
403 var interval = this.intervals_[i]; | |
404 | |
405 mockValues.push({ | |
406 time: interval.start, | |
407 longDescription: eventType + ' at ' + | |
408 new Date(interval.start) + ' blah, blah blah'}); | |
409 mockValues.push({ | |
410 time: (interval.start + interval.end) / 2, | |
411 longDescription: eventType + ' at ' + | |
412 new Date((interval.start + interval.end) / 2) + ' blah, blah'}); | |
413 mockValues.push({ | |
414 time: interval.end, | |
415 longDescription: eventType + ' at ' + new Date(interval.end) + | |
416 ' blah, blah blah'}); | |
417 } | |
418 return mockValues; | |
419 }, | |
420 | |
421 /** | |
422 * Request new data for |eventType|, for times in the current range. | 369 * Request new data for |eventType|, for times in the current range. |
423 * @param {!string} eventType type of event to get new data for | 370 * @param {!string} eventType type of event to get new data for |
424 */ | 371 */ |
425 refreshEventType: function(eventType) { | 372 refreshEventType: function(eventType) { |
426 // Mark eventType as awaiting response. | 373 // Mark eventType as awaiting response. |
427 this.eventMap_[eventType].data = null; | 374 this.eventMap_[eventType].data = null; |
428 this.onReceiveEventType(eventType, this.getMockEventValues(eventType)); | 375 |
429 // Replace with: | 376 chrome.send('getEvents', [eventType, this.start, this.end]); |
430 // chrome.send("getEvents", eventType, this.range.start, this.range.end); | |
431 }, | 377 }, |
432 | 378 |
433 /** | 379 /** |
434 * Receive new data for |eventType|. If the event has been deselected while | 380 * Receive new events for |eventType|. If |eventType| has been deselected |
435 * awaiting webui response, do nothing. Otherwise, save the data directly, | 381 * while awaiting webui response, do nothing. Otherwise, save the data |
436 * since events are handled differently than metrics when drawing | 382 * directly, since events are handled differently than metrics when drawing |
437 * (no "series"), and redraw all the affected charts. | 383 * (no "series"), and redraw all the affected charts. |
438 * @param {!string} eventType type of event the new data applies to | 384 * @param {!{ |
439 * @param {!Array.<{time: !number, longDescription: !string}>} values | 385 * type: !number, |
440 * new event values | 386 * points: !Array.<{time: !number, longDescription: string}> |
387 * }} result Object giving eventType ID, and time/description pairs for | |
388 * each event of that type in the range requested. | |
441 */ | 389 */ |
442 onReceiveEventType: function(eventType, values) { | 390 getEventsCallback: function(result) { |
443 var eventValue = this.eventMap_[eventType]; | 391 var eventValue = this.eventMap_[result.eventType]; |
444 | 392 |
445 if (eventValue.divs.length == 0) | 393 if (eventValue.divs.length == 0) |
446 return; | 394 return; |
447 | 395 |
448 eventValue.data = values; | 396 result.points.forEach(function(elememt) { |
397 element.time -= timezoneOffset; | |
398 }); | |
399 | |
400 eventValue.data = result.points; | |
449 eventValue.divs.forEach(this.drawChart, this); | 401 eventValue.divs.forEach(this.drawChart, this); |
450 }, | 402 }, |
451 | 403 |
452 /** | 404 /** |
453 * Return an object containing an array of metrics and another of events | 405 * Return an object containing an array of metrics and another of events |
454 * that include |chart| as one of the divs into which they display. | 406 * that include |chart| as one of the divs into which they display. |
455 * @param {HTMLDivElement} chart div for which to get relevant items | 407 * @param {HTMLDivElement} chart div for which to get relevant items |
456 * @return {!{metrics: !Array,<Object>, events: !Array.<Object>}} | 408 * @return {!{metrics: !Array,<Object>, events: !Array.<Object>}} |
457 * @private | 409 * @private |
458 */ | 410 */ |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
517 * }>} mark data structure for Flot to use | 469 * }>} mark data structure for Flot to use |
518 * @private | 470 * @private |
519 */ | 471 */ |
520 getEventMarks_: function(eventValues) { | 472 getEventMarks_: function(eventValues) { |
521 var markings = []; | 473 var markings = []; |
522 | 474 |
523 for (var i = 0; i < eventValues.length; i++) { | 475 for (var i = 0; i < eventValues.length; i++) { |
524 var eventValue = eventValues[i]; | 476 var eventValue = eventValues[i]; |
525 for (var d = 0; d < eventValue.data.length; d++) { | 477 for (var d = 0; d < eventValue.data.length; d++) { |
526 var point = eventValue.data[d]; | 478 var point = eventValue.data[d]; |
527 markings.push({ | 479 if (point.time >= this.start && point.time <= this.end) { |
528 color: eventValue.color, | 480 markings.push({ |
529 description: eventValue.description, | 481 color: eventValue.color, |
530 xaxis: {from: point.time, to: point.time} | 482 description: eventValue.description, |
531 }); | 483 xaxis: {from: point.time, to: point.time} |
484 }); | |
485 } else { | |
486 console.log('Event out of time range at: ' + point.time); | |
487 } | |
532 } | 488 } |
533 } | 489 } |
534 | 490 |
535 return markings; | 491 return markings; |
536 }, | 492 }, |
537 | 493 |
538 /** | 494 /** |
539 * Redraw the chart in div |chart|, *if* all its dependent data is present. | 495 * Redraw the chart in div |chart|, *if* all its dependent data is present. |
540 * Otherwise simply return, and await another call when all data is | 496 * Otherwise simply return, and await another call when all data is |
541 * available. | 497 * available. |
(...skipping 12 matching lines...) Expand all Loading... | |
554 for (var i = 0; i < value.data.length; i++) { | 510 for (var i = 0; i < value.data.length; i++) { |
555 seriesSeq.push({ | 511 seriesSeq.push({ |
556 color: value.yAxis.color, | 512 color: value.yAxis.color, |
557 data: value.data[i], | 513 data: value.data[i], |
558 label: i == 0 ? value.description + ' (' + value.units + ')' : null, | 514 label: i == 0 ? value.description + ' (' + value.units + ')' : null, |
559 yaxis: yAxes.length, // Use just-added Y axis. | 515 yaxis: yAxes.length, // Use just-added Y axis. |
560 }); | 516 }); |
561 } | 517 } |
562 }); | 518 }); |
563 | 519 |
520 // Ensure at least one y axis, as a reference for event placement. | |
521 if (yAxes.length == 0) | |
522 yAxes.push({max: 1.0}); | |
523 | |
564 var markings = this.getEventMarks_(chartData.events); | 524 var markings = this.getEventMarks_(chartData.events); |
565 var chart = this.charts[0]; | 525 var chart = this.charts[0]; |
566 var plot = $.plot(chart, seriesSeq, { | 526 var plot = $.plot(chart, seriesSeq, { |
567 yaxes: yAxes, | 527 yaxes: yAxes, |
568 xaxis: {mode: 'time'}, | 528 xaxis: { |
529 mode: 'time', | |
530 min: this.start - timezoneOffset, | |
531 max: this.end - timezoneOffset | |
532 }, | |
533 points: {show: true, radius: 1}, | |
534 lines: {show: true}, | |
569 grid: {markings: markings} | 535 grid: {markings: markings} |
570 }); | 536 }); |
571 | 537 |
572 // For each event in |markings|, create also a label div, with left | 538 // For each event in |markings|, create also a label div, with left |
573 // edge colinear with the event vertical-line. Top of label is | 539 // edge colinear with the event vertical line. Top of label is |
574 // presently a hack-in, putting labels in three tiers of 25px height | 540 // presently a hack-in, putting labels in three tiers of 25px height |
575 // each to avoid overlap. Will need something better. | 541 // each to avoid overlap. Will need something better. |
576 var labelTemplate = $('#labelTemplate')[0]; | 542 var labelTemplate = $('#labelTemplate')[0]; |
577 for (var i = 0; i < markings.length; i++) { | 543 for (var i = 0; i < markings.length; i++) { |
578 var mark = markings[i]; | 544 var mark = markings[i]; |
579 var point = | 545 var point = |
580 plot.pointOffset({x: mark.xaxis.to, y: yAxes[0].max, yaxis: 1}); | 546 plot.pointOffset({x: mark.xaxis.to, y: yAxes[0].max, yaxis: 1}); |
581 var labelDiv = labelTemplate.cloneNode(true); | 547 var labelDiv = labelTemplate.cloneNode(true); |
582 labelDiv.innerText = mark.description; | 548 labelDiv.innerText = mark.description; |
583 labelDiv.style.left = point.left + 'px'; | 549 labelDiv.style.left = point.left + 'px'; |
584 labelDiv.style.top = (point.top + 25 * (i % 3)) + 'px'; | 550 labelDiv.style.top = (point.top + 25 * (i % 3)) + 'px'; |
551 | |
585 chart.appendChild(labelDiv); | 552 chart.appendChild(labelDiv); |
586 } | 553 } |
587 } | 554 } |
588 }; | 555 }; |
589 return { | 556 return { |
590 PerformanceMonitor: PerformanceMonitor | 557 PerformanceMonitor: PerformanceMonitor |
591 }; | 558 }; |
592 }(); | 559 }); |
593 | 560 |
594 var performanceMonitor = new Installer.PerformanceMonitor(); | 561 var PerformanceMonitor = new performance_monitor.PerformanceMonitor(); |
OLD | NEW |