blob: 9d95f6e2a6b3beb5ce88cc009731b78da6dca4ba [file] [log] [blame]
Paul Greyson740bdaf2013-03-18 16:10:48 -07001/*global d3*/
2
3function createTopologyView() {
Paul Greyson952ccb62013-03-18 22:22:08 -07004 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 Greyson740bdaf2013-03-18 16:10:48 -07006}
7
Paul Greyson740bdaf2013-03-18 16:10:48 -07008function drawHeader(model) {
9 d3.select('#lastUpdate').text(model.timestamp);
Paul Greyson952ccb62013-03-18 22:22:08 -070010 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
11 d3.select('#activeFlows').text(model.flows.length);
12}
13
14function toRadians (angle) {
15 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -070016}
17
18function drawTopology(svg, model) {
19
20 var rings = [{
21 radius: 3,
Paul Greyson952ccb62013-03-18 22:22:08 -070022 width: 8,
23 switches: model.edgeSwitches,
24 className: 'edge'
Paul Greyson740bdaf2013-03-18 16:10:48 -070025 }, {
26 radius: 1.5,
27 width: 32,
Paul Greyson952ccb62013-03-18 22:22:08 -070028 switches: model.aggregationSwitches,
29 className: 'aggregation'
Paul Greyson740bdaf2013-03-18 16:10:48 -070030 }, {
31 radius: 1,
32 width: 32,
Paul Greyson952ccb62013-03-18 22:22:08 -070033 switches: model.coreSwitches,
34 className: 'core'
Paul Greyson740bdaf2013-03-18 16:10:48 -070035 }];
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")
49 .attr("class", "square")
50 .attr("transform", function(_, i) {
Paul Greysonf8f43172013-03-18 23:00:30 -070051 return "rotate(" + i * k + ")translate(" + data.radius * 150 + ")rotate(" + (-i * k) + ")";
52 })
Paul Greyson952ccb62013-03-18 22:22:08 -070053 .append("svg:circle")
54 .attr('class', data.className)
55 .attr("transform", function(_, i) {
56 var m = document.querySelector('svg').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -070057 if (data.scale) {
58 m = m.scale(data.scale);
59 }
60 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -070061 })
Paul Greyson740bdaf2013-03-18 16:10:48 -070062 .attr("x", -data.width / 2)
63 .attr("y", -data.width / 2)
Paul Greysonf8f43172013-03-18 23:00:30 -070064 // .attr("width", data.width)
65 // .attr("height", data.width)
66 .attr("r", data.width)
Paul Greyson740bdaf2013-03-18 16:10:48 -070067 }
68
69 var ring = svg.selectAll("g")
70 .data(rings)
71 .enter().append("svg:g")
72 .attr("class", "ring")
73 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -070074
75 function zoom(d, i) {
76 model.edgeSwitches.forEach(function (s) {
77 s.scale = 1;
78 });
79 d.scale = 2;
80
81 svg.selectAll('.edge').data(model.edgeSwitches).transition().duration(100)
82 .attr("transform", function(data, i) {
83 var m = document.querySelector('svg').getTransformToElement().inverse();
84 m = m.scale(data.scale);
85 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
86 })
87 }
88
89 svg.selectAll('.edge').on('mouseover', zoom);
90 svg.selectAll('.edge').on('mousedown', zoom);
Paul Greyson740bdaf2013-03-18 16:10:48 -070091}
92
93function sync(svg) {
Paul Greyson6f86d1e2013-03-18 14:40:39 -070094 updateModel(function (model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -070095
96 drawHeader(model);
97 drawTopology(svg, model);
98
99 // do it again in 1s
100 setTimeout(function () {
101 sync(svg)
102 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700103 });
104}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700105
106sync(createTopologyView());