|
| 1 | +/** |
| 2 | + * @license Copyright 2017 Google Inc. All Rights Reserved. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 4 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
| 5 | + */ |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +/* global Plotly, dashboardResults */ |
| 9 | +/* eslint-env browser */ |
| 10 | + |
| 11 | +class Dashboard { |
| 12 | + constructor(metrics, charts) { |
| 13 | + this._charts = charts; |
| 14 | + this._currentMetric = metrics[0]; |
| 15 | + this._numberOfBatchesToShow = 0; |
| 16 | + this._initializeSelectMetricControl(metrics); |
| 17 | + this._initializeSelectNumberOfBatchesToShow(); |
| 18 | + } |
| 19 | + |
| 20 | + render() { |
| 21 | + this._charts.render(this._currentMetric, this._numberOfBatchesToShow); |
| 22 | + } |
| 23 | + |
| 24 | + _initializeSelectMetricControl(metrics) { |
| 25 | + const metricsControl = document.getElementById('select-metric'); |
| 26 | + for (const metric of metrics) { |
| 27 | + const option = document.createElement('option'); |
| 28 | + option.label = metric; |
| 29 | + option.value = metric; |
| 30 | + metricsControl.appendChild(option); |
| 31 | + } |
| 32 | + metricsControl.addEventListener('change', e => this._onSelectMetric(e), false); |
| 33 | + } |
| 34 | + |
| 35 | + _onSelectMetric(event) { |
| 36 | + this._currentMetric = event.target.value; |
| 37 | + this.render(); |
| 38 | + } |
| 39 | + |
| 40 | + _initializeSelectNumberOfBatchesToShow() { |
| 41 | + const control = document.getElementById('select-number-of-batches'); |
| 42 | + control.addEventListener('change', e => this._onSelectNumberOfBatchesToShow(e), false); |
| 43 | + } |
| 44 | + |
| 45 | + _onSelectNumberOfBatchesToShow(event) { |
| 46 | + if (event.target.value === 'all') { |
| 47 | + this._numberOfBatchesToShow = 0; |
| 48 | + } else { |
| 49 | + this._numberOfBatchesToShow = parseInt(event.target.value, 10); |
| 50 | + } |
| 51 | + this.render(); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +class Charts { |
| 56 | + constructor(renderingScheduler) { |
| 57 | + this._renderingScheduler = renderingScheduler; |
| 58 | + this._elementId = 1; |
| 59 | + this._layout = { |
| 60 | + width: 400, |
| 61 | + height: 300, |
| 62 | + xaxis: { |
| 63 | + showgrid: false, |
| 64 | + zeroline: false, |
| 65 | + tickangle: 60, |
| 66 | + showticklabels: false |
| 67 | + }, |
| 68 | + yaxis: { |
| 69 | + zeroline: true, |
| 70 | + rangemode: 'tozero' |
| 71 | + }, |
| 72 | + showlegend: false, |
| 73 | + titlefont: { |
| 74 | + family: `"Roboto", -apple-system, BlinkMacSystemFont, sans-serif`, |
| 75 | + size: 14 |
| 76 | + } |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + render(currentMetric, numberOfBatchesToShow) { |
| 81 | + Utils.removeChildren(document.getElementById('charts')); |
| 82 | + for (const [metricName, site] of Object.entries(dashboardResults[currentMetric])) { |
| 83 | + const percentiles = Object.entries(site) |
| 84 | + .map(([batchName, batch]) => { |
| 85 | + return { |
| 86 | + x: batchName, |
| 87 | + higher: Utils.calculatePercentile(batch.map(metric => metric.timing), 0.8), |
| 88 | + median: Utils.calculatePercentile(batch.map(metric => metric.timing), 0.5), |
| 89 | + lower: Utils.calculatePercentile(batch.map(metric => metric.timing), 0.2) |
| 90 | + }; |
| 91 | + }) |
| 92 | + .slice(-1 * numberOfBatchesToShow); |
| 93 | + |
| 94 | + const median = { |
| 95 | + x: percentiles.map(r => r.x), |
| 96 | + y: percentiles.map(r => r.median), |
| 97 | + type: 'scatter', |
| 98 | + mode: 'line', |
| 99 | + name: 'median' |
| 100 | + }; |
| 101 | + |
| 102 | + const errorBands = { |
| 103 | + x: percentiles.map(r => r.x).concat(percentiles.map(r => r.x).reverse()), |
| 104 | + y: percentiles.map(r => r.higher).concat(percentiles.map(r => r.lower).reverse()), |
| 105 | + fill: 'toself', |
| 106 | + fillcolor: 'rgba(0,176,246,0.2)', |
| 107 | + line: {color: 'transparent'}, |
| 108 | + name: 'error bands', |
| 109 | + showlegend: false, |
| 110 | + type: 'scatter' |
| 111 | + }; |
| 112 | + this._renderPreviewChart([median, errorBands], metricName); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + _renderPreviewChart(data, title) { |
| 117 | + this._renderingScheduler.enqueue(_ => { |
| 118 | + Plotly.newPlot( |
| 119 | + this._createPreviewChartElement(data, title), |
| 120 | + data, |
| 121 | + Object.assign({title}, this._layout) |
| 122 | + ); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + _createPreviewChartElement(data, title) { |
| 127 | + const chart = document.createElement('div'); |
| 128 | + chart.style = 'display: inline-block; position: relative'; |
| 129 | + chart.id = 'chart' + this._elementId++; |
| 130 | + |
| 131 | + const button = document.createElement('button'); |
| 132 | + button.className = 'dth-button show-bigger-button'; |
| 133 | + button.appendChild(document.createTextNode('Focus')); |
| 134 | + button.addEventListener('click', () => this._onFocusChart(data, title), false); |
| 135 | + chart.appendChild(button); |
| 136 | + |
| 137 | + const container = document.getElementById('charts'); |
| 138 | + container.appendChild(chart); |
| 139 | + return chart.id; |
| 140 | + } |
| 141 | + |
| 142 | + _onFocusChart(data, title) { |
| 143 | + const overlay = document.createElement('div'); |
| 144 | + overlay.id = 'overlay'; |
| 145 | + document.body.appendChild(overlay); |
| 146 | + |
| 147 | + document.getElementById('charts').style.display = 'none'; |
| 148 | + |
| 149 | + const closeButton = document.createElement('button'); |
| 150 | + closeButton.className = 'dth-button close-button'; |
| 151 | + closeButton.appendChild(document.createTextNode('Close')); |
| 152 | + closeButton.addEventListener('click', onCloseFocusedChart, false); |
| 153 | + overlay.appendChild(closeButton); |
| 154 | + |
| 155 | + const chart = document.createElement('div'); |
| 156 | + chart.className = 'chart'; |
| 157 | + overlay.appendChild(chart); |
| 158 | + this._renderFocusedChart(data, title, chart); |
| 159 | + |
| 160 | + function onCloseFocusedChart() { |
| 161 | + document.getElementById('charts').style.display = 'block'; |
| 162 | + document.body.removeChild(overlay); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + _renderFocusedChart(data, title, element) { |
| 167 | + Plotly.newPlot( |
| 168 | + element, |
| 169 | + data, |
| 170 | + Object.assign({title}, this._layout, { |
| 171 | + width: document.body.clientWidth - 100, |
| 172 | + height: 500 |
| 173 | + }) |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +class RenderingScheduler { |
| 179 | + constructor() { |
| 180 | + this._queue = []; |
| 181 | + } |
| 182 | + |
| 183 | + enqueue(fn) { |
| 184 | + const isFirst = this._queue.length == 0; |
| 185 | + this._queue.push(fn); |
| 186 | + if (isFirst) { |
| 187 | + this._render(); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + _render() { |
| 192 | + window.requestAnimationFrame(_ => { |
| 193 | + const plotFn = this._queue.shift(); |
| 194 | + if (plotFn) { |
| 195 | + plotFn(); |
| 196 | + this._render(); |
| 197 | + } |
| 198 | + }); |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +const Utils = { |
| 203 | + /** |
| 204 | + * @param {!Element} parent |
| 205 | + */ |
| 206 | + removeChildren(parent) { |
| 207 | + while (parent.firstChild) { |
| 208 | + parent.removeChild(parent.firstChild); |
| 209 | + } |
| 210 | + }, |
| 211 | + |
| 212 | + /** |
| 213 | + * Calculate the value at a given percentile |
| 214 | + * Based on: https://gist.github.com/IceCreamYou/6ffa1b18c4c8f6aeaad2 |
| 215 | + * @param {!Array<number>} array |
| 216 | + * @param {number} percentile should be from 0 to 1 |
| 217 | + */ |
| 218 | + calculatePercentile(array, percentile) { |
| 219 | + const sorted = array.filter(x => x !== null).sort((a, b) => a - b); |
| 220 | + |
| 221 | + if (sorted.length === 0) { |
| 222 | + return 0; |
| 223 | + } |
| 224 | + if (sorted.length === 1 || percentile <= 0) { |
| 225 | + return sorted[0]; |
| 226 | + } |
| 227 | + if (percentile >= 1) { |
| 228 | + return sorted[sorted.length - 1]; |
| 229 | + } |
| 230 | + |
| 231 | + const index = (sorted.length - 1) * percentile; |
| 232 | + const lower = Math.floor(index); |
| 233 | + const upper = lower + 1; |
| 234 | + const weight = index % 1; |
| 235 | + |
| 236 | + return sorted[lower] * (1 - weight) + sorted[upper] * weight; |
| 237 | + } |
| 238 | +}; |
| 239 | + |
| 240 | +function main() { |
| 241 | + /** |
| 242 | + * Navigation Start is usually not a very informative metric. |
| 243 | + */ |
| 244 | + const metrics = Object.keys(dashboardResults).filter(m => m !== 'Navigation Start'); |
| 245 | + |
| 246 | + const renderingScheduler = new RenderingScheduler(); |
| 247 | + const charts = new Charts(renderingScheduler); |
| 248 | + const dashboard = new Dashboard(metrics, charts); |
| 249 | + dashboard.render(); |
| 250 | +} |
| 251 | + |
| 252 | +main(); |
0 commit comments