| 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; |
| 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 y-axis 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: Aside from the |time| key, each event type has varying other |
| 143 * properties, with unknown key names, which properties must still be |
| 144 * displayed. Such properties always have value of form |
| 145 * {label: 'some label', value: 'some value'}, with label and value |
| 146 * internationalized. |
| 147 * @typedef {{ |
| 148 * eventId: number, |
| 149 * name: string, |
| 150 * popupTitle: string, |
| 90 * description: string, | 151 * description: string, |
| 91 * color: string, | 152 * color: string, |
| 92 * divs: !Array.<HTMLDivElement>, | 153 * divs: !Array.<HTMLDivElement>, |
| 93 * data: ?Array.<{time: number, longDescription: string}> | 154 * data: ?Array.<{time: number}> |
| 155 * }} |
| 156 */ |
| 157 PerformanceMonitor.EventDetails; |
| 158 |
| 159 /** |
| 160 * Metrics fall into categories that have common units and thus may |
| 161 * share a common graph, or share y-axes within a multi-y-axis graph. |
| 162 * Each category has one home chart in which metrics of that category |
| 163 * are displayed. Currently this is also the only chart in which such |
| 164 * metrics are displayed, but the design permits a metric to show in |
| 165 * several charts if this is useful later on. |
| 166 * @type {Object.<string, { |
| 167 * metricCategoryId: number, |
| 168 * name: string, |
| 169 * description: string, |
| 170 * unit: string, |
| 171 * details: Array.<{!PerformanceMonitor.MetricDetails}>, |
| 172 * homeChart: !HTMLDivElement |
| 94 * }>} | 173 * }>} |
| 95 * @private | 174 * @private |
| 96 */ | 175 */ |
| 97 this.eventMap_ = {}; | 176 this.metricCategoryMap_ = {}; |
| 177 |
| 178 /** |
| 179 * Comprehensive map from metricId to MetricDetails. |
| 180 * @type {Object.<number, {PerformanceMonitor.MetricDetails}>} |
| 181 * @private |
| 182 */ |
| 183 this.metricDetailsMap_ = {}; |
| 184 |
| 185 /** |
| 186 * Events fall into categories just like metrics, above. This category |
| 187 * grouping is not as important as that for metrics, since events |
| 188 * needn't share maxima, y-axes, nor units, and since events appear on |
| 189 * all charts. But grouping of event categories in the event-selection |
| 190 * UI is still useful. |
| 191 * @type {Object.<string, { |
| 192 * eventCategoryId: number, |
| 193 * name: string, |
| 194 * description: string, |
| 195 * details: !Array.<!PerformanceMonitor.EventDetails>, |
| 196 * }>} |
| 197 * @private |
| 198 */ |
| 199 this.eventCategoryMap_ = {}; |
| 200 |
| 201 /** |
| 202 * Comprehensive map from eventId to EventDetails. |
| 203 * @type {Object.<number, {PerformanceMonitor.EventDetails}>} |
| 204 * @private |
| 205 */ |
| 206 this.eventDetailsMap_ = {}; |
| 98 | 207 |
| 99 /** | 208 /** |
| 100 * Time periods in which the browser was active and collecting metrics | 209 * Time periods in which the browser was active and collecting metrics |
| 101 * and events. | 210 * and events. |
| 102 * @type {!Array.<{start: number, end: number}>} | 211 * @type {!Array.<{start: number, end: number}>} |
| 103 * @private | 212 * @private |
| 104 */ | 213 */ |
| 105 this.intervals_ = []; | 214 this.intervals_ = []; |
| 106 | 215 |
| 107 this.setupTimeRangeChooser_(); | 216 /** |
| 108 chrome.send('getAllEventTypes'); | 217 * Handle of timer interval function used to update charts |
| 109 chrome.send('getAllMetricTypes'); | 218 * @type {Object} |
| 110 this.setupMainChart_(); | 219 * @private |
| 220 */ |
| 221 this.updateTimer_ = null; |
| 222 |
| 223 /** |
| 224 * Handle of timer interval function used to check for resizes. Nonnull |
| 225 * only when resize events are coming steadily. |
| 226 * @type {Object} |
| 227 * @private |
| 228 */ |
| 229 this.resizeTimer_ = null; |
| 230 |
| 231 /** |
| 232 * All chart divs in the display, whether hidden or not. Presently |
| 233 * this has one entry for each metric category in |this.metricCategoryMap|. |
| 234 * @type {Array.<HTMLDivElement>} |
| 235 * @private |
| 236 */ |
| 237 this.charts_ = []; |
| 238 |
| 239 this.setupTimeRangeTab_(); |
| 240 chrome.send('getEventTypes'); |
| 241 chrome.send('getMetricTypes'); |
| 111 TimeRange_.day.element.click(); | 242 TimeRange_.day.element.click(); |
| 112 } | 243 } |
| 113 | 244 |
| 114 PerformanceMonitor.prototype = { | 245 PerformanceMonitor.prototype = { |
| 115 /** | 246 /** |
| 116 * Receive a list of all metrics, and populate |this.metricMap_| to | 247 * Receive a list of all metric categories, each with its corresponding |
| 117 * reflect said list. Reconfigure the checkbox set for metric selection. | 248 * list of metric details. Populate |this.metricCategoryMap_| and |
| 118 * @param {Array.<{metricType: string, shortDescription: string}>} | 249 * |this.metrictDetailsMap_| to reflect said list. Reconfigure the |
| 119 * allMetrics All metrics from which to select. | 250 * checkbox set for metric selection. |
| 251 * @param {Array.<{ |
| 252 * metricCategoryId: number, |
| 253 * name: string, |
| 254 * unit: string, |
| 255 * description: string, |
| 256 * details: Array.<{ |
| 257 * metricId: number, |
| 258 * name: string, |
| 259 * description: string |
| 260 * }> |
| 261 * }>} categories All metric categories needing charts and checkboxes. |
| 120 */ | 262 */ |
| 121 getAllMetricTypesCallback: function(allMetrics) { | 263 getMetricTypesCallback: function(categories) { |
| 122 for (var i = 0; i < allMetrics.length; i++) { | 264 categories.forEach(function(category) { |
| 123 var metric = allMetrics[i]; | 265 this.addCategoryChart_(category); |
| 266 this.metricCategoryMap_[category.metricCategoryId] = category; |
| 124 | 267 |
| 125 this.metricMap_[metric.metricType] = { | 268 category.details.forEach(function(metric) { |
| 126 id: metric.metricType, | 269 metric.color = ColorTable_[metric.metricId % ColorTable_.length]; |
| 127 description: metric.shortDescription, | 270 metric.maxValue = 1; |
| 128 units: metric.units, | 271 metric.divs = []; |
| 129 yAxis: {min: 0, max: metric.maxValue, | 272 metric.data = null; |
| 130 color: jQuery.color.parse('blue')}, | 273 metric.category = category; |
| 131 divs: [], | 274 this.metricDetailsMap_[metric.metricId] = metric; |
| 132 data: null | 275 }, this); |
| 133 }; | 276 }, this); |
| 134 } | |
| 135 | 277 |
| 136 this.setupCheckboxes_($('#choose-metrics')[0], | 278 this.setupCheckboxes_($('#choose-metrics')[0], |
| 137 this.metricMap_, this.addMetric, this.dropMetric); | 279 this.metricCategoryMap_, 'metricId', this.addMetric, this.dropMetric); |
| 138 }, | 280 }, |
| 139 | 281 |
| 140 /** | 282 /** |
| 141 * Receive a list of all event types, and populate |this.eventMap_| to | 283 * Receive a list of all event categories, each with its correspoinding |
| 142 * reflect said list. Reconfigure the checkbox set for event selection. | 284 * list of event details. Populate |this.eventCategoryMap_| and |
| 143 * @param {Array.<{eventType: string, shortDescription: string}>} | 285 * |this.eventDetailsMap| to reflect said list. Reconfigure the |
| 144 * allEvents All events from which to select. | 286 * checkbox set for event selection. |
| 287 * @param {Array.<{ |
| 288 * eventCategoryId: number, |
| 289 * name: string, |
| 290 * description: string, |
| 291 * details: Array.<{ |
| 292 * eventId: number, |
| 293 * name: string, |
| 294 * description: string |
| 295 * }> |
| 296 * }>} categories All event categories needing charts checkboxes. |
| 145 */ | 297 */ |
| 146 getAllEventTypesCallback: function(allEvents) { | 298 getEventTypesCallback: function(categories) { |
| 147 for (var i = 0; i < allEvents.length; i++) { | 299 categories.forEach(function(category) { |
| 148 var eventInfo = allEvents[i]; | 300 this.eventCategoryMap_[category.eventCategoryId] = category; |
| 149 | 301 |
| 150 this.eventMap_[eventInfo.eventType] = { | 302 category.details.forEach(function(event) { |
| 151 id: eventInfo.eventType, | 303 event.color = ColorTable_[event.eventId % ColorTable_.length]; |
| 152 color: jQuery.color.parse('red'), | 304 event.divs = []; |
| 153 data: null, | 305 event.data = null; |
| 154 description: eventInfo.shortDescription, | 306 this.eventDetailsMap_[event.eventId] = event; |
| 155 divs: [] | 307 }, this); |
| 156 }; | 308 }, this); |
| 157 } | |
| 158 | 309 |
| 159 this.setupCheckboxes_($('#choose-events')[0], | 310 this.setupCheckboxes_($('#choose-events')[0], this.eventCategoryMap_, |
| 160 this.eventMap_, this.addEventType, this.dropEventType); | 311 'eventId', this.addEventType, this.dropEventType); |
| 161 }, | 312 }, |
| 162 | 313 |
| 163 /** | 314 /** |
| 164 * Set up the radio button set to choose time range. Use div#radio-template | 315 * Set up the radio button set to choose time range. Use div#radio-template |
| 165 * as a template. | 316 * as a template. |
| 166 * @private | 317 * @private |
| 167 */ | 318 */ |
| 168 setupTimeRangeChooser_: function() { | 319 setupTimeRangeTab_: function() { |
| 169 var timeDiv = $('#choose-time-range')[0]; | 320 var timeDiv = $('#choose-time-range')[0]; |
| 321 var backButton = $('#back-time')[0]; |
| 322 var forwardButton = $('#forward-time')[0]; |
| 170 var radioTemplate = $('#radio-template')[0]; | 323 var radioTemplate = $('#radio-template')[0]; |
| 171 | 324 |
| 172 for (var time in TimeRange_) { | 325 for (var time in TimeRange_) { |
| 173 var timeRange = TimeRange_[time]; | 326 var timeRange = TimeRange_[time]; |
| 174 var radio = radioTemplate.cloneNode(true); | 327 var radio = radioTemplate.cloneNode(true); |
| 175 var input = radio.querySelector('input'); | 328 var input = radio.querySelector('input'); |
| 176 | 329 |
| 177 input.value = timeRange.value; | 330 input.value = timeRange.value; |
| 178 input.timeRange = timeRange; | 331 input.timeRange = timeRange; |
| 179 radio.querySelector('span').innerText = timeRange.name; | 332 radio.querySelector('span').innerText = timeRange.name; |
| 180 timeDiv.appendChild(radio); | 333 timeDiv.appendChild(radio); |
| 181 timeRange.element = input; | 334 timeRange.element = input; |
| 182 } | 335 } |
| 183 | 336 |
| 184 timeDiv.addEventListener('click', function(e) { | 337 timeDiv.addEventListener('click', function(e) { |
| 185 if (!e.target.webkitMatchesSelector('input[type="radio"]')) | 338 if (!e.target.webkitMatchesSelector('input[type="radio"]')) |
| 186 return; | 339 return; |
| 187 | 340 |
| 188 this.setTimeRange(e.target.timeRange); | 341 this.setTimeRange(e.target.timeRange, Date.now(), true); |
| 189 }.bind(this)); | 342 }.bind(this)); |
| 190 }, | 343 |
| 191 | 344 forwardButton.addEventListener('click', this.forwardTime.bind(this)); |
| 192 /** | 345 backButton.addEventListener('click', this.backTime.bind(this)); |
| 193 * Generalized function for setting up checkbox blocks for either events | 346 }, |
| 194 * or metrics. Take a div |div| into which to place the checkboxes, | 347 |
| 195 * and a map |optionMap| with values that each include a property | 348 /** |
| 196 * |description|. Set up one checkbox for each entry in |optionMap| | 349 * Generalized function to create checkboxes for either events |
| 197 * labelled with that description. Arrange callbacks to function |check| | 350 * or metrics, given a |div| into which to put the checkboxes, and a |
| 198 * or |uncheck|, passing them the key of the checked or unchecked option, | 351 * |optionCategoryMap| describing the checkbox structure. |
| 199 * when the relevant checkbox state changes. | 352 * |
| 353 * For instance, |optionCategoryMap| might be metricCategoryMap_, with |
| 354 * contents thus: |
| 355 * |
| 356 * optionCategoryMap : { |
| 357 * 1: { |
| 358 * name: 'CPU', |
| 359 * details: [ |
| 360 * {metricId: 1, name: 'CPU Usage', color: 'rgb(255, 128, 128)'} |
| 361 * ], |
| 362 * 2: { |
| 363 * name : 'Memory', |
| 364 * details: [ |
| 365 * {metricId: 2, name: 'Private Memory Usage', |
| 366 * color: 'rgb(128, 255, 128)'}, |
| 367 * {metricId: 3, name: 'Shared Memory Usage', |
| 368 * color: 'rgb(128, 128, 255)'} |
| 369 * ] |
| 370 * } |
| 371 * |
| 372 * and we would call setupCheckboxes_ thus: |
| 373 * |
| 374 * this.setupCheckboxes_(<parent div>, this.metricCategoryMap_, 'metricId', |
| 375 * this.addMetric, this.dropMetric); |
| 376 * |
| 377 * MetricCategoryMap_'s values each have a |name| and |details| property. |
| 378 * SetupCheckboxes_ creates one major header for each such value, with title |
| 379 * given by the |name| field. Under each major header are checkboxes, |
| 380 * one for each element in the |details| property. The checkbox titles |
| 381 * come from the |name| property of each |details| object, |
| 382 * and they each have an associated colored icon matching the |color| |
| 383 * property of the details object. |
| 384 * |
| 385 * So, for the example given, the generated HTML looks thus: |
| 386 * |
| 387 * <div id="category-label-template" class="category-label">CPU</div> |
| 388 * <div id="detail-checkbox-template" class="detail-checkbox"> |
| 389 * <div class="horizontal-box"> |
| 390 * <input type="checkbox"> |
| 391 * <div class="detail-label">CPU Usage</div> |
| 392 * <div class="spacer"></div> |
| 393 * <div class="color-icon" |
| 394 * style="background-color: rgb(255, 128, 128);"></div> |
| 395 * </div> |
| 396 * </div> |
| 397 * </div> |
| 398 * |
| 399 * <div id="category-label-template" class="category-label">Memory</div> |
| 400 * <div id="detail-checkbox-template" class="detail-checkbox"> |
| 401 * <div class="horizontal-box"> |
| 402 * <input type="checkbox"> |
| 403 * <div class="detail-label">Private Memory Usage</div> |
| 404 * <div class="spacer"></div> |
| 405 * <div class="color-icon" |
| 406 * style="background-color: rgb(128, 255, 128);"></div> |
| 407 * </div> |
| 408 * </div> |
| 409 * </div><div id="detail-checkbox-template" class="detail-checkbox"> |
| 410 * <div class="horizontal-box"> |
| 411 * <input type="checkbox"> |
| 412 * <div class="detail-label">Shared Memory Usage</div> |
| 413 * <div class="spacer"></div> |
| 414 * <div class="color-icon" |
| 415 * style="background-color: rgb(128, 128, 255);"></div> |
| 416 * </div> |
| 417 * </div> |
| 418 * </div> |
| 419 * |
| 420 * The checkboxes for each details object call addMetric or |
| 421 * dropMetric as they are checked and unchecked, passing the relevant |
| 422 * |metricId| value. Parameter 'metricId' identifies key |metricId| as the |
| 423 * identifying property to pass to the methods. So, for instance, checking |
| 424 * the CPU Usage box results in a call to this.addMetric(1), since |
| 425 * metricCategoryMap_[1].details[0].metricId == 1. |
| 426 * |
| 427 * In general, |optionCategoryMap| must have values that each include |
| 428 * a property |name|, and a property |details|. The |details| value must |
| 429 * be an array of objects that in turn each have an identifying property |
| 430 * with key given by parameter |idKey|, plus a property |name| and a |
| 431 * property |color|. |
| 432 * |
| 200 * @param {!HTMLDivElement} div A <div> into which to put checkboxes. | 433 * @param {!HTMLDivElement} div A <div> into which to put checkboxes. |
| 201 * @param {!Object} optionMap A map of metric/event entries. | 434 * @param {!Object} optionCategoryMap A map of metric/event categories. |
| 435 * @param {string} idKey The key of the id property. |
| 202 * @param {!function(this:Controller, Object)} check | 436 * @param {!function(this:Controller, Object)} check |
| 203 * The function to select an entry (metric or event). | 437 * The function to select an entry (metric or event). |
| 204 * @param {!function(this:Controller, Object)} uncheck | 438 * @param {!function(this:Controller, Object)} uncheck |
| 205 * The function to deselect an entry (metric or event). | 439 * The function to deselect an entry (metric or event). |
| 206 * @private | 440 * @private |
| 207 */ | 441 */ |
| 208 setupCheckboxes_: function(div, optionMap, check, uncheck) { | 442 setupCheckboxes_: function(div, optionCategoryMap, idKey, check, uncheck) { |
| 209 var checkboxTemplate = $('#checkbox-template')[0]; | 443 var checkboxTemplate = $('#detail-checkbox-template')[0]; |
| 210 | 444 var labelTemplate = $('#category-label-template')[0]; |
| 211 for (var option in optionMap) { | 445 |
| 212 var checkbox = checkboxTemplate.cloneNode(true); | 446 for (var c in optionCategoryMap) { |
| 213 checkbox.querySelector('span').innerText = 'Show ' + | 447 var category = optionCategoryMap[c]; |
| 214 optionMap[option].description; | 448 var label = labelTemplate.cloneNode(true); |
| 215 div.appendChild(checkbox); | 449 |
| 216 | 450 label.innerText = category.name; |
| 217 var input = checkbox.querySelector('input'); | 451 div.appendChild(label); |
| 218 input.option = optionMap[option].id; | 452 |
| 219 input.addEventListener('change', function(e) { | 453 category.details.forEach(function(details) { |
| 220 (e.target.checked ? check : uncheck).call(this, e.target.option); | 454 var checkbox = checkboxTemplate.cloneNode(true); |
| 221 }.bind(this)); | 455 var input = checkbox.querySelector('input'); |
| 456 var label = checkbox.querySelector('.detail-label'); |
| 457 var icon = checkbox.querySelector('.color-icon'); |
| 458 |
| 459 input.option = details[idKey]; |
| 460 input.icon = icon; |
| 461 input.addEventListener('change', function(e) { |
| 462 (e.target.checked ? check : uncheck).call(this, e.target.option); |
| 463 e.target.icon.style.visibility = |
| 464 e.target.checked ? 'visible' : 'hidden'; |
| 465 }.bind(this)); |
| 466 |
| 467 label.innerText = details.name; |
| 468 |
| 469 icon.style.backgroundColor = details.color; |
| 470 |
| 471 div.appendChild(checkbox); |
| 472 }, this); |
| 222 } | 473 } |
| 223 }, | 474 }, |
| 224 | 475 |
| 225 /** | 476 /** |
| 226 * Set up just one chart in which all metrics will be displayed | 477 * Add a new chart for |category|, making it initially hidden, |
| 227 * initially. But, the design readily accommodates addition of | 478 * with no metrics displayed in it. |
| 228 * new charts, and movement of metrics into those other charts. | 479 * @param {!Object} category The metric category for which to create |
| 480 * the chart. Category is a value from metricCategoryMap_. |
| 229 * @private | 481 * @private |
| 230 */ | 482 */ |
| 231 setupMainChart_: function() { | 483 addCategoryChart_: function(category) { |
| 232 this.chartParent = $('#charts')[0]; | 484 var chartParent = $('#charts')[0]; |
| 233 this.charts = [document.createElement('div')]; | 485 var chart = document.createElement('div'); |
| 234 this.charts[0].className = 'chart'; | 486 |
| 235 this.chartParent.appendChild(this.charts[0]); | 487 chart.className = 'chart'; |
| 488 chart.hidden = true; |
| 489 chartParent.appendChild(chart); |
| 490 this.charts_.push(chart); |
| 491 category.homeChart = chart; |
| 492 chart.refs = 0; |
| 493 chart.hovers = []; |
| 494 |
| 495 // Receive hover events from Flot. |
| 496 // Attached to chart will be properties 'hovers', a list of {x, div} |
| 497 // pairs. As pos events arrive, check each hover to see if it should |
| 498 // be hidden or made visible. |
| 499 $(chart).bind('plothover', function(event, pos, item) { |
| 500 var tolerance = this.range.resolution; |
| 501 |
| 502 chart.hovers.forEach(function(hover) { |
| 503 hover.div.hidden = hover.x < pos.x - tolerance || |
| 504 hover.x > pos.x + tolerance; |
| 505 }); |
| 506 |
| 507 }.bind(this)); |
| 508 |
| 509 $(window).resize(function() { |
| 510 if (this.resizeTimer_ != null) |
| 511 clearTimeout(this.resizeTimer_); |
| 512 this.resizeTimer_ = setTimeout(this.checkResize_.bind(this), |
| 513 resizeDelay_); |
| 514 }.bind(this)); |
| 515 }, |
| 516 |
| 517 /** |
| 518 * |resizeDelay_| ms have elapsed since the last resize event, and the timer |
| 519 * for redrawing has triggered. Clear it, and redraw all the charts. |
| 520 * @private |
| 521 */ |
| 522 checkResize_: function() { |
| 523 clearTimeout(this.resizeTimer_); |
| 524 this.resizeTimer_ = null; |
| 525 |
| 526 this.charts_.forEach(function(chart) {this.drawChart(chart);}, this); |
| 236 }, | 527 }, |
| 237 | 528 |
| 238 /** | 529 /** |
| 239 * Set the time range for which to display metrics and events. For | 530 * Set the time range for which to display metrics and events. For |
| 240 * now, the time range always ends at "now", but future implementations | 531 * now, the time range always ends at 'now', but future implementations |
| 241 * may allow time ranges not so anchored. | 532 * may allow time ranges not so anchored. |
| 242 * @param {!{start: number, end: number, resolution: number}} range | 533 * @param {!{start: number, end: number, resolution: number}} range |
| 243 * The time range for which to get display data. | 534 * The time range for which to get display data. |
| 244 */ | 535 * @param {number} end Ending time, in ms since epoch, to which to |
| 245 setTimeRange: function(range) { | 536 * set the new time range. |
| 537 * @param {boolean} startTimer Indicates whether we should restart the |
| 538 * range-update timer. |
| 539 */ |
| 540 setTimeRange: function(range, end, startTimer) { |
| 246 this.range = range; | 541 this.range = range; |
| 247 this.end = Math.floor(Date.now() / range.resolution) * | 542 if (this.updateTimer_ != null) |
| 543 clearInterval(this.updateTimer_); |
| 544 if (startTimer) |
| 545 this.updateTimer_ = setInterval(this.forwardTime.bind(this), |
| 546 intervalMultiple_ * range.resolution); |
| 547 this.end = Math.floor(end / range.resolution) * |
| 248 range.resolution; | 548 range.resolution; |
| 249 | 549 |
| 250 this.start = this.end - range.timeSpan; | 550 this.start = this.end - range.timeSpan; |
| 251 this.requestIntervals(); | 551 this.requestIntervals(); |
| 252 }, | 552 }, |
| 253 | 553 |
| 254 /** | 554 /** |
| 555 * Back up the time range by 1/2 of its current span, and cause chart |
| 556 * redraws. |
| 557 */ |
| 558 backTime: function() { |
| 559 this.setTimeRange(this.range, this.end - this.range.timeSpan / 2, false); |
| 560 }, |
| 561 |
| 562 /** |
| 563 * Advance the time range by 1/2 of its current span, or up to the point |
| 564 * where it ends at the present time, whichever is less. |
| 565 */ |
| 566 forwardTime: function() { |
| 567 var now = Date.now(); |
| 568 var newEnd = Math.min(now, this.end + this.range.timeSpan / 2); |
| 569 |
| 570 this.setTimeRange(this.range, newEnd, newEnd == now); |
| 571 }, |
| 572 |
| 573 /** |
| 255 * Request activity intervals in the current time range. | 574 * Request activity intervals in the current time range. |
| 256 */ | 575 */ |
| 257 requestIntervals: function() { | 576 requestIntervals: function() { |
| 258 chrome.send('getActiveIntervals', [this.start, this.end]); | 577 chrome.send('getActiveIntervals', [this.start, this.end]); |
| 259 }, | 578 }, |
| 260 | 579 |
| 261 /** | 580 /** |
| 262 * Webui callback delivering response from requestIntervals call. Assumes | 581 * Webui callback delivering response from requestIntervals call. Assumes |
| 263 * this is a new time range choice, which results in complete refresh of | 582 * this is a new time range choice, which results in complete refresh of |
| 264 * all metrics and event types that are currently selected. | 583 * all metrics and event types that are currently selected. |
| 265 * @param {!Array.<{start: number, end: number}>} intervals | 584 * @param {!Array.<{start: number, end: number}>} intervals |
| 266 * The new intervals. | 585 * The new intervals. |
| 267 */ | 586 */ |
| 268 getActiveIntervalsCallback: function(intervals) { | 587 getActiveIntervalsCallback: function(intervals) { |
| 269 this.intervals_ = intervals; | 588 this.intervals_ = intervals; |
| 270 | 589 |
| 271 for (var metric in this.metricMap_) { | 590 for (var metric in this.metricDetailsMap_) { |
| 272 var metricValue = this.metricMap_[metric]; | 591 var metricDetails = this.metricDetailsMap_[metric]; |
| 273 if (metricValue.divs.length > 0) // if we're displaying this metric. | 592 if (metricDetails.divs.length > 0) // if we're displaying this metric. |
| 274 this.refreshMetric(metric); | 593 this.refreshMetric(metricDetails); |
| 275 } | 594 } |
| 276 | 595 |
| 277 for (var eventType in this.eventMap_) { | 596 for (var eventType in this.eventDetailsMap_) { |
| 278 var eventValue = this.eventMap_[eventType]; | 597 var eventValue = this.eventDetailsMap_[eventType]; |
| 279 if (eventValue.divs.length > 0) | 598 if (eventValue.divs.length > 0) |
| 280 this.refreshEventType(eventValue.id); | 599 this.refreshEventType(eventValue.eventId); |
| 281 } | 600 } |
| 282 }, | 601 }, |
| 283 | 602 |
| 284 /** | 603 /** |
| 285 * Add a new metric to the main (currently only) chart. | 604 * Add a new metric to the display, showing it on its category's home |
| 286 * @param {string} metric The metric to start displaying. | 605 * chart. Un-hide the home chart if needed. |
| 606 * @param {number} metricId The id of the metric to start displaying. |
| 287 */ | 607 */ |
| 288 addMetric: function(metric) { | 608 addMetric: function(metricId) { |
| 289 this.metricMap_[metric].divs.push(this.charts[0]); | 609 var details = this.metricDetailsMap_[metricId]; |
| 290 this.refreshMetric(metric); | 610 var homeChart = details.category.homeChart; |
| 611 |
| 612 details.divs.push(homeChart); |
| 613 if (homeChart.refs == 0) |
| 614 homeChart.hidden = false; |
| 615 homeChart.refs++; |
| 616 |
| 617 this.refreshMetric(details); |
| 291 }, | 618 }, |
| 292 | 619 |
| 293 /** | 620 /** |
| 294 * Remove a metric from the chart(s). | 621 * Remove a metric from the chart(s). |
| 295 * @param {string} metric The metric to stop displaying. | 622 * @param {string} metric The metric to stop displaying. |
| 296 */ | 623 */ |
| 297 dropMetric: function(metric) { | 624 dropMetric: function(metric) { |
| 298 var metricValue = this.metricMap_[metric]; | 625 var details = this.metricDetailsMap_[metric]; |
| 299 var affectedCharts = metricValue.divs; | 626 var affectedCharts = details.divs; |
| 300 metricValue.divs = []; | |
| 301 | 627 |
| 302 affectedCharts.forEach(this.drawChart, this); | 628 details.divs = []; // Gotta do this now to get correct drawChart result. |
| 629 affectedCharts.forEach(function(chart) { |
| 630 chart.refs--; |
| 631 if (chart.refs == 0) |
| 632 chart.hidden = true; |
| 633 this.drawChart(chart); |
| 634 }, this); |
| 635 |
| 303 }, | 636 }, |
| 304 | 637 |
| 305 /** | 638 /** |
| 306 * Request new metric data, assuming the metric table and chart already | 639 * Request new metric data, assuming the metric table and chart already |
| 307 * exist. | 640 * exist. |
| 308 * @param {string} metric The metric for which to get data. | 641 * @param {string} metricDetails The metric details for which to get data. |
| 309 */ | 642 */ |
| 310 refreshMetric: function(metric) { | 643 refreshMetric: function(metricDetails) { |
| 311 var metricValue = this.metricMap_[metric]; | 644 metricDetails.data = null; // Mark metric as awaiting response. |
| 312 | 645 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]); | 646 this.start, this.end, this.range.resolution]); |
| 316 }, | 647 }, |
| 317 | 648 |
| 318 /** | 649 /** |
| 319 * Receive new datapoints for a metric, convert the data to Flot-usable | 650 * Receive new datapoints for a metric, convert the data to Flot-usable |
| 320 * form, and redraw all affected charts. | 651 * form, and redraw all affected charts. |
| 321 * @param {!{ | 652 * @param {!{ |
| 322 * type: number, | 653 * id: number, |
| 323 * points: !Array.<{time: number, value: number}> | 654 * max: number, |
| 324 * }} result An object giving metric ID, and time/value point pairs for | 655 * metrics: !Array.<{time: number, value: number}> |
| 325 * that id. | 656 * }} result An object giving metric ID, max expected value of the data, |
| 657 * and time/value point pairs for that id. |
| 326 */ | 658 */ |
| 327 getMetricCallback: function(result) { | 659 getMetricCallback: function(result) { |
| 328 var metricValue = this.metricMap_[result.metricType]; | 660 |
| 661 var metricDetails = this.metricDetailsMap_[result.metricId]; |
| 329 // Might have been dropped while waiting for data. | 662 // Might have been dropped while waiting for data. |
| 330 if (metricValue.divs.length == 0) | 663 if (metricDetails.divs.length == 0) |
| 331 return; | 664 return; |
| 332 | 665 |
| 333 var series = []; | 666 var series = []; |
| 334 metricValue.data = [series]; // Data ends with current open series. | 667 metricDetails.data = [series]; // Data ends with current open series. |
| 335 | 668 |
| 336 // Traverse the points, and the intervals, in parallel. Both are in | 669 // Traverse the points, and the intervals, in parallel. Both are in |
| 337 // ascending time order. Create a sequence of data "series" (per Flot) | 670 // ascending time order. Create a sequence of data 'series' (per Flot) |
| 338 // arrays, with each series comprising all points within a given interval. | 671 // arrays, with each series comprising all points within a given interval. |
| 339 var interval = this.intervals_[0]; | 672 var interval = this.intervals_[0]; |
| 340 var intervalIndex = 0; | 673 var intervalIndex = 0; |
| 341 var pointIndex = 0; | 674 var pointIndex = 0; |
| 342 while (pointIndex < result.points.length && | 675 while (pointIndex < result.metrics.length && |
| 343 intervalIndex < this.intervals_.length) { | 676 intervalIndex < this.intervals_.length) { |
| 344 var point = result.points[pointIndex++]; | 677 var point = result.metrics[pointIndex++]; |
| 345 while (intervalIndex < this.intervals_.length && | 678 while (intervalIndex < this.intervals_.length && |
| 346 point.time > interval.end) { | 679 point.time > interval.end) { |
| 347 interval = this.intervals_[++intervalIndex]; // Jump to new interval. | 680 interval = this.intervals_[++intervalIndex]; // Jump to new interval. |
| 348 if (series.length > 0) { | 681 if (series.length > 0) { |
| 349 series = []; // Start a new series. | 682 series = []; // Start a new series. |
| 350 metricValue.data.push(series); // Put it on the end of the data. | 683 metricDetails.data.push(series); // Put it on the end of the data. |
| 351 } | 684 } |
| 352 } | 685 } |
| 353 if (intervalIndex < this.intervals_.length && | 686 if (intervalIndex < this.intervals_.length && |
| 354 point.time > interval.start) { | 687 point.time > interval.start) { |
| 355 series.push([point.time - timezoneOffset, point.value]); | 688 series.push([point.time - timezoneOffset_, point.value]); |
| 356 } | 689 } |
| 357 } | 690 } |
| 358 metricValue.divs.forEach(this.drawChart, this); | 691 metricDetails.maxValue = Math.max(metricDetails.maxValue, |
| 692 result.maxValue); |
| 693 |
| 694 metricDetails.divs.forEach(this.drawChart, this); |
| 359 }, | 695 }, |
| 360 | 696 |
| 361 /** | 697 /** |
| 362 * Add a new event to the chart(s). | 698 * Add a new event to the chart(s). |
| 363 * @param {string} eventType The type of event to start displaying. | 699 * @param {string} eventType The type of event to start displaying. |
| 364 */ | 700 */ |
| 365 addEventType: function(eventType) { | 701 addEventType: function(eventType) { |
| 366 // Events show on all charts. | 702 // Events show on all charts. |
| 367 this.eventMap_[eventType].divs = this.charts; | 703 this.eventDetailsMap_[eventType].divs = this.charts_; |
| 368 this.refreshEventType(eventType); | 704 this.refreshEventType(eventType); |
| 369 }, | 705 }, |
| 370 | 706 |
| 371 /* | 707 /* |
| 372 * Remove an event from the chart(s). | 708 * Remove an event from the chart(s). |
| 373 * @param {string} eventType The type of event to stop displaying. | 709 * @param {string} eventType The type of event to stop displaying. |
| 374 */ | 710 */ |
| 375 dropEventType: function(eventType) { | 711 dropEventType: function(eventType) { |
| 376 var eventValue = this.eventMap_[eventType]; | 712 var eventValue = this.eventDetailsMap_[eventType]; |
| 377 var affectedCharts = eventValue.divs; | 713 var affectedCharts = eventValue.divs; |
| 378 eventValue.divs = []; | |
| 379 | 714 |
| 715 eventValue.divs = []; // Gotta do this now for correct drawChart results. |
| 380 affectedCharts.forEach(this.drawChart, this); | 716 affectedCharts.forEach(this.drawChart, this); |
| 381 }, | 717 }, |
| 382 | 718 |
| 383 /** | 719 /** |
| 384 * Request new data for |eventType|, for times in the current range. | 720 * Request new data for |eventType|, for times in the current range. |
| 385 * @param {string} eventType The type of event to get new data for. | 721 * @param {string} eventType The type of event to get new data for. |
| 386 */ | 722 */ |
| 387 refreshEventType: function(eventType) { | 723 refreshEventType: function(eventType) { |
| 388 // Mark eventType as awaiting response. | 724 // Mark eventType as awaiting response. |
| 389 this.eventMap_[eventType].data = null; | 725 this.eventDetailsMap_[eventType].data = null; |
| 390 | 726 |
| 391 chrome.send('getEvents', [eventType, this.start, this.end]); | 727 chrome.send('getEvents', [eventType, this.start, this.end]); |
| 392 }, | 728 }, |
| 393 | 729 |
| 394 /** | 730 /** |
| 395 * Receive new events for |eventType|. If |eventType| has been deselected | 731 * Receive new events for |eventType|. If |eventType| has been deselected |
| 396 * while awaiting webui handler response, do nothing. Otherwise, save the | 732 * while awaiting webui handler response, do nothing. Otherwise, save the |
| 397 * data directly, since events are handled differently than metrics | 733 * data directly, since events are handled differently than metrics |
| 398 * when drawing (no "series"), and redraw all the affected charts. | 734 * when drawing (no 'series'), and redraw all the affected charts. |
| 399 * @param {!{ | 735 * @param {!{ |
| 400 * type: number, | 736 * id: number, |
| 401 * points: !Array.<{time: number, longDescription: string}> | 737 * events: !Array.<{time: number}> |
| 402 * }} result An object giving eventType ID, and time/description pairs for | 738 * }} result An object giving eventType id, and times at which that event |
| 403 * each event of that type in the range requested. | 739 * type occurred in the range requested. Each object in the array may |
| 740 * also have an arbitrary list of properties to be displayed as |
| 741 * a tooltip message for the event. |
| 404 */ | 742 */ |
| 405 getEventsCallback: function(result) { | 743 getEventsCallback: function(result) { |
| 406 var eventValue = this.eventMap_[result.eventType]; | 744 var eventValue = this.eventDetailsMap_[result.eventId]; |
| 407 | 745 |
| 408 if (eventValue.divs.length == 0) | 746 if (eventValue.divs.length == 0) |
| 409 return; | 747 return; |
| 410 | 748 |
| 411 result.points.forEach(function(element) { | 749 result.events.forEach(function(element) { |
| 412 element.time -= timezoneOffset; | 750 element.time -= timezoneOffset_; |
| 413 }); | 751 }); |
| 414 | 752 |
| 415 eventValue.data = result.points; | 753 eventValue.data = result.events; |
| 416 eventValue.divs.forEach(this.drawChart, this); | 754 eventValue.divs.forEach(this.drawChart, this); |
| 417 }, | 755 }, |
| 418 | 756 |
| 419 /** | 757 /** |
| 420 * Return an object containing an array of metrics and another of events | 758 * 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. | 759 * that include |chart| as one of the divs into which they display. |
| 422 * @param {HTMLDivElement} chart The <div> for which to get relevant items. | 760 * @param {HTMLDivElement} chart The <div> for which to get relevant items. |
| 423 * @return {!{metrics: !Array,<Object>, events: !Array.<Object>}} | 761 * @return {!{metrics: !Array,<Object>, events: !Array.<Object>}} |
| 424 * @private | 762 * @private |
| 425 */ | 763 */ |
| 426 getChartData_: function(chart) { | 764 getChartData_: function(chart) { |
| 427 var result = {metrics: [], events: []}; | 765 var result = {metrics: [], events: []}; |
| 428 | 766 |
| 429 for (var metric in this.metricMap_) { | 767 for (var metric in this.metricDetailsMap_) { |
| 430 var metricValue = this.metricMap_[metric]; | 768 var metricDetails = this.metricDetailsMap_[metric]; |
| 431 | 769 |
| 432 if (metricValue.divs.indexOf(chart) != -1) | 770 if (metricDetails.divs.indexOf(chart) != -1) |
| 433 result.metrics.push(metricValue); | 771 result.metrics.push(metricDetails); |
| 434 } | 772 } |
| 435 | 773 |
| 436 for (var eventType in this.eventMap_) { | 774 for (var eventType in this.eventDetailsMap_) { |
| 437 var eventValue = this.eventMap_[eventType]; | 775 var eventValue = this.eventDetailsMap_[eventType]; |
| 438 | 776 |
| 439 // Events post to all divs, if they post to any. | 777 // Events post to all divs, if they post to any. |
| 440 if (eventValue.divs.length > 0) | 778 if (eventValue.divs.length > 0) |
| 441 result.events.push(eventValue); | 779 result.events.push(eventValue); |
| 442 } | 780 } |
| 443 | 781 |
| 444 return result; | 782 return result; |
| 445 }, | 783 }, |
| 446 | 784 |
| 447 /** | 785 /** |
| 448 * Check all entries in an object of the type returned from getChartData, | 786 * 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 | 787 * above, to see if all events and metrics have completed data (none is |
| 450 * awaiting an asynchronous webui handler response to get their current | 788 * awaiting an asynchronous webui handler response to get their current |
| 451 * data). | 789 * data). |
| 452 * @param {!{metrics: !Array,<Object>, events: !Array.<Object>}} chartData | 790 * @param {!{metrics: !Array,<Object>, events: !Array.<Object>}} chartData |
| 453 * The event/metric data to check for readiness. | 791 * The event/metric data to check for readiness. |
| 454 * @return {boolean} Whether data is ready. | 792 * @return {boolean} Whether data is ready. |
| 455 * @private | 793 * @private |
| 456 */ | 794 */ |
| 457 isDataReady_: function(chartData) { | 795 isDataReady_: function(chartData) { |
| 458 for (var i = 0; i < chartData.metrics.length; i++) { | 796 return chartData.metrics.every(function(metric) {return metric.data;}) && |
| 459 if (!chartData.metrics[i].data) | 797 chartData.events.every(function(event) {return event.data;}); |
| 460 return false; | |
| 461 } | |
| 462 | |
| 463 for (var i = 0; i < chartData.events.length; i++) { | |
| 464 if (!chartData.events[i].data) | |
| 465 return false; | |
| 466 } | |
| 467 | |
| 468 return true; | |
| 469 }, | 798 }, |
| 470 | 799 |
| 471 /** | 800 /** |
| 472 * Create and return an array of "markings" (per Flot), representing | 801 * Create and return an array of 'markings' (per Flot), representing |
| 473 * vertical lines at the event time, in the event's color. Also add | 802 * 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 | 803 * (not per Flot) a |popupTitle| property to each, to be used for |
| 475 * creating description boxes. | 804 * labelling description popups. |
| 476 * @param {!Array.<{ | 805 * @param {!Array.<{ |
| 477 * description: string, | 806 * description: string, |
| 478 * color: string, | 807 * color: string, |
| 479 * data: !Array.<{time: number}> | 808 * data: !Array.<{time: number}> |
| 480 * }>} eventValues The events to make markings for. | 809 * }>} eventValues The events to make markings for. |
| 481 * @return {!Array.<{ | 810 * @return {!Array.<{ |
| 482 * color: string, | 811 * color: string, |
| 483 * description: string, | 812 * description: string, |
| 484 * xaxis: {from: number, to: number} | 813 * xaxis: {from: number, to: number} |
| 485 * }>} A marks data structure for Flot to use. | 814 * }>} A marks data structure for Flot to use. |
| 486 * @private | 815 * @private |
| 487 */ | 816 */ |
| 488 getEventMarks_: function(eventValues) { | 817 getEventMarks_: function(eventValues) { |
| 489 var markings = []; | 818 var markings = []; |
| 819 var explanation; |
| 820 var date, hours, minutes; |
| 490 | 821 |
| 491 for (var i = 0; i < eventValues.length; i++) { | 822 eventValues.forEach(function(eventValue) { |
| 492 var eventValue = eventValues[i]; | 823 eventValue.data.forEach(function(point) { |
| 493 for (var d = 0; d < eventValue.data.length; d++) { | 824 if (point.time >= this.start - timezoneOffset_ && |
| 494 var point = eventValue.data[d]; | 825 point.time <= this.end - timezoneOffset_) { |
| 495 if (point.time >= this.start - timezoneOffset && | 826 |
| 496 point.time <= this.end - timezoneOffset) { | 827 // Date wants Zulu time. |
| 828 date = new Date(point.time + timezoneOffset_); |
| 829 hours = date.getHours(); |
| 830 minutes = date.getMinutes(); |
| 831 explanation = eventValue.popupTitle + '\nAt: ' + |
| 832 (date.getMonth() + 1) + '/' + date.getDate() + ' ' + |
| 833 (hours < 10 ? '0' : '') + hours + ':' + |
| 834 (minutes < 10 ? '0' : '') + minutes + '\n'; |
| 835 |
| 836 for (var key in point) { |
| 837 if (key != 'time') { |
| 838 var datum = point[key]; |
| 839 if ('label' in datum && 'value' in datum) |
| 840 explanation = explanation + datum.label + ': ' + |
| 841 datum.value + '\n'; |
| 842 } |
| 843 } |
| 497 markings.push({ | 844 markings.push({ |
| 498 color: eventValue.color, | 845 color: eventValue.color, |
| 499 description: eventValue.description, | 846 popupContent: explanation, |
| 500 xaxis: {from: point.time, to: point.time} | 847 xaxis: {from: point.time, to: point.time} |
| 501 }); | 848 }); |
| 502 } else { | 849 } else { |
| 503 console.log('Event out of time range ' + this.start + ' -> ' + | 850 console.log('Event out of time range ' + this.start + ' -> ' + |
| 504 this.end + ' at: ' + point.time); | 851 this.end + ' at: ' + point.time); |
| 505 } | 852 } |
| 506 } | 853 }, this); |
| 507 } | 854 }, this); |
| 508 | 855 |
| 509 return markings; | 856 return markings; |
| 510 }, | 857 }, |
| 511 | 858 |
| 512 /** | 859 /** |
| 513 * Redraw the chart in div |chart|, *if* all its dependent data is present. | 860 * 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 | 861 * Otherwise simply return, and await another call when all data is |
| 515 * available. | 862 * available. |
| 516 * @param {HTMLDivElement} chart The <div> to redraw. | 863 * @param {HTMLDivElement} chart The <div> to redraw. |
| 517 */ | 864 */ |
| 518 drawChart: function(chart) { | 865 drawChart: function(chart) { |
| 519 var chartData = this.getChartData_(chart); | 866 var chartData = this.getChartData_(chart); |
| 867 var axisMap = {}; // Maps category ids to y-axis numbers |
| 520 | 868 |
| 521 if (!this.isDataReady_(chartData)) | 869 if (chart.hidden || !this.isDataReady_(chartData)) |
| 522 return; | 870 return; |
| 523 | 871 |
| 524 var seriesSeq = []; | 872 var seriesSeq = []; |
| 525 var yAxes = []; | 873 var yAxes = []; |
| 526 chartData.metrics.forEach(function(value) { | 874 chartData.metrics.forEach(function(metricDetails) { |
| 527 yAxes.push(value.yAxis); | 875 var metricCategory = metricDetails.category; |
| 528 for (var i = 0; i < value.data.length; i++) { | 876 var yAxisNumber = axisMap[metricCategory.metricCategoryId]; |
| 877 |
| 878 // Add a new y-axis if we are encountering this category of metric |
| 879 // for the first time. Otherwise, update the existing y-axis with |
| 880 // a new max value if needed. (Presently, we expect only one category |
| 881 // of metric per chart, but this design permits more in the future.) |
| 882 if (yAxisNumber === undefined) { |
| 883 yAxes.push({min: 0, max: metricDetails.maxValue * yAxisMargin_, |
| 884 labelWidth: 60}); |
| 885 axisMap[metricCategory.metricCategoryId] = yAxisNumber = yAxes.length; |
| 886 } else { |
| 887 yAxes[yAxisNumber - 1].max = Math.max(yAxes[yAxisNumber - 1].max, |
| 888 metricDetails.maxValue * yAxisMargin_); |
| 889 } |
| 890 |
| 891 for (var i = 0; i < metricDetails.data.length; i++) { |
| 529 seriesSeq.push({ | 892 seriesSeq.push({ |
| 530 color: value.yAxis.color, | 893 color: metricDetails.color, |
| 531 data: value.data[i], | 894 data: metricDetails.data[i], |
| 532 label: i == 0 ? value.description + ' (' + value.units + ')' : null, | 895 label: i == 0 ? metricDetails.name + |
| 533 yaxis: yAxes.length, // Use just-added Y axis. | 896 ' (' + metricCategory.unit + ')' : null, |
| 897 yaxis: yAxisNumber |
| 534 }); | 898 }); |
| 535 } | 899 } |
| 536 }); | 900 }); |
| 537 | 901 |
| 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); | 902 var markings = this.getEventMarks_(chartData.events); |
| 543 var chart = this.charts[0]; | |
| 544 var plot = $.plot(chart, seriesSeq, { | 903 var plot = $.plot(chart, seriesSeq, { |
| 545 yaxes: yAxes, | 904 yaxes: yAxes, |
| 546 xaxis: { | 905 xaxis: { |
| 547 mode: 'time', | 906 mode: 'time', |
| 548 min: this.start - timezoneOffset, | 907 min: this.start - timezoneOffset_, |
| 549 max: this.end - timezoneOffset | 908 max: this.end - timezoneOffset_ |
| 550 }, | 909 }, |
| 551 points: {show: true, radius: 1}, | 910 points: {show: true, radius: 1}, |
| 552 lines: {show: true}, | 911 lines: {show: true}, |
| 553 grid: {markings: markings} | 912 grid: {markings: markings, hoverable: true, autoHighlight: true} |
| 554 }); | 913 }); |
| 555 | 914 |
| 556 // For each event in |markings|, create also a label div, with left | 915 // For each event in |markings|, create also a label div, with left |
| 557 // edge colinear with the event vertical line. Top of label is | 916 // edge colinear with the event vertical line. Top of label is |
| 558 // presently a hack-in, putting labels in three tiers of 25px height | 917 // presently a hack-in, putting labels in three tiers of 25px height |
| 559 // each to avoid overlap. Will need something better. | 918 // each to avoid overlap. Will need something better. |
| 560 var labelTemplate = $('#label-template')[0]; | 919 var labelTemplate = $('#label-template')[0]; |
| 561 for (var i = 0; i < markings.length; i++) { | 920 for (var i = 0; i < markings.length; i++) { |
| 562 var mark = markings[i]; | 921 var mark = markings[i]; |
| 563 var point = | 922 var point = |
| 564 plot.pointOffset({x: mark.xaxis.to, y: yAxes[0].max, yaxis: 1}); | 923 plot.pointOffset({x: mark.xaxis.to, y: yAxes[0].max, yaxis: 1}); |
| 565 var labelDiv = labelTemplate.cloneNode(true); | 924 var labelDiv = labelTemplate.cloneNode(true); |
| 566 labelDiv.innerText = mark.description; | 925 labelDiv.innerText = mark.popupContent; |
| 567 labelDiv.style.left = point.left + 'px'; | 926 labelDiv.style.left = point.left + 'px'; |
| 568 labelDiv.style.top = (point.top + 25 * (i % 3)) + 'px'; | 927 labelDiv.style.top = (point.top + 100 * (i % 3)) + 'px'; |
| 569 | 928 |
| 570 chart.appendChild(labelDiv); | 929 chart.appendChild(labelDiv); |
| 930 labelDiv.hidden = true; |
| 931 chart.hovers.push({x: mark.xaxis.to, div: labelDiv}); |
| 571 } | 932 } |
| 572 } | 933 } |
| 573 }; | 934 }; |
| 574 return { | 935 return { |
| 575 PerformanceMonitor: PerformanceMonitor | 936 PerformanceMonitor: PerformanceMonitor |
| 576 }; | 937 }; |
| 577 }); | 938 }); |
| 578 | 939 |
| 579 var PerformanceMonitor = new performance_monitor.PerformanceMonitor(); | 940 var PerformanceMonitor = new performance_monitor.PerformanceMonitor(); |
| OLD | NEW |