Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | ONOS GUI -- Intent Performance View Module |
| 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
| 23 | // injected refs |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 24 | var $log, tbs, ts, wss, sus, flash, fs, mast; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 25 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 26 | // internal state |
| 27 | var handlerMap, |
| 28 | openListener, |
| 29 | theSample = [], |
| 30 | graph; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 31 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 32 | // ========================== |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 33 | |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 34 | function createGraph(h, samples) { |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 35 | var stopped = false, |
| 36 | n = 243, |
| 37 | duration = 750, |
| 38 | now = new Date(Date.now() - duration), |
| 39 | headers = h, |
| 40 | data = []; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 41 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 42 | var dim = fs.windowSize(mast.mastHeight()); |
| 43 | var margin, width, height, x, y; |
| 44 | var svg, axis; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 45 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 46 | var lines = [], |
| 47 | paths = []; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 48 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 49 | var transition = d3.select({}).transition() |
| 50 | .duration(duration) |
| 51 | .ease("linear"); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 52 | |
Thomas Vachuska | 72dd99d | 2015-03-27 18:41:18 -0700 | [diff] [blame] | 53 | svg = d3.select("#intent-perf-chart").append("p").append("svg") |
| 54 | .attr("id", "intent-perf-svg") |
| 55 | .append("g") |
| 56 | .attr("id", "intent-perf-svg-g"); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 57 | |
Thomas Vachuska | 72dd99d | 2015-03-27 18:41:18 -0700 | [diff] [blame] | 58 | svg.append("defs").append("clipPath") |
| 59 | .attr("id", "intent-perf-clip") |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 60 | .append("rect"); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 61 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 62 | axis = svg.append("g") |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 63 | .attr("class", "x axis") |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 64 | .attr("id", "intent-perf-x"); |
| 65 | |
| 66 | svg.append("g").attr("class", "y axis") |
| 67 | .attr("id", "intent-perf-yl"); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 68 | |
| 69 | svg.append("g") |
| 70 | .attr("class", "y axis") |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 71 | .attr("id", "intent-perf-yr"); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 72 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 73 | resize(dim); |
| 74 | |
| 75 | headers.forEach(function (h, li) { |
Thomas Vachuska | 90c7be2 | 2015-03-27 14:12:46 -0700 | [diff] [blame] | 76 | // Prime the data to match the headers and zero it out. |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 77 | data[li] = d3.range(n).map(function() { return 0 }); |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 78 | |
| 79 | if (li < headers.length - 1) { |
| 80 | samples.forEach(function (s, i) { |
| 81 | var di = dataIndex(s.time); |
| 82 | if (di >= 0) { |
| 83 | data[li][di] = s.data[li]; |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | data[li].forEach(function (d, i) { |
| 88 | if (!d && i > 0) { |
| 89 | data[li][i] = data[li][i - 1]; |
| 90 | } |
| 91 | }); |
| 92 | } else { |
| 93 | data[li].forEach(function (t, i) { |
| 94 | for (var si = 0; si < headers.length - 1; si++) { |
| 95 | data[li][i] = data[si][i]; |
| 96 | } |
| 97 | }); |
| 98 | } |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 99 | |
| 100 | // Create the lines |
| 101 | lines[li] = d3.svg.line() |
| 102 | .interpolate("basis") |
| 103 | .x(function(d, i) { return x(now - (n - 1 - i) * duration); }) |
| 104 | .y(function(d, i) { return y(d); }); |
| 105 | |
| 106 | // Create the SVG paths |
| 107 | paths[li] = svg.append("g") |
| 108 | .attr("clip-path", "url(#intent-perf-clip)") |
| 109 | .append("path") |
| 110 | .datum(function () { return data[li]; }) |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 111 | .attr("id", "line" + li); |
| 112 | |
| 113 | if (li < headers.length - 1) { |
| 114 | paths[li].attr("class", "line").style("stroke", lineColor(li)); |
| 115 | } else { |
| 116 | paths[li].attr("class", "lineTotal"); |
| 117 | } |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 118 | }); |
| 119 | |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 120 | function dataIndex(time) { |
| 121 | var delta = now.getTime() - time; |
| 122 | var di = Math.round(n - 2 - (delta / duration)); |
| 123 | // $log.info('now=' + now.getTime() + '; then=' + time + '; delta=' + delta + '; di=' + di + ';'); |
| 124 | return di >= n || di < 0 ? -1 : di; |
| 125 | } |
| 126 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 127 | function lineColor(li) { |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 128 | return sus.cat7().getColor(li, false, ts.theme()); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 131 | function tick() { |
| 132 | if (stopped) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | transition = transition.each(function() { |
| 137 | // update the domains |
| 138 | now = new Date(); |
| 139 | x.domain([now - (n - 2) * duration, now - duration]); |
| 140 | |
| 141 | data.forEach(function (d, li) { |
| 142 | // push the new most recent sample onto the back |
| 143 | d.push(theSample[li]); |
| 144 | |
| 145 | // redraw the line and slide it left |
| 146 | paths[li].attr("d", lines[li]).attr("transform", null); |
Thomas Vachuska | 90c7be2 | 2015-03-27 14:12:46 -0700 | [diff] [blame] | 147 | paths[li].transition() |
| 148 | .attr("transform", "translate(" + x(now - (n - 1) * duration) + ")"); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 149 | |
| 150 | // pop the old data point off the front |
| 151 | d.shift(); |
| 152 | }); |
| 153 | |
| 154 | // slide the x-axis left |
| 155 | axis.call(x.axis); |
| 156 | }).transition().each("start", tick); |
| 157 | } |
| 158 | |
| 159 | function start() { |
| 160 | stopped = false; |
| 161 | headers.forEach(function (h, li) { |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 162 | theSample[li] = data[li][n-1]; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 163 | }); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 164 | tick(); |
| 165 | } |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 166 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 167 | function stop() { |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 168 | headers.forEach(function (h, li) { |
| 169 | theSample[li] = 0; |
| 170 | }); |
| 171 | // Schedule delayed stop to allow 0s to render. |
| 172 | setTimeout(function () { stopped = true; }, 1000); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 173 | } |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 174 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 175 | function resize(dim) { |
| 176 | margin = {top: 20, right: 90, bottom: 20, left: 70}; |
| 177 | width = dim.width - margin.right - margin.left; |
| 178 | height = 480 - margin.top - margin.bottom; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 179 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 180 | x = d3.time.scale() |
| 181 | .domain([now - (n - 2) * duration, now - duration]) |
| 182 | .range([0, width]); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 183 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 184 | y = d3.scale.linear() |
| 185 | .domain([0, 200000]) |
| 186 | .range([height, 0]); |
| 187 | |
| 188 | d3.select("#intent-perf-svg") |
| 189 | .attr("width", width + margin.left + margin.right) |
| 190 | .attr("height", height + margin.top + margin.bottom); |
| 191 | d3.select("#intent-perf-svg-g") |
| 192 | .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
| 193 | |
Thomas Vachuska | 72dd99d | 2015-03-27 18:41:18 -0700 | [diff] [blame] | 194 | d3.select("#intent-perf-clip rect") |
| 195 | .attr("width", width) |
| 196 | .attr("height", height); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 197 | |
| 198 | d3.select("#intent-perf-x") |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 199 | .attr("transform", "translate(0," + height + ")") |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 200 | .call(x.axis = d3.svg.axis().scale(x).orient("bottom")); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 201 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 202 | d3.select("#intent-perf-yl") |
| 203 | .call(d3.svg.axis().scale(y).orient("left")) |
| 204 | d3.select("#intent-perf-yr") |
| 205 | .attr("transform", "translate(" + width + " ,0)") |
| 206 | .call(d3.svg.axis().scale(y).orient("right")) |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 207 | } |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 208 | |
| 209 | return { |
| 210 | start: start, |
| 211 | stop: stop, |
| 212 | resize: resize |
| 213 | }; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 216 | |
| 217 | function wsOpen(host, url) { |
| 218 | $log.debug('IntentPerf: web socket open - cluster node:', host, 'URL:', url); |
| 219 | // Request batch of initial data from the new server |
| 220 | wss.sendEvent('intentPerfStart'); |
| 221 | } |
| 222 | |
| 223 | function createAndInitGraph(d) { |
| 224 | if (!graph) { |
| 225 | d.headers.push("total"); |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 226 | graph = createGraph(d.headers, d.samples); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 227 | } |
| 228 | graph.start(); |
| 229 | } |
| 230 | |
| 231 | function graphResized(dim) { |
| 232 | $log.info("Resized: " + dim.width + "x" + dim.height); |
Thomas Vachuska | 72dd99d | 2015-03-27 18:41:18 -0700 | [diff] [blame] | 233 | if (graph) { |
| 234 | graph.resize(dim); |
| 235 | } |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | function recordSample(sample) { |
| 239 | var total = 0; |
| 240 | sample.data.forEach(function (d, i) { |
| 241 | theSample[i] = d; |
| 242 | total = total + d; |
| 243 | }); |
| 244 | theSample[sample.data.length] = total; |
| 245 | } |
| 246 | |
| 247 | function createHandlerMap() { |
| 248 | handlerMap = { |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 249 | intentPerfInit: createAndInitGraph, |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 250 | intentPerfSample: recordSample |
| 251 | }; |
| 252 | } |
| 253 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 254 | // define the controller |
| 255 | |
| 256 | angular.module('ovIntentPerf', ['onosUtil']) |
| 257 | .controller('OvIntentPerfCtrl', |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 258 | ['$scope', '$log', 'ToolbarService', 'WebSocketService', |
| 259 | 'ThemeService', 'FlashService', 'SvgUtilService', 'FnService', |
| 260 | 'MastService', |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 261 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 262 | function ($scope, _$log_, _tbs_, _wss_, _ts_, _flash_, _sus_, _fs_, _mast_) { |
| 263 | var self = this; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 264 | |
| 265 | $log = _$log_; |
| 266 | tbs = _tbs_; |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 267 | wss = _wss_; |
| 268 | ts = _ts_; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 269 | flash = _flash_; |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 270 | sus = _sus_; |
| 271 | fs = _fs_; |
| 272 | mast = _mast_; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 273 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 274 | createHandlerMap(); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 275 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 276 | self.notifyResize = function () { |
| 277 | graphResized(fs.windowSize(mast.mastHeight())); |
| 278 | }; |
| 279 | |
| 280 | function start() { |
| 281 | openListener = wss.addOpenListener(wsOpen); |
| 282 | wss.bindHandlers(handlerMap); |
| 283 | wss.sendEvent('intentPerfStart'); |
| 284 | $log.debug('intentPerf comms started'); |
| 285 | } |
| 286 | |
| 287 | function stop() { |
| 288 | graph.stop(); |
| 289 | wss.sendEvent('intentPerfStop'); |
| 290 | wss.unbindHandlers(handlerMap); |
| 291 | wss.removeOpenListener(openListener); |
| 292 | openListener = null; |
| 293 | graph = null; |
| 294 | $log.debug('intentPerf comms stopped'); |
| 295 | } |
| 296 | |
| 297 | // Cleanup on destroyed scope.. |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 298 | $scope.$on('$destroy', function () { |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 299 | $log.log('OvIntentPerfCtrl is saying Buh-Bye!'); |
| 300 | stop(); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 301 | }); |
| 302 | |
Thomas Vachuska | 90c7be2 | 2015-03-27 14:12:46 -0700 | [diff] [blame] | 303 | $log.log('OvIntentPerfCtrl has been created'); |
| 304 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 305 | start(); |
| 306 | }]); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 307 | }()); |