Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 1 | /*global d3*/ |
| 2 | |
| 3 | function createTopologyView() { |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 4 | return d3.select('#svg-container').append('svg:svg').attr('viewBox', '0 0 1000 1000').attr('preserveAspectRatio', 'none'). |
| 5 | attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 6 | } |
| 7 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 8 | function drawHeader(model) { |
| 9 | d3.select('#lastUpdate').text(model.timestamp); |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 10 | d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length); |
| 11 | d3.select('#activeFlows').text(model.flows.length); |
| 12 | } |
| 13 | |
| 14 | function toRadians (angle) { |
| 15 | return angle * (Math.PI / 180); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | function drawTopology(svg, model) { |
| 19 | |
| 20 | var rings = [{ |
| 21 | radius: 3, |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame^] | 22 | width: 4, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 23 | switches: model.edgeSwitches, |
| 24 | className: 'edge' |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 25 | }, { |
| 26 | radius: 1.5, |
| 27 | width: 32, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 28 | switches: model.aggregationSwitches, |
| 29 | className: 'aggregation' |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 30 | }, { |
| 31 | radius: 1, |
| 32 | width: 32, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 33 | switches: model.coreSwitches, |
| 34 | className: 'core' |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 35 | }]; |
| 36 | |
| 37 | function ringEnter(data, i) { |
| 38 | if (!data.switches.length) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | var k = 360 / data.switches.length; |
| 43 | |
| 44 | d3.select(this).selectAll("g") |
| 45 | .data(d3.range(data.switches.length).map(function() { |
| 46 | return data; |
| 47 | })) |
| 48 | .enter().append("svg:g") |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame^] | 49 | .attr("id", function (_, i) { |
| 50 | return data.className + i; |
| 51 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 52 | .attr("transform", function(_, i) { |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 53 | return "rotate(" + i * k + ")translate(" + data.radius * 150 + ")rotate(" + (-i * k) + ")"; |
| 54 | }) |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 55 | .append("svg:circle") |
| 56 | .attr('class', data.className) |
| 57 | .attr("transform", function(_, i) { |
| 58 | var m = document.querySelector('svg').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 59 | if (data.scale) { |
| 60 | m = m.scale(data.scale); |
| 61 | } |
| 62 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 63 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 64 | .attr("x", -data.width / 2) |
| 65 | .attr("y", -data.width / 2) |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 66 | // .attr("width", data.width) |
| 67 | // .attr("height", data.width) |
| 68 | .attr("r", data.width) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | var ring = svg.selectAll("g") |
| 72 | .data(rings) |
| 73 | .enter().append("svg:g") |
| 74 | .attr("class", "ring") |
| 75 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 76 | |
| 77 | function zoom(d, i) { |
| 78 | model.edgeSwitches.forEach(function (s) { |
| 79 | s.scale = 1; |
| 80 | }); |
| 81 | d.scale = 2; |
| 82 | |
| 83 | svg.selectAll('.edge').data(model.edgeSwitches).transition().duration(100) |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame^] | 84 | // .attr("transform", function(data, i) { |
| 85 | // var m = document.querySelector('svg').getTransformToElement().inverse(); |
| 86 | // m = m.scale(data.scale); |
| 87 | // return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
| 88 | // }) |
| 89 | .attr("r", function (data, i) { |
| 90 | return rings[0].width * data.scale; |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 91 | }) |
| 92 | } |
| 93 | |
| 94 | svg.selectAll('.edge').on('mouseover', zoom); |
| 95 | svg.selectAll('.edge').on('mousedown', zoom); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | function sync(svg) { |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 99 | updateModel(function (model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 100 | |
| 101 | drawHeader(model); |
| 102 | drawTopology(svg, model); |
| 103 | |
| 104 | // do it again in 1s |
| 105 | setTimeout(function () { |
| 106 | sync(svg) |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame^] | 107 | }, 5000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 108 | }); |
| 109 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 110 | |
| 111 | sync(createTopologyView()); |