Chromium Code Reviews| 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 | |
| 6 cr.define('performance_monitor', function() { | 5 cr.define('performance_monitor', function() { |
| 7 'use strict'; | 6 'use strict'; |
| 8 | 7 |
| 9 /** | 8 /** |
| 10 * Enum for time ranges, giving a descriptive name, time span prior to |now|, | 9 * Enum for time ranges, giving for each a descriptive name, time span in ms |
| 11 * data point resolution, and time-label frequency and format for each. | 10 * prior to |now|, data point resolution in ms, time-label frequency in data |
| 11 * points per label, and format using Date.js standards, for each. | |
| 12 * | |
| 13 * The additional |element| field is added in setupTimeRangeTab_, giving | |
| 14 * the HTML input tag for the radio button selecting the given time range. | |
| 12 * @enum {{ | 15 * @enum {{ |
| 13 * value: number, | 16 * value: number, |
| 14 * name: string, | 17 * name: string, |
| 15 * timeSpan: number, | 18 * timeSpan: number, |
| 16 * resolution: number, | 19 * resolution: number, |
| 17 * labelEvery: number, | 20 * labelEvery: number, |
| 18 * format: string | 21 * format: string, |
| 22 * element: HTMLElement | |
| 19 * }} | 23 * }} |
| 20 * @private | 24 * @private |
| 21 */ | 25 */ |
| 22 var TimeRange_ = { | 26 var TimeRange_ = { |
| 23 // Prior 12 min, resolution of 1s, at most 720 points. | 27 // Prior 15 min, resolution of 5s, at most 180 points. |
| 24 // Labels at 60 point (1 min) intervals. | 28 // Labels at 12 point (1 min) intervals. |
| 25 minutes: {value: 0, name: 'Last 12 min', timeSpan: 720 * 1000, | 29 minutes: {value: 0, name: 'Last 15 min', timeSpan: 900 * 1000, |
| 26 resolution: 1000, labelEvery: 60, format: 'MM-dd'}, | 30 resolution: 1000 * 5, labelEvery: 12, format: 'MM-dd'}, |
| 27 | 31 |
| 28 // Prior hour, resolution of 5s, at most 720 points. | 32 // Prior hour, resolution of 20s, at most 180 points. |
| 29 // Labels at 60 point (5 min) intervals. | 33 // Labels at 15 point (5 min) intervals. |
| 30 hour: {value: 1, name: 'Last Hour', timeSpan: 3600 * 1000, | 34 hour: {value: 1, name: 'Last Hour', timeSpan: 3600 * 1000, |
| 31 resolution: 1000 * 5, labelEvery: 60, format: 'MM-dd'}, | 35 resolution: 1000 * 20, labelEvery: 15, format: 'MM-dd'}, |
| 32 | 36 |
| 33 // Prior day, resolution of 2 min, at most 720 points. | 37 // Prior day, resolution of 5 min, at most 288 points. |
| 34 // Labels at 90 point (3 hour) intervals. | 38 // Labels at 36 point (3 hour) intervals. |
| 35 day: {value: 2, name: 'Last Day', timeSpan: 24 * 3600 * 1000, | 39 day: {value: 2, name: 'Last Day', timeSpan: 24 * 3600 * 1000, |
| 36 resolution: 1000 * 60 * 2, labelEvery: 90, format: 'MM-dd'}, | 40 resolution: 1000 * 60 * 5, labelEvery: 36, format: 'MM-dd'}, |
| 37 | 41 |
| 38 // Prior week, resolution of 15 min -- at most 672 data points. | 42 // Prior week, resolution of 1 hr -- at most 168 data points. |
| 39 // Labels at 96 point (daily) intervals. | 43 // Labels at 24 point (daily) intervals. |
| 40 week: {value: 3, name: 'Last Week', timeSpan: 7 * 24 * 3600 * 1000, | 44 week: {value: 3, name: 'Last Week', timeSpan: 7 * 24 * 3600 * 1000, |
| 41 resolution: 1000 * 60 * 15, labelEvery: 96, format: 'M/d'}, | 45 resolution: 1000 * 3600, labelEvery: 24, format: 'M/d'}, |
| 42 | 46 |
| 43 // Prior month (30 days), resolution of 1 hr -- at most 720 data points. | 47 // Prior month (30 days), resolution of 4 hr -- at most 180 data points. |
| 44 // Labels at 168 point (weekly) intervals. | 48 // Labels at 42 point (weekly) intervals. |
| 45 month: {value: 4, name: 'Last Month', timeSpan: 30 * 24 * 3600 * 1000, | 49 month: {value: 4, name: 'Last Month', timeSpan: 30 * 24 * 3600 * 1000, |
| 46 resolution: 1000 * 3600, labelEvery: 168, format: 'M/d'}, | 50 resolution: 1000 * 3600 * 4, labelEvery: 42, format: 'M/d'}, |
| 47 | 51 |
| 48 // Prior quarter (90 days), resolution of 3 hr -- at most 720 data points. | 52 // Prior quarter (90 days), resolution of 12 hr -- at most 180 data points. |
| 49 // Labels at 112 point (fortnightly) intervals. | 53 // Labels at 28 point (fortnightly) intervals. |
| 50 quarter: {value: 5, name: 'Last Quarter', timeSpan: 90 * 24 * 3600 * 1000, | 54 quarter: {value: 5, name: 'Last Quarter', timeSpan: 90 * 24 * 3600 * 1000, |
| 51 resolution: 1000 * 3600 * 3, labelEvery: 112, format: 'M/yy'}, | 55 resolution: 1000 * 3600 * 12, labelEvery: 28, format: 'M/yy'}, |
| 52 }; | 56 }; |
| 53 | 57 |
| 54 /* | 58 /* |
| 55 * Offset, in ms, by which to subtract to convert GMT to local time | 59 * Table of colors to use for metrics and events. Basically boxing the |
| 56 * @type {number} | 60 * colorwheel, but leaving out yellows and fully saturated colors. |
| 57 */ | 61 * @type {Array.<string>} |
| 58 var timezoneOffset = new Date().getTimezoneOffset() * 60000; | 62 * @private |
| 63 */ | |
| 64 var ColorTable_ = [ | |
| 65 'rgb(255, 128, 128)', 'rgb(128, 255, 128)', 'rgb(128, 128, 255)', | |
| 66 'rgb(128, 255, 255)', 'rgb(255, 128, 255)', // No bright yellow | |
| 67 'rgb(255, 64, 64)', 'rgb( 64, 255, 64)', 'rgb( 64, 64, 255)', | |
| 68 'rgb( 64, 255, 255)', 'rgb(255, 64, 255)', // No medium yellow either | |
| 69 'rgb(128, 64, 64)', 'rgb( 64, 128, 64)', 'rgb( 64, 64, 128)', | |
| 70 'rgb( 64, 128, 128)', 'rgb(128, 64, 128)', 'rgb(128, 128, 64)' | |
| 71 ]; | |
| 72 | |
| 73 /* | |
| 74 * Offset, in ms, by which to subtract to convert GMT to local time. | |
| 75 * @type {number} | |
| 76 * @private | |
| 77 */ | |
| 78 var timezoneOffset_ = new Date().getTimezoneOffset() * 60000; | |
| 79 | |
| 80 /* | |
| 81 * Additional range multiplier to ensure that points don't hit the top of | |
| 82 * the graph. | |
| 83 * @type {number} | |
| 84 * @private | |
| 85 */ | |
| 86 var yAxisMargin_ = 1.05; | |
| 87 | |
| 88 /* | |
| 89 * Number of time resolution periods to wait between automated update of | |
| 90 * graphs. | |
| 91 * @type {number} | |
| 92 * @private | |
| 93 */ | |
| 94 var intervalMultiple_ = 2; | |
| 95 | |
| 96 /* | |
| 97 * Number of milliseconds to wait before deciding that the most recent | |
| 98 * resize event is not going to be followed immediately by another, and | |
| 99 * thus needs handling. | |
| 100 * @type {number} | |
| 101 * @private | |
| 102 */ | |
| 103 var resizeDelay_ = 500; | |
|
Jeffrey Yasskin
2012/08/31 22:28:16
The general guideline I've seen is that 200ms is a
clintstaley
2012/09/04 19:28:56
Right, but then if they pause the mouse for even 1
| |
| 59 | 104 |
| 60 /** @constructor */ | 105 /** @constructor */ |
| 61 function PerformanceMonitor() { | 106 function PerformanceMonitor() { |
| 62 this.__proto__ = PerformanceMonitor.prototype; | 107 this.__proto__ = PerformanceMonitor.prototype; |
| 63 /** | 108 |
| 64 * All metrics have entries, but those not displayed have an empty div list. | 109 /** |
| 65 * If a div list is not empty, the associated data will be non-null, or | 110 * Detailed information on a metric in the UI. MetricId is a unique |
| 66 * null but about to be filled by webui handler response. Thus, any metric | 111 * identifying number for the metric, provided by the webui, and assumed |
| 67 * with non-empty div list but null data is awaiting a data response | 112 * to be densely populated. All metrics also have a description and |
| 68 * from the webui handler. The webui handler uses numbers to uniquely | 113 * an associated category giving their unit information and home chart. |
| 69 * identify metric and event types, so we use the same numbers (in | 114 * They also have a color in which they are displayed, and a maximum value |
| 70 * string form) for the map key, repeating the id number in the |id| | 115 * by which to scale their y-axis. |
| 71 * field, as a number. | 116 * |
| 72 * @type {Object.<string, { | 117 * Although in the present UI each metric appears only in the home chart of |
| 73 * id: number, | 118 * its metric category, we keep the divs property to allow future |
| 74 * description: string, | 119 * modifications in which the same metric might appear in several charts |
| 75 * units: string, | 120 * for side-by-side comparisons. Metrics not being displayed have an |
| 76 * yAxis: !{max: number, color: string}, | 121 * empty div list. If a div list is not empty, the associated data will |
| 77 * divs: !Array.<HTMLDivElement>, | 122 * be non-null, or null but about to be filled by webui handler response. |
| 78 * data: ?Array.<{time: number, value: number}> | 123 * Thus, any metric with non-empty div list but null data is awaiting a |
| 79 * }>} | 124 * data response from the webui handler. |
| 80 * @private | 125 * @typedef {{ |
| 81 */ | 126 * metricId: number, |
| 82 this.metricMap_ = {}; | 127 * description: string, |
| 83 | 128 * category: !Object, |
| 84 /* | 129 * color: string, |
| 85 * Similar data for events, though no yAxis info is needed since events | 130 * maxValue: number, |
| 86 * are simply labelled markers at X locations. Rules regarding null data | 131 * divs: !Array.<HTMLDivElement>, |
| 87 * with non-empty div list apply here as for metricMap_ above. | 132 * data: ?Array.<{time: number, value: number}> |
| 88 * @type {Object.<number, { | 133 * }} |
| 89 * id: number, | 134 */ |
| 135 PerformanceMonitor.MetricDetails; | |
| 136 | |
| 137 /** | |
| 138 * Similar data for events as for metrics, though no yAxis info is needed | |
| 139 * since events are simply labelled markers at X locations. Rules regarding | |
| 140 * null data with non-empty div list apply here as for metricDetailsMap_ | |
| 141 * above. The |data| field follows a special rule not describable in | |
| 142 * JSDoc: Each array element may include arbitrary other properties with | |
| 143 * value {label: 'some label', value: 'some value'} for display in the | |
| 144 * event mouseover. These will vary per event type. | |
|
Jeffrey Yasskin
2012/08/31 22:28:16
I guess what I'm wondering is how JS code uses the
clintstaley
2012/09/04 19:28:56
The *value* has predictable keys of "label" and "v
| |
| 145 * @typedef {{ | |
| 146 * eventId: number, | |
| 147 * name: string, | |
| 148 * popupTitle: string, | |
| 90 * description: string, | 149 * description: string, |
| 91 * color: string, | 150 * color: string, |
| 92 * divs: !Array.<HTMLDivElement>, | 151 * divs: !Array.<HTMLDivElement>, |
| 93 * data: ?Array.<{time: number, longDescription: string}> | 152 * data: ?Array.<{time: number}> |
| 153 * }} | |
| 154 */ | |
| 155 PerformanceMonitor.EventDetails; | |
| 156 | |
| 157 /** | |
| 158 * Metrics fall into categories that have common units and thus may | |
| 159 * share a common graph, or share y-axes within a multi-y-axis graph. | |
| 160 * Each category has one home chart in which metrics of that category | |
| 161 * are displayed. Currently this is also the only chart in which such | |
| 162 * metrics are displayed, but the design permits a metric to show in | |
| 163 * several charts if this is useful later on. | |
| 164 * @type {Object.<string, { | |
| 165 * metricCategoryId: number, | |
| 166 * name: string, | |
| 167 * description: string, | |
| 168 * unit: string, | |
| 169 * details: Array.<{!PerformanceMonitor.MetricDetails}>, | |
| 170 * homeChart: !HTMLDivElement | |
| 94 * }>} | 171 * }>} |
| 95 * @private | 172 * @private |
| 96 */ | 173 */ |
| 97 this.eventMap_ = {}; | 174 this.metricCategoryMap_ = {}; |
| 175 | |
| 176 /** | |
| 177 * Comprehensive map from metricId to MetricDetails. | |
| 178 * @type {Object.<number, {PerformanceMonitor.MetricDetails}>} | |
| 179 * @private | |
| 180 */ | |
| 181 this.metricDetailsMap_ = {}; | |
| 182 | |
| 183 /** | |
| 184 * Events fall into categories just like metrics, above. This category | |
| 185 * grouping is not as important as that for metrics, since events | |
| 186 * needn't share maxima, y-axes, nor units, and since events appear on | |
| 187 * all charts. But grouping of event categories in the event-selection | |
| 188 * UI is still useful. | |
| 189 * @type {Object.<string, { | |
| 190 * eventCategoryId: number, | |
| 191 * name: string, | |
| 192 * description: string, | |
| 193 * details: !Array.<!PerformanceMonitor.EventDetails>, | |
| 194 * }>} | |
| 195 * @private | |
| 196 */ | |
| 197 this.eventCategoryMap_ = {}; | |
| 198 | |
| 199 /** | |
| 200 * Comprehensive map from eventId to EventDetails. | |
| 201 * @type {Object.<number, {PerformanceMonitor.EventDetails}>} | |
| 202 * @private | |
| 203 */ | |
| 204 this.eventDetailsMap_ = {}; | |
| 98 | 205 |
| 99 /** | 206 /** |
| 100 * Time periods in which the browser was active and collecting metrics | 207 * Time periods in which the browser was active and collecting metrics |
| 101 * and events. | 208 * and events. |
| 102 * @type {!Array.<{start: number, end: number}>} | 209 * @type {!Array.<{start: number, end: number}>} |
| 103 * @private | 210 * @private |
| 104 */ | 211 */ |
| 105 this.intervals_ = []; | 212 this.intervals_ = []; |
| 106 | 213 |
| 107 this.setupTimeRangeChooser_(); | 214 /** |
| 108 chrome.send('getAllEventTypes'); | 215 * Handle of timer interval function used to update charts |
| 109 chrome.send('getAllMetricTypes'); | 216 * @type {Object} |
| 110 this.setupMainChart_(); | 217 * @private |
| 218 */ | |
| 219 this.updateTimer_ = null; | |
| 220 | |
| 221 /** | |
| 222 * Handle of timer interval function used to check for resizes. Nonnull | |
| 223 * only when resize events are coming steadily. | |
| 224 * @type {Object} | |
| 225 * @private | |
| 226 */ | |
| 227 this.resizeTimer_ = null; | |
| 228 | |
| 229 /** | |
| 230 * Time of last UNHANDLED resize event, or null if no resizes or if | |
| 231 * last one has been handled. | |
| 232 * @type {number} | |
| 233 * @private | |
| 234 */ | |
| 235 this.lastResizeTime_ = null; | |
| 236 | |
| 237 /** | |
| 238 * All chart divs in the display, whether hidden or not. Presently | |
| 239 * this has one entry for each metric category in |this.metricCategoryMap|. | |
| 240 * @type {Array.<HTMLDivElement>} | |
| 241 * @private | |
| 242 */ | |
| 243 this.charts_ = []; | |
| 244 | |
| 245 this.setupTimeRangeTab_(); | |
| 246 chrome.send('getEventTypes'); | |
| 247 chrome.send('getMetricTypes'); | |
| 111 TimeRange_.day.element.click(); | 248 TimeRange_.day.element.click(); |
| 112 } | 249 } |
| 113 | 250 |
| 114 PerformanceMonitor.prototype = { | 251 PerformanceMonitor.prototype = { |
| 115 /** | 252 /** |
| 116 * Receive a list of all metrics, and populate |this.metricMap_| to | 253 * Receive a list of all metric categories, each with its correspoinding |
| 117 * reflect said list. Reconfigure the checkbox set for metric selection. | 254 * list of metric details. Populate |this.metricCategoryMap_| and |
| 118 * @param {Array.<{metricType: string, shortDescription: string}>} | 255 * |this.metrictDetailsMap_| to reflect said list. Reconfigure the |
| 119 * allMetrics All metrics from which to select. | 256 * checkbox set for metric selection. |
| 257 * @param {Array.<{ | |
| 258 * metricCategoryId: number, | |
| 259 * name: string, | |
| 260 * unit: string, | |
| 261 * description: string, | |
| 262 * details: Array.<{ | |
| 263 * metricId: number, | |
| 264 * name: string, | |
| 265 * description: string | |
| 266 * }> | |
| 267 * }>} categories All metric categories needing charts and checkboxes. | |
| 120 */ | 268 */ |
| 121 getAllMetricTypesCallback: function(allMetrics) { | 269 getMetricTypesCallback: function(categories) { |
| 122 for (var i = 0; i < allMetrics.length; i++) { | 270 categories.forEach(function(category) { |
| 123 var metric = allMetrics[i]; | 271 this.addCategoryChart_(category); |
| 272 this.metricCategoryMap_[category.metricCategoryId] = category; | |
| 124 | 273 |
| 125 this.metricMap_[metric.metricType] = { | 274 category.details.forEach(function(metric) { |
| 126 id: metric.metricType, | 275 metric.color = ColorTable_[metric.metricId % ColorTable_.length]; |
| 127 description: metric.shortDescription, | 276 metric.maxValue = 1; |
| 128 units: metric.units, | 277 metric.divs = []; |
| 129 yAxis: {min: 0, max: metric.maxValue, | 278 metric.data = null; |
| 130 color: jQuery.color.parse('blue')}, | 279 metric.category = category; |
| 131 divs: [], | 280 this.metricDetailsMap_[metric.metricId] = metric; |
| 132 data: null | 281 }, this); |
| 133 }; | 282 }, this); |
| 134 } | |
| 135 | 283 |
| 136 this.setupCheckboxes_($('#choose-metrics')[0], | 284 this.setupCheckboxes_($('#choose-metrics')[0], |
| 137 this.metricMap_, this.addMetric, this.dropMetric); | 285 this.metricCategoryMap_, 'metricId', this.addMetric, this.dropMetric); |
| 138 }, | 286 }, |
| 139 | 287 |
| 140 /** | 288 /** |
| 141 * Receive a list of all event types, and populate |this.eventMap_| to | 289 * Receive a list of all event categories, each with its correspoinding |
| 142 * reflect said list. Reconfigure the checkbox set for event selection. | 290 * list of event details. Populate |this.eventCategoryMap_| and |
| 143 * @param {Array.<{eventType: string, shortDescription: string}>} | 291 * |this.eventDetailsMap| to reflect said list. Reconfigure the |
| 144 * allEvents All events from which to select. | 292 * checkbox set for event selection. |
| 293 * @param {Array.<{ | |
| 294 * eventCategoryId: number, | |
| 295 * name: string, | |
| 296 * description: string, | |
| 297 * details: Array.<{ | |
| 298 * eventId: number, | |
| 299 * name: string, | |
| 300 * description: string | |
| 301 * }> | |
| 302 * }>} categories All event categories needing charts checkboxes. | |
| 145 */ | 303 */ |
| 146 getAllEventTypesCallback: function(allEvents) { | 304 getEventTypesCallback: function(categories) { |
| 147 for (var i = 0; i < allEvents.length; i++) { | 305 categories.forEach(function(category) { |
| 148 var eventInfo = allEvents[i]; | 306 this.eventCategoryMap_[category.eventCategoryId] = category; |
| 149 | 307 |
| 150 this.eventMap_[eventInfo.eventType] = { | 308 category.details.forEach(function(event) { |
| 151 id: eventInfo.eventType, | 309 event.color = ColorTable_[event.eventId % ColorTable_.length]; |
| 152 color: jQuery.color.parse('red'), | 310 event.divs = []; |
| 153 data: null, | 311 event.data = null; |
| 154 description: eventInfo.shortDescription, | 312 this.eventDetailsMap_[event.eventId] = event; |
| 155 divs: [] | 313 }, this); |
| 156 }; | 314 }, this); |
| 157 } | |
| 158 | 315 |
| 159 this.setupCheckboxes_($('#choose-events')[0], | 316 this.setupCheckboxes_($('#choose-events')[0], this.eventCategoryMap_, |
| 160 this.eventMap_, this.addEventType, this.dropEventType); | 317 'eventId', this.addEventType, this.dropEventType); |
| 161 }, | 318 }, |
| 162 | 319 |
| 163 /** | 320 /** |
| 164 * Set up the radio button set to choose time range. Use div#radio-template | 321 * Set up the radio button set to choose time range. Use div#radio-template |
| 165 * as a template. | 322 * as a template. |
| 166 * @private | 323 * @private |
| 167 */ | 324 */ |
| 168 setupTimeRangeChooser_: function() { | 325 setupTimeRangeTab_: function() { |
| 169 var timeDiv = $('#choose-time-range')[0]; | 326 var timeDiv = $('#choose-time-range')[0]; |
| 327 var backButton = $('#back-time')[0]; | |
| 328 var forwardButton = $('#forward-time')[0]; | |
| 170 var radioTemplate = $('#radio-template')[0]; | 329 var radioTemplate = $('#radio-template')[0]; |
| 171 | 330 |
| 172 for (var time in TimeRange_) { | 331 for (var time in TimeRange_) { |
| 173 var timeRange = TimeRange_[time]; | 332 var timeRange = TimeRange_[time]; |
| 174 var radio = radioTemplate.cloneNode(true); | 333 var radio = radioTemplate.cloneNode(true); |
| 175 var input = radio.querySelector('input'); | 334 var input = radio.querySelector('input'); |
| 176 | 335 |
| 177 input.value = timeRange.value; | 336 input.value = timeRange.value; |
| 178 input.timeRange = timeRange; | 337 input.timeRange = timeRange; |
| 179 radio.querySelector('span').innerText = timeRange.name; | 338 radio.querySelector('span').innerText = timeRange.name; |
| 180 timeDiv.appendChild(radio); | 339 timeDiv.appendChild(radio); |
| 181 timeRange.element = input; | 340 timeRange.element = input; |
| 182 } | 341 } |
| 183 | 342 |
| 184 timeDiv.addEventListener('click', function(e) { | 343 timeDiv.addEventListener('click', function(e) { |
| 185 if (!e.target.webkitMatchesSelector('input[type="radio"]')) | 344 if (!e.target.webkitMatchesSelector('input[type="radio"]')) |
| 186 return; | 345 return; |
| 187 | 346 |
| 188 this.setTimeRange(e.target.timeRange); | 347 this.setTimeRange(e.target.timeRange, Date.now(), true); |
| 189 }.bind(this)); | 348 }.bind(this)); |
| 349 | |
| 350 forwardButton.addEventListener('click', this.forwardTime.bind(this)); | |
| 351 backButton.addEventListener('click', this.backTime.bind(this)); | |
| 190 }, | 352 }, |
| 191 | 353 |
| 192 /** | 354 /** |
| 193 * Generalized function for setting up checkbox blocks for either events | 355 * Generalized function for setting up checkbox blocks for either events |
| 194 * or metrics. Take a div |div| into which to place the checkboxes, | 356 * or metrics. Take a div |div| into which to place the checkboxes, |
| 195 * and a map |optionMap| with values that each include a property | 357 * and a map |optionCategoryMap| with values that each include a property |
| 196 * |description|. Set up one checkbox for each entry in |optionMap| | 358 * |description|, and a property |details|, which gives an array of objects |
| 197 * labelled with that description. Arrange callbacks to function |check| | 359 * that in turn each have an id property with key given by parameter |
| 198 * or |uncheck|, passing them the key of the checked or unchecked option, | 360 * idKey, and a property |name|. |
| 199 * when the relevant checkbox state changes. | 361 * |
| 362 * For instance, |optionCategoryMap| might be metricCategoryMap_, and | |
|
Jeffrey Yasskin
2012/08/31 22:28:16
I meant something more like:
For example: setupCh
clintstaley
2012/09/04 19:28:56
OK. Was hoping a clear description would suffice :
| |
| 363 * |idKey| would be 'metricID'. MetricCategoryMap_'s values each | |
| 364 * have a description and details property. The details property in | |
| 365 * this case would be an array of MetricDetails, each having properties | |
| 366 * metricId and name. | |
| 367 * | |
| 368 * Set up a checkbox group for each entry in |optionCategoryMap|, which | |
| 369 * group has one checkbox for each entry in the |details| array. | |
| 370 * Arrange callbacks to function |check| or |uncheck|, passing them | |
| 371 * the id of the checked or unchecked option, when the relevant checkbox | |
| 372 * state changes. | |
| 373 * | |
| 374 * For the example above, we'd thus have a checkbox group for each | |
| 375 * metric category labeled by its description, with one checkbox | |
| 376 * underneath it for each metric, labelled by the metric's name. Methods | |
| 377 * addMetric and dropMetric would be the |check| and |uncheck| handlers. | |
| 378 * | |
| 200 * @param {!HTMLDivElement} div A <div> into which to put checkboxes. | 379 * @param {!HTMLDivElement} div A <div> into which to put checkboxes. |
| 201 * @param {!Object} optionMap A map of metric/event entries. | 380 * @param {!Object} optionCategory Map A map of metric/event categories. |
| 381 * @param {string} idKey The key of the id property. | |
| 202 * @param {!function(this:Controller, Object)} check | 382 * @param {!function(this:Controller, Object)} check |
| 203 * The function to select an entry (metric or event). | 383 * The function to select an entry (metric or event). |
| 204 * @param {!function(this:Controller, Object)} uncheck | 384 * @param {!function(this:Controller, Object)} uncheck |
| 205 * The function to deselect an entry (metric or event). | 385 * The function to deselect an entry (metric or event). |
| 206 * @private | 386 * @private |
| 207 */ | 387 */ |
| 208 setupCheckboxes_: function(div, optionMap, check, uncheck) { | 388 setupCheckboxes_: function(div, optionCategoryMap, idKey, check, uncheck) { |
| 209 var checkboxTemplate = $('#checkbox-template')[0]; | 389 var checkboxTemplate = $('#detail-checkbox-template')[0]; |
| 390 var labelTemplate = $('#category-label-template')[0]; | |
| 210 | 391 |
| 211 for (var option in optionMap) { | 392 for (var c in optionCategoryMap) { |
| 212 var checkbox = checkboxTemplate.cloneNode(true); | 393 var category = optionCategoryMap[c]; |
| 213 checkbox.querySelector('span').innerText = 'Show ' + | 394 var label = labelTemplate.cloneNode(true); |
| 214 optionMap[option].description; | |
| 215 div.appendChild(checkbox); | |
| 216 | 395 |
| 217 var input = checkbox.querySelector('input'); | 396 label.innerText = category.name; |
| 218 input.option = optionMap[option].id; | 397 div.appendChild(label); |
| 219 input.addEventListener('change', function(e) { | 398 |
| 220 (e.target.checked ? check : uncheck).call(this, e.target.option); | 399 category.details.forEach(function(details) { |
| 221 }.bind(this)); | 400 var checkbox = checkboxTemplate.cloneNode(true); |
| 401 var input = checkbox.querySelector('input'); | |
| 402 var label = checkbox.querySelector('.detail-label'); | |
| 403 var icon = checkbox.querySelector('.color-icon'); | |
| 404 | |
| 405 input.option = details[idKey]; | |
| 406 input.icon = icon; | |
| 407 input.addEventListener('change', function(e) { | |
| 408 (e.target.checked ? check : uncheck).call(this, e.target.option); | |
| 409 e.target.icon.style.visibility = | |
| 410 e.target.checked ? 'visible' : 'hidden'; | |
| 411 }.bind(this)); | |
| 412 | |
| 413 label.innerText = details.name; | |
| 414 | |
| 415 icon.style.backgroundColor = details.color; | |
| 416 | |
| 417 div.appendChild(checkbox); | |
| 418 }, this); | |
| 222 } | 419 } |
| 223 }, | 420 }, |
| 224 | 421 |
| 225 /** | 422 /** |
| 226 * Set up just one chart in which all metrics will be displayed | 423 * Add a new chart for |category|, making it initially hidden, |
| 227 * initially. But, the design readily accommodates addition of | 424 * with no metrics displayed in it. |
| 228 * new charts, and movement of metrics into those other charts. | 425 * @param {!Object} category The metric category for which to create |
| 426 * the chart. Category is a value from metricCategoryMap_. | |
| 229 * @private | 427 * @private |
| 230 */ | 428 */ |
| 231 setupMainChart_: function() { | 429 addCategoryChart_: function(category) { |
| 232 this.chartParent = $('#charts')[0]; | 430 var chartParent = $('#charts')[0]; |
| 233 this.charts = [document.createElement('div')]; | 431 var chart = document.createElement('div'); |
| 234 this.charts[0].className = 'chart'; | 432 |
| 235 this.chartParent.appendChild(this.charts[0]); | 433 chart.className = 'chart'; |
| 434 chart.hidden = true; | |
| 435 chartParent.appendChild(chart); | |
| 436 this.charts_.push(chart); | |
| 437 category.homeChart = chart; | |
| 438 chart.refs = 0; | |
| 439 chart.hovers = []; | |
| 440 | |
| 441 // Receive hover events from Flot. | |
| 442 // Attached to chart will be properties 'hovers', a list of {x, div} | |
| 443 // pairs. As pos events arrive, check each hover to see if it should | |
| 444 // be hidden or made visible. | |
| 445 $(chart).bind('plothover', function(event, pos, item) { | |
| 446 var tolerance = this.range.resolution; | |
| 447 | |
| 448 chart.hovers.forEach(function(hover) { | |
| 449 hover.div.hidden = hover.x < pos.x - tolerance || | |
| 450 hover.x > pos.x + tolerance; | |
| 451 }); | |
| 452 | |
| 453 }.bind(this)); | |
| 454 | |
| 455 $(window).resize(function() { | |
| 456 this.lastResizeTime_ = Date.now(); | |
| 457 if (this.resizeTimer_ == null) | |
| 458 this.resizeTimer = setInterval(this.checkResize_.bind(this), 100); | |
|
Jeffrey Yasskin
2012/08/31 22:28:16
I think what I would do here is use setTimeout() t
clintstaley
2012/09/04 19:28:56
Wow. I tested it, too. Dunno what happened. But
| |
| 459 }.bind(this)); | |
| 460 }, | |
| 461 | |
| 462 /** | |
| 463 * Check to see if resize events have recently occured, and if a long | |
| 464 * enough time has passed since the last one to justify actually redrawing | |
| 465 * all the charts. If so, redraw them all, and clear the resize state. | |
| 466 * @private | |
| 467 */ | |
| 468 checkResize_: function() { | |
| 469 if (this.lastResizeTime_ != null && Date.now() - this.lastResizeTime_ > | |
| 470 resizeDelay_) { | |
| 471 this.lastResizeTime_ = null; | |
| 472 | |
| 473 // Timer's done its job. Turn it off till more resize events arrive. | |
| 474 clearInterval(this.resizeTimer_); | |
| 475 this.resizeTimer_ = null; | |
| 476 | |
| 477 this.charts_.forEach(function(chart) {this.drawChart(chart);}, this); | |
| 478 } | |
| 236 }, | 479 }, |
| 237 | 480 |
| 238 /** | 481 /** |
| 239 * Set the time range for which to display metrics and events. For | 482 * Set the time range for which to display metrics and events. For |
| 240 * now, the time range always ends at "now", but future implementations | 483 * now, the time range always ends at 'now', but future implementations |
| 241 * may allow time ranges not so anchored. | 484 * may allow time ranges not so anchored. |
| 242 * @param {!{start: number, end: number, resolution: number}} range | 485 * @param {!{start: number, end: number, resolution: number}} range |
| 243 * The time range for which to get display data. | 486 * The time range for which to get display data. |
| 487 * @param {number} end Ending time, in ms since epoch, to which to | |
| 488 * set the new time range. | |
| 489 * @param {boolean} startTimer Indicates whether we should restart the | |
| 490 * range-update timer. | |
| 244 */ | 491 */ |
| 245 setTimeRange: function(range) { | 492 setTimeRange: function(range, end, startTimer) { |
| 246 this.range = range; | 493 this.range = range; |
| 247 this.end = Math.floor(Date.now() / range.resolution) * | 494 if (this.updateTimer_ != null) |
| 495 clearInterval(this.updateTimer_); | |
| 496 if (startTimer) | |
| 497 this.updateTimer_ = setInterval(this.forwardTime.bind(this), | |
| 498 intervalMultiple_ * range.resolution); | |
| 499 this.end = Math.floor(end / range.resolution) * | |
| 248 range.resolution; | 500 range.resolution; |
| 249 | 501 |
| 250 this.start = this.end - range.timeSpan; | 502 this.start = this.end - range.timeSpan; |
| 251 this.requestIntervals(); | 503 this.requestIntervals(); |
| 252 }, | 504 }, |
| 253 | 505 |
| 254 /** | 506 /** |
| 507 * Back up the time range by 1/2 of its current span, and cause chart | |
| 508 * redraws. | |
| 509 */ | |
| 510 backTime: function() { | |
| 511 this.setTimeRange(this.range, this.end - this.range.timeSpan / 2, false); | |
| 512 }, | |
| 513 | |
| 514 /** | |
| 515 * Advance the time range by 1/2 of its current span, or up to the point | |
| 516 * where it ends at the present time, whichever is less. | |
| 517 */ | |
| 518 forwardTime: function() { | |
| 519 var now = Date.now(); | |
| 520 var newEnd = Math.min(now, this.end + this.range.timeSpan / 2); | |
| 521 | |
| 522 this.setTimeRange(this.range, newEnd, newEnd == now); | |
| 523 }, | |
| 524 | |
| 525 /** | |
| 255 * Request activity intervals in the current time range. | 526 * Request activity intervals in the current time range. |
| 256 */ | 527 */ |
| 257 requestIntervals: function() { | 528 requestIntervals: function() { |
| 258 chrome.send('getActiveIntervals', [this.start, this.end]); | 529 chrome.send('getActiveIntervals', [this.start, this.end]); |
| 259 }, | 530 }, |
| 260 | 531 |
| 261 /** | 532 /** |
| 262 * Webui callback delivering response from requestIntervals call. Assumes | 533 * Webui callback delivering response from requestIntervals call. Assumes |
| 263 * this is a new time range choice, which results in complete refresh of | 534 * this is a new time range choice, which results in complete refresh of |
| 264 * all metrics and event types that are currently selected. | 535 * all metrics and event types that are currently selected. |
| 265 * @param {!Array.<{start: number, end: number}>} intervals | 536 * @param {!Array.<{start: number, end: number}>} intervals |
| 266 * The new intervals. | 537 * The new intervals. |
| 267 */ | 538 */ |
| 268 getActiveIntervalsCallback: function(intervals) { | 539 getActiveIntervalsCallback: function(intervals) { |
| 269 this.intervals_ = intervals; | 540 this.intervals_ = intervals; |
| 270 | 541 |
| 271 for (var metric in this.metricMap_) { | 542 for (var metric in this.metricDetailsMap_) { |
| 272 var metricValue = this.metricMap_[metric]; | 543 var metricDetails = this.metricDetailsMap_[metric]; |
| 273 if (metricValue.divs.length > 0) // if we're displaying this metric. | 544 if (metricDetails.divs.length > 0) // if we're displaying this metric. |
| 274 this.refreshMetric(metric); | 545 this.refreshMetric(metricDetails); |
| 275 } | 546 } |
| 276 | 547 |
| 277 for (var eventType in this.eventMap_) { | 548 for (var eventType in this.eventDetailsMap_) { |
| 278 var eventValue = this.eventMap_[eventType]; | 549 var eventValue = this.eventDetailsMap_[eventType]; |
| 279 if (eventValue.divs.length > 0) | 550 if (eventValue.divs.length > 0) |
| 280 this.refreshEventType(eventValue.id); | 551 this.refreshEventType(eventValue.eventId); |
| 281 } | 552 } |
| 282 }, | 553 }, |
| 283 | 554 |
| 284 /** | 555 /** |
| 285 * Add a new metric to the main (currently only) chart. | 556 * Add a new metric to the display, showing it on its category's home |
| 286 * @param {string} metric The metric to start displaying. | 557 * chart. Un-hide the home chart if needed. |
| 558 * @param {number} metricId The id of the metric to start displaying. | |
| 287 */ | 559 */ |
| 288 addMetric: function(metric) { | 560 addMetric: function(metricId) { |
| 289 this.metricMap_[metric].divs.push(this.charts[0]); | 561 var details = this.metricDetailsMap_[metricId]; |
| 290 this.refreshMetric(metric); | 562 var homeChart = details.category.homeChart; |
| 563 | |
| 564 details.divs.push(homeChart); | |
| 565 if (homeChart.refs == 0) | |
| 566 homeChart.hidden = false; | |
| 567 homeChart.refs++; | |
| 568 | |
| 569 this.refreshMetric(details); | |
| 291 }, | 570 }, |
| 292 | 571 |
| 293 /** | 572 /** |
| 294 * Remove a metric from the chart(s). | 573 * Remove a metric from the chart(s). |
| 295 * @param {string} metric The metric to stop displaying. | 574 * @param {string} metric The metric to stop displaying. |
| 296 */ | 575 */ |
| 297 dropMetric: function(metric) { | 576 dropMetric: function(metric) { |
| 298 var metricValue = this.metricMap_[metric]; | 577 var details = this.metricDetailsMap_[metric]; |
| 299 var affectedCharts = metricValue.divs; | 578 var affectedCharts = details.divs; |
| 300 metricValue.divs = []; | |
| 301 | 579 |
| 302 affectedCharts.forEach(this.drawChart, this); | 580 details.divs = []; // Gotta do this now to get correct drawChart result. |
| 581 affectedCharts.forEach(function(chart) { | |
| 582 chart.refs--; | |
| 583 if (chart.refs == 0) | |
| 584 chart.hidden = true; | |
| 585 this.drawChart(chart); | |
| 586 }, this); | |
| 587 | |
| 303 }, | 588 }, |
| 304 | 589 |
| 305 /** | 590 /** |
| 306 * Request new metric data, assuming the metric table and chart already | 591 * Request new metric data, assuming the metric table and chart already |
| 307 * exist. | 592 * exist. |
| 308 * @param {string} metric The metric for which to get data. | 593 * @param {string} metricDetails The metric details for which to get data. |
| 309 */ | 594 */ |
| 310 refreshMetric: function(metric) { | 595 refreshMetric: function(metricDetails) { |
| 311 var metricValue = this.metricMap_[metric]; | 596 metricDetails.data = null; // Mark metric as awaiting response. |
| 312 | 597 chrome.send('getMetric', [metricDetails.metricId, |
| 313 metricValue.data = null; // Mark metric as awaiting response. | |
| 314 chrome.send('getMetric', [metricValue.id, | |
| 315 this.start, this.end, this.range.resolution]); | 598 this.start, this.end, this.range.resolution]); |
| 316 }, | 599 }, |
| 317 | 600 |
| 318 /** | 601 /** |
| 319 * Receive new datapoints for a metric, convert the data to Flot-usable | 602 * Receive new datapoints for a metric, convert the data to Flot-usable |
| 320 * form, and redraw all affected charts. | 603 * form, and redraw all affected charts. |
| 321 * @param {!{ | 604 * @param {!{ |
| 322 * type: number, | 605 * id: number, |
| 323 * points: !Array.<{time: number, value: number}> | 606 * max: number, |
| 324 * }} result An object giving metric ID, and time/value point pairs for | 607 * metrics: !Array.<{time: number, value: number}> |
| 325 * that id. | 608 * }} result An object giving metric ID, max expected value of the data, |
| 609 * and time/value point pairs for that id. | |
| 326 */ | 610 */ |
| 327 getMetricCallback: function(result) { | 611 getMetricCallback: function(result) { |
| 328 var metricValue = this.metricMap_[result.metricType]; | 612 var metricDetails = this.metricDetailsMap_[result.metricId]; |
| 329 // Might have been dropped while waiting for data. | 613 // Might have been dropped while waiting for data. |
| 330 if (metricValue.divs.length == 0) | 614 if (metricDetails.divs.length == 0) |
| 331 return; | 615 return; |
| 332 | 616 |
| 333 var series = []; | 617 var series = []; |
| 334 metricValue.data = [series]; // Data ends with current open series. | 618 metricDetails.data = [series]; // Data ends with current open series. |
| 335 | 619 |
| 336 // Traverse the points, and the intervals, in parallel. Both are in | 620 // Traverse the points, and the intervals, in parallel. Both are in |
| 337 // ascending time order. Create a sequence of data "series" (per Flot) | 621 // ascending time order. Create a sequence of data 'series' (per Flot) |
| 338 // arrays, with each series comprising all points within a given interval. | 622 // arrays, with each series comprising all points within a given interval. |
| 339 var interval = this.intervals_[0]; | 623 var interval = this.intervals_[0]; |
| 340 var intervalIndex = 0; | 624 var intervalIndex = 0; |
| 341 var pointIndex = 0; | 625 var pointIndex = 0; |
| 342 while (pointIndex < result.points.length && | 626 while (pointIndex < result.metrics.length && |
| 343 intervalIndex < this.intervals_.length) { | 627 intervalIndex < this.intervals_.length) { |
| 344 var point = result.points[pointIndex++]; | 628 var point = result.metrics[pointIndex++]; |
| 345 while (intervalIndex < this.intervals_.length && | 629 while (intervalIndex < this.intervals_.length && |
| 346 point.time > interval.end) { | 630 point.time > interval.end) { |
| 347 interval = this.intervals_[++intervalIndex]; // Jump to new interval. | 631 interval = this.intervals_[++intervalIndex]; // Jump to new interval. |
| 348 if (series.length > 0) { | 632 if (series.length > 0) { |
| 349 series = []; // Start a new series. | 633 series = []; // Start a new series. |
| 350 metricValue.data.push(series); // Put it on the end of the data. | 634 metricDetails.data.push(series); // Put it on the end of the data. |
| 351 } | 635 } |
| 352 } | 636 } |
| 353 if (intervalIndex < this.intervals_.length && | 637 if (intervalIndex < this.intervals_.length && |
| 354 point.time > interval.start) { | 638 point.time > interval.start) { |
| 355 series.push([point.time - timezoneOffset, point.value]); | 639 series.push([point.time - timezoneOffset_, point.value]); |
| 356 } | 640 } |
| 357 } | 641 } |
| 358 metricValue.divs.forEach(this.drawChart, this); | 642 metricDetails.maxValue = Math.max(metricDetails.maxValue, |
| 643 result.maxValue); | |
| 644 | |
| 645 metricDetails.divs.forEach(this.drawChart, this); | |
| 359 }, | 646 }, |
| 360 | 647 |
| 361 /** | 648 /** |
| 362 * Add a new event to the chart(s). | 649 * Add a new event to the chart(s). |
| 363 * @param {string} eventType The type of event to start displaying. | 650 * @param {string} eventType The type of event to start displaying. |
| 364 */ | 651 */ |
| 365 addEventType: function(eventType) { | 652 addEventType: function(eventType) { |
| 366 // Events show on all charts. | 653 // Events show on all charts. |
| 367 this.eventMap_[eventType].divs = this.charts; | 654 this.eventDetailsMap_[eventType].divs = this.charts_; |
| 368 this.refreshEventType(eventType); | 655 this.refreshEventType(eventType); |
| 369 }, | 656 }, |
| 370 | 657 |
| 371 /* | 658 /* |
| 372 * Remove an event from the chart(s). | 659 * Remove an event from the chart(s). |
| 373 * @param {string} eventType The type of event to stop displaying. | 660 * @param {string} eventType The type of event to stop displaying. |
| 374 */ | 661 */ |
| 375 dropEventType: function(eventType) { | 662 dropEventType: function(eventType) { |
| 376 var eventValue = this.eventMap_[eventType]; | 663 var eventValue = this.eventDetailsMap_[eventType]; |
| 377 var affectedCharts = eventValue.divs; | 664 var affectedCharts = eventValue.divs; |
| 378 eventValue.divs = []; | |
| 379 | 665 |
| 666 eventValue.divs = []; // Gotta do this now for correct drawChart results. | |
| 380 affectedCharts.forEach(this.drawChart, this); | 667 affectedCharts.forEach(this.drawChart, this); |
| 381 }, | 668 }, |
| 382 | 669 |
| 383 /** | 670 /** |
| 384 * Request new data for |eventType|, for times in the current range. | 671 * Request new data for |eventType|, for times in the current range. |
| 385 * @param {string} eventType The type of event to get new data for. | 672 * @param {string} eventType The type of event to get new data for. |
| 386 */ | 673 */ |
| 387 refreshEventType: function(eventType) { | 674 refreshEventType: function(eventType) { |
| 388 // Mark eventType as awaiting response. | 675 // Mark eventType as awaiting response. |
| 389 this.eventMap_[eventType].data = null; | 676 this.eventDetailsMap_[eventType].data = null; |
| 390 | 677 |
| 391 chrome.send('getEvents', [eventType, this.start, this.end]); | 678 chrome.send('getEvents', [eventType, this.start, this.end]); |
| 392 }, | 679 }, |
| 393 | 680 |
| 394 /** | 681 /** |
| 395 * Receive new events for |eventType|. If |eventType| has been deselected | 682 * Receive new events for |eventType|. If |eventType| has been deselected |
| 396 * while awaiting webui handler response, do nothing. Otherwise, save the | 683 * while awaiting webui handler response, do nothing. Otherwise, save the |
| 397 * data directly, since events are handled differently than metrics | 684 * data directly, since events are handled differently than metrics |
| 398 * when drawing (no "series"), and redraw all the affected charts. | 685 * when drawing (no 'series'), and redraw all the affected charts. |
| 399 * @param {!{ | 686 * @param {!{ |
| 400 * type: number, | 687 * id: number, |
| 401 * points: !Array.<{time: number, longDescription: string}> | 688 * events: !Array.<{time: number}> |
| 402 * }} result An object giving eventType ID, and time/description pairs for | 689 * }} result An object giving eventType id, and times at which that event |
| 403 * each event of that type in the range requested. | 690 * type occured in the range requested. Each object in the array may |
| 691 * also have an arbitrary list of properties to be displayed as | |
| 692 * a tooltip message for the event. | |
| 404 */ | 693 */ |
| 405 getEventsCallback: function(result) { | 694 getEventsCallback: function(result) { |
| 406 var eventValue = this.eventMap_[result.eventType]; | 695 var eventValue = this.eventDetailsMap_[result.eventId]; |
| 407 | 696 |
| 408 if (eventValue.divs.length == 0) | 697 if (eventValue.divs.length == 0) |
| 409 return; | 698 return; |
| 410 | 699 |
| 411 result.points.forEach(function(element) { | 700 result.events.forEach(function(element) { |
| 412 element.time -= timezoneOffset; | 701 element.time -= timezoneOffset_; |
| 413 }); | 702 }); |
| 414 | 703 |
| 415 eventValue.data = result.points; | 704 eventValue.data = result.events; |
| 416 eventValue.divs.forEach(this.drawChart, this); | 705 eventValue.divs.forEach(this.drawChart, this); |
| 417 }, | 706 }, |
| 418 | 707 |
| 419 /** | 708 /** |
| 420 * Return an object containing an array of metrics and another of events | 709 * Return an object containing an array of metrics and another of events |
| 421 * that include |chart| as one of the divs into which they display. | 710 * that include |chart| as one of the divs into which they display. |
| 422 * @param {HTMLDivElement} chart The <div> for which to get relevant items. | 711 * @param {HTMLDivElement} chart The <div> for which to get relevant items. |
| 423 * @return {!{metrics: !Array,<Object>, events: !Array.<Object>}} | 712 * @return {!{metrics: !Array,<Object>, events: !Array.<Object>}} |
| 424 * @private | 713 * @private |
| 425 */ | 714 */ |
| 426 getChartData_: function(chart) { | 715 getChartData_: function(chart) { |
| 427 var result = {metrics: [], events: []}; | 716 var result = {metrics: [], events: []}; |
| 428 | 717 |
| 429 for (var metric in this.metricMap_) { | 718 for (var metric in this.metricDetailsMap_) { |
| 430 var metricValue = this.metricMap_[metric]; | 719 var metricDetails = this.metricDetailsMap_[metric]; |
| 431 | 720 |
| 432 if (metricValue.divs.indexOf(chart) != -1) | 721 if (metricDetails.divs.indexOf(chart) != -1) |
| 433 result.metrics.push(metricValue); | 722 result.metrics.push(metricDetails); |
| 434 } | 723 } |
| 435 | 724 |
| 436 for (var eventType in this.eventMap_) { | 725 for (var eventType in this.eventDetailsMap_) { |
| 437 var eventValue = this.eventMap_[eventType]; | 726 var eventValue = this.eventDetailsMap_[eventType]; |
| 438 | 727 |
| 439 // Events post to all divs, if they post to any. | 728 // Events post to all divs, if they post to any. |
| 440 if (eventValue.divs.length > 0) | 729 if (eventValue.divs.length > 0) |
| 441 result.events.push(eventValue); | 730 result.events.push(eventValue); |
| 442 } | 731 } |
| 443 | 732 |
| 444 return result; | 733 return result; |
| 445 }, | 734 }, |
| 446 | 735 |
| 447 /** | 736 /** |
| 448 * Check all entries in an object of the type returned from getChartData, | 737 * Check all entries in an object of the type returned from getChartData, |
| 449 * above, to see if all events and metrics have completed data (none is | 738 * above, to see if all events and metrics have completed data (none is |
| 450 * awaiting an asynchronous webui handler response to get their current | 739 * awaiting an asynchronous webui handler response to get their current |
| 451 * data). | 740 * data). |
| 452 * @param {!{metrics: !Array,<Object>, events: !Array.<Object>}} chartData | 741 * @param {!{metrics: !Array,<Object>, events: !Array.<Object>}} chartData |
| 453 * The event/metric data to check for readiness. | 742 * The event/metric data to check for readiness. |
| 454 * @return {boolean} Whether data is ready. | 743 * @return {boolean} Whether data is ready. |
| 455 * @private | 744 * @private |
| 456 */ | 745 */ |
| 457 isDataReady_: function(chartData) { | 746 isDataReady_: function(chartData) { |
| 458 for (var i = 0; i < chartData.metrics.length; i++) { | 747 chartData.metrics.forEach(function(metric) { |
| 459 if (!chartData.metrics[i].data) | 748 if (!metric.data) |
| 460 return false; | 749 return false; |
|
Jeffrey Yasskin
2012/08/31 22:28:16
This 'return false' doesn't return all the way out
clintstaley
2012/09/04 19:28:56
This worked OK with standard for-loops, too. But
| |
| 461 } | 750 }); |
| 462 | 751 |
| 463 for (var i = 0; i < chartData.events.length; i++) { | 752 chartData.events.forEach(function(event) { |
| 464 if (!chartData.events[i].data) | 753 if (!event.data) |
| 465 return false; | 754 return false; |
| 466 } | 755 }); |
| 467 | 756 |
| 468 return true; | 757 return true; |
| 469 }, | 758 }, |
| 470 | 759 |
| 471 /** | 760 /** |
| 472 * Create and return an array of "markings" (per Flot), representing | 761 * Create and return an array of 'markings' (per Flot), representing |
| 473 * vertical lines at the event time, in the event's color. Also add | 762 * vertical lines at the event time, in the event's color. Also add |
| 474 * (not per Flot) a |description| property to each, to be used for hand | 763 * (not per Flot) a |popupTitle| property to each, to be used for |
| 475 * creating description boxes. | 764 * labelling description popups. |
| 476 * @param {!Array.<{ | 765 * @param {!Array.<{ |
| 477 * description: string, | 766 * description: string, |
| 478 * color: string, | 767 * color: string, |
| 479 * data: !Array.<{time: number}> | 768 * data: !Array.<{time: number}> |
| 480 * }>} eventValues The events to make markings for. | 769 * }>} eventValues The events to make markings for. |
| 481 * @return {!Array.<{ | 770 * @return {!Array.<{ |
| 482 * color: string, | 771 * color: string, |
| 483 * description: string, | 772 * description: string, |
| 484 * xaxis: {from: number, to: number} | 773 * xaxis: {from: number, to: number} |
| 485 * }>} A marks data structure for Flot to use. | 774 * }>} A marks data structure for Flot to use. |
| 486 * @private | 775 * @private |
| 487 */ | 776 */ |
| 488 getEventMarks_: function(eventValues) { | 777 getEventMarks_: function(eventValues) { |
| 489 var markings = []; | 778 var markings = []; |
| 779 var explanation; | |
| 780 var date, hours, minutes; | |
| 490 | 781 |
| 491 for (var i = 0; i < eventValues.length; i++) { | 782 eventValues.forEach(function(eventValue) { |
| 492 var eventValue = eventValues[i]; | 783 eventValue.data.forEach(function(point) { |
| 493 for (var d = 0; d < eventValue.data.length; d++) { | 784 if (point.time >= this.start - timezoneOffset_ && |
| 494 var point = eventValue.data[d]; | 785 point.time <= this.end - timezoneOffset_) { |
| 495 if (point.time >= this.start - timezoneOffset && | 786 |
| 496 point.time <= this.end - timezoneOffset) { | 787 // Date wants Zulu time. |
| 788 date = new Date(point.time + timezoneOffset_); | |
| 789 hours = date.getHours(); | |
| 790 minutes = date.getMinutes(); | |
| 791 explanation = eventValue.popupTitle + '\nAt: ' + | |
| 792 (date.getMonth() + 1) + '/' + date.getDate() + ' ' + | |
| 793 (hours < 10 ? '0' : '') + hours + ':' + | |
| 794 (minutes < 10 ? '0' : '') + minutes + '\n'; | |
| 795 | |
| 796 for (var key in point) { | |
| 797 if (key != 'time') { | |
| 798 var datum = point[key]; | |
| 799 if ('label' in datum && 'value' in datum) | |
| 800 explanation = explanation + datum.label + ': ' + | |
| 801 datum.value + '\n'; | |
| 802 } | |
| 803 } | |
| 497 markings.push({ | 804 markings.push({ |
| 498 color: eventValue.color, | 805 color: eventValue.color, |
| 499 description: eventValue.description, | 806 popupContent: explanation, |
| 500 xaxis: {from: point.time, to: point.time} | 807 xaxis: {from: point.time, to: point.time} |
| 501 }); | 808 }); |
| 502 } else { | 809 } else { |
| 503 console.log('Event out of time range ' + this.start + ' -> ' + | 810 console.log('Event out of time range ' + this.start + ' -> ' + |
|
Jeffrey Yasskin
2012/08/31 22:28:16
Remember that 'this' gets reset within a function
clintstaley
2012/09/04 19:28:56
Thanks. Missed this one when scanning for "this" u
| |
| 504 this.end + ' at: ' + point.time); | 811 this.end + ' at: ' + point.time); |
| 505 } | 812 } |
| 506 } | 813 }); |
| 507 } | 814 }); |
| 508 | 815 |
| 509 return markings; | 816 return markings; |
| 510 }, | 817 }, |
| 511 | 818 |
| 512 /** | 819 /** |
| 513 * Redraw the chart in div |chart|, *if* all its dependent data is present. | 820 * Redraw the chart in div |chart|, *if* all its dependent data is present. |
| 514 * Otherwise simply return, and await another call when all data is | 821 * Otherwise simply return, and await another call when all data is |
| 515 * available. | 822 * available. |
| 516 * @param {HTMLDivElement} chart The <div> to redraw. | 823 * @param {HTMLDivElement} chart The <div> to redraw. |
| 517 */ | 824 */ |
| 518 drawChart: function(chart) { | 825 drawChart: function(chart) { |
| 519 var chartData = this.getChartData_(chart); | 826 var chartData = this.getChartData_(chart); |
| 827 var axisMap = {}; // Maps category ids to yAxis numbers | |
| 520 | 828 |
| 521 if (!this.isDataReady_(chartData)) | 829 if (chart.hidden || !this.isDataReady_(chartData)) |
| 522 return; | 830 return; |
| 523 | 831 |
| 524 var seriesSeq = []; | 832 var seriesSeq = []; |
| 525 var yAxes = []; | 833 var yAxes = []; |
| 526 chartData.metrics.forEach(function(value) { | 834 chartData.metrics.forEach(function(metricDetails) { |
| 527 yAxes.push(value.yAxis); | 835 var metricCategory = metricDetails.category; |
| 528 for (var i = 0; i < value.data.length; i++) { | 836 var yAxisNumber = axisMap[metricCategory.metricCategoryId]; |
| 837 | |
| 838 // Add a new y-axis if we are encountering this category of metric | |
| 839 // for the first time. Otherwise, update the existing y-axis with | |
| 840 // a new max value if needed. (Presently, we expect only one category | |
| 841 // of metric per chart, but this design permits more in the future.) | |
| 842 if (yAxisNumber === undefined) { | |
| 843 yAxes.push({min: 0, max: metricDetails.maxValue * yAxisMargin_, | |
| 844 labelWidth: 60}); | |
| 845 axisMap[metricCategory.metricCategoryId] = yAxisNumber = yAxes.length; | |
| 846 } else { | |
| 847 yAxes[yAxisNumber - 1].max = Math.max(yAxes[yAxisNumber - 1].max, | |
| 848 metricDetails.maxValue * yAxisMargin_); | |
| 849 } | |
| 850 | |
| 851 for (var i = 0; i < metricDetails.data.length; i++) { | |
| 529 seriesSeq.push({ | 852 seriesSeq.push({ |
| 530 color: value.yAxis.color, | 853 color: metricDetails.color, |
| 531 data: value.data[i], | 854 data: metricDetails.data[i], |
| 532 label: i == 0 ? value.description + ' (' + value.units + ')' : null, | 855 label: i == 0 ? metricDetails.name + |
| 533 yaxis: yAxes.length, // Use just-added Y axis. | 856 ' (' + metricCategory.unit + ')' : null, |
| 857 yaxis: yAxisNumber | |
| 534 }); | 858 }); |
| 535 } | 859 } |
| 536 }); | 860 }); |
| 537 | 861 |
| 538 // Ensure at least one y axis, as a reference for event placement. | |
| 539 if (yAxes.length == 0) | |
| 540 yAxes.push({max: 1.0}); | |
| 541 | |
| 542 var markings = this.getEventMarks_(chartData.events); | 862 var markings = this.getEventMarks_(chartData.events); |
| 543 var chart = this.charts[0]; | |
| 544 var plot = $.plot(chart, seriesSeq, { | 863 var plot = $.plot(chart, seriesSeq, { |
| 545 yaxes: yAxes, | 864 yaxes: yAxes, |
| 546 xaxis: { | 865 xaxis: { |
| 547 mode: 'time', | 866 mode: 'time', |
| 548 min: this.start - timezoneOffset, | 867 min: this.start - timezoneOffset_, |
| 549 max: this.end - timezoneOffset | 868 max: this.end - timezoneOffset_ |
| 550 }, | 869 }, |
| 551 points: {show: true, radius: 1}, | 870 points: {show: true, radius: 1}, |
| 552 lines: {show: true}, | 871 lines: {show: true}, |
| 553 grid: {markings: markings} | 872 grid: {markings: markings, hoverable: true, autoHighlight: true} |
| 554 }); | 873 }); |
| 555 | 874 |
| 556 // For each event in |markings|, create also a label div, with left | 875 // For each event in |markings|, create also a label div, with left |
| 557 // edge colinear with the event vertical line. Top of label is | 876 // edge colinear with the event vertical line. Top of label is |
| 558 // presently a hack-in, putting labels in three tiers of 25px height | 877 // presently a hack-in, putting labels in three tiers of 25px height |
| 559 // each to avoid overlap. Will need something better. | 878 // each to avoid overlap. Will need something better. |
| 560 var labelTemplate = $('#label-template')[0]; | 879 var labelTemplate = $('#label-template')[0]; |
| 561 for (var i = 0; i < markings.length; i++) { | 880 for (var i = 0; i < markings.length; i++) { |
| 562 var mark = markings[i]; | 881 var mark = markings[i]; |
| 563 var point = | 882 var point = |
| 564 plot.pointOffset({x: mark.xaxis.to, y: yAxes[0].max, yaxis: 1}); | 883 plot.pointOffset({x: mark.xaxis.to, y: yAxes[0].max, yaxis: 1}); |
| 565 var labelDiv = labelTemplate.cloneNode(true); | 884 var labelDiv = labelTemplate.cloneNode(true); |
| 566 labelDiv.innerText = mark.description; | 885 labelDiv.innerText = mark.popupContent; |
| 567 labelDiv.style.left = point.left + 'px'; | 886 labelDiv.style.left = point.left + 'px'; |
| 568 labelDiv.style.top = (point.top + 25 * (i % 3)) + 'px'; | 887 labelDiv.style.top = (point.top + 100 * (i % 3)) + 'px'; |
| 569 | 888 |
| 570 chart.appendChild(labelDiv); | 889 chart.appendChild(labelDiv); |
| 890 labelDiv.hidden = true; | |
| 891 chart.hovers.push({x: mark.xaxis.to, div: labelDiv}); | |
| 571 } | 892 } |
| 572 } | 893 } |
| 573 }; | 894 }; |
| 574 return { | 895 return { |
| 575 PerformanceMonitor: PerformanceMonitor | 896 PerformanceMonitor: PerformanceMonitor |
| 576 }; | 897 }; |
| 577 }); | 898 }); |
| 578 | 899 |
| 579 var PerformanceMonitor = new performance_monitor.PerformanceMonitor(); | 900 var PerformanceMonitor = new performance_monitor.PerformanceMonitor(); |
| OLD | NEW |