blob: a5d84ccd6380e61b22d1a04981a14464bf65cbde [file] [log] [blame]
Ubuntu82b8a832013-02-06 22:00:11 +00001function gui(data_source){
2 var width = 960,
3 height = 500;
4 var color = d3.scale.category20();
5
6 var svg = d3.select("body").append("svg:svg")
7 .attr("width", width)
8 .attr("height", height);
9
10 var force = d3.layout.force()
11 .charge(-500)
12 .linkDistance(100)
13 .size([width, height]);
14
15 var path = svg.selectAll("path");
16 var circle = svg.selectAll("circle");
17 var text = svg.selectAll("g");
18
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +000019 d3.json(data_source, init);
Ubuntu82b8a832013-02-06 22:00:11 +000020
21 function init(json){
22 nodes = force.nodes();
23 links = force.links();
24
25 json.nodes.forEach(function(item) {
26 nodes.push(item);
27 });
28 json.links.forEach(function(item) {
29 links.push(item);
30 });
31 draw(nodes, links);
32 }
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +000033 function gone_links (json, links, gone) {
34 for (var i = 0; i < links.length ; i ++){
35 var found = 0;
36 for (var j = 0; j < json.links.length ; j ++){
37 if (links[i].source.name == json.nodes[json.links[j].source].name &&
38 links[i].target.name == json.nodes[json.links[j].target].name ){
39 found = 1;
40 break;
41 }
42 }
43 if ( found == 0 ){
44 gone.push(links[i]);
45 }
46 }
47 return gone;
48 }
49 function added_links (json, links, added) {
50 for (var j = 0; j < json.links.length ; j ++){
51 var found = 0;
52 for (var i = 0; i < links.length ; i ++){
53 if (links[i].source.name == json.nodes[json.links[j].source].name &&
54 links[i].target.name == json.nodes[json.links[j].target].name ){
55 found = 1;
56 break;
57 }
58 }
59 if ( found == 0 ){
60 added.push(json.links[j]);
61 }
62 }
63 return added;
64 }
Ubuntu82b8a832013-02-06 22:00:11 +000065
66 function update(json) {
67 Array.prototype.diff2 = function(arr) {
68 return this.filter(function(i) {
69 for (var j = 0; j < arr.length ; j++) {
70 if (arr[j].source === i.source.index &&
71 arr[j].target === i.target.index)
72 return false;
73 }
74 return true;
75 });
76 };
77
78 Array.prototype.diff = function(arr) {
79 return this.filter(function(i) {
80 for (var j = 0; j < arr.length ; j++) {
81 if (arr[j].source.index === i.source &&
82 arr[j].target.index === i.target)
83 return false;
84 }
85 return true;
86 });
87 };
88
89 Array.prototype.node_diff = function(arr) {
90 return this.filter(function(i) {
91 for (var j = 0; j < arr.length ; j++) {
92 if (arr[j].name === i.name)
93 return false;
94 }
95 return true;
96 });
97 };
98
99
100// links.sort(function(a,b) {
101// if (a.source > b.source) {return 1;}
102// else if (a.source < b.source) {return -1;}
103// else {
104// if (a.target > b.target) {return 1;}
105// if (a.target < b.target) {return -1;}
106// else {return 0;}
107// }
108// });
109// for (var i=0; i<links.length; i++) {
110// if (i != 0 &&
111// links[i].source == links[i-1].source &&
112// links[i].target == links[i-1].target) {
113// links[i].linknum = links[i-1].linknum + 1;
114// }
115// else {links[i].linknum = 1;};
116// };
117
Ubuntu82b8a832013-02-06 22:00:11 +0000118 function cdiff(topo) {
119 var changed = false;
Ubuntu82b8a832013-02-06 22:00:11 +0000120
121 var n_adds = topo.nodes.node_diff(nodes);
122 var n_rems = nodes.node_diff(topo.nodes);
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +0000123 for (var i = 0; i < n_adds.length; i++) {
124 nodes.push(n_adds[i]);
Ubuntu82b8a832013-02-06 22:00:11 +0000125 changed = true;
126 }
127 for (var i = 0; i < n_rems.length; i++) {
128 for (var j = 0; j < nodes.length; j++) {
129 if ( nodes[j].name == n_rems[i].name ){
130 nodes.splice(j,1);
131 changed = true;
132 break;
133 }
134 }
135 }
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +0000136
137 var l_adds = [];
138 var l_rems = [];
139 l_adds = added_links(topo, links, l_adds);
140 l_rems = gone_links(topo, links, l_rems);
141
142 for (var i = 0; i < l_rems.length ; i++) {
143 for (var j = 0; j < links.length; j++) {
144 if (links[j].source.name == l_rems[i].source.name &&
145 links[j].target.name == l_rems[i].target.name) {
146 links.splice(j,1);
147 changed = true;
148 break;
149 }
150 }
151 }
152 for (var i = 0; i < l_adds.length; i++) {
153 var s;
154 var t;
155 for (var j = 0; j < nodes.length; j++) {
156 if ( json.nodes[l_adds[i].source].name == nodes[j].name ){
157 s = j;
158 break;
159 }
160 }
161 for (var j = 0; j < nodes.length; j++) {
162 if ( json.nodes[l_adds[i].target].name == nodes[j].name ){
163 t = j;
164 break;
165 }
166 }
167 l_adds[i].source = s;
168 l_adds[i].target = t;
169 links.push(l_adds[i]);
Ubuntu82b8a832013-02-06 22:00:11 +0000170 changed = true;
171 }
172 return changed
173 }
174
Ubuntu82b8a832013-02-06 22:00:11 +0000175
176 var changed = cdiff(json);
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +0000177 for (var i = 0; i < nodes.length; i++) {
178 for (var j = 0; j < json.nodes.length; j++) {
179 if ( nodes[i].name == json.nodes[j].name ){
180 nodes[i].group = json.nodes[j].group
181 }
182 }
Ubuntu82b8a832013-02-06 22:00:11 +0000183 }
184
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +0000185// console.log(nodes);
186// console.log(links);
Ubuntu82b8a832013-02-06 22:00:11 +0000187
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +0000188 console.log("changed? " + changed);
Ubuntu82b8a832013-02-06 22:00:11 +0000189
190 if (changed){
191 path = svg.selectAll("path").data(links)
192 circle = svg.selectAll("circle").data(nodes);
193 text = svg.selectAll("text").data(nodes);
194
195 force.stop();
Ubuntu82b8a832013-02-06 22:00:11 +0000196 path.enter().append("svg:path")
197 .attr("class", function(d) { return "link"; })
198 .attr("marker-end", "url(#Triangle)");
199
200 circle.enter().append("svg:circle")
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +0000201 .attr("r", 6)
202 .call(force.drag);
Ubuntu82b8a832013-02-06 22:00:11 +0000203
204/* text.enter().append("svg:text")
205 .attr("x", 8)
206 .attr("y", ".31em")
207 .attr("class", "shadow")
208 .text(function(d) { return d.name.split(":")[7]; }); */
209
210 text.enter().append("svg:text")
211 .attr("x", 8)
212 .attr("y", ".31em")
213 .text(function(d) { return d.name.split(":")[7]; });
214
215 circle.append("title")
216 .text(function(d) { return d.name; });
217
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +0000218 circle.attr("fill", function(d) {
219 if (d.group == 1){return "red";}
220 else if (d.group == 2){return "blue";}
221 else if (d.group == 3){return "green";}
222 else{ return "gray"; }
223 });
224
225
Ubuntu82b8a832013-02-06 22:00:11 +0000226 path.exit().remove();
227 circle.exit().remove();
228 text.exit().remove();
229
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +0000230 console.log(nodes);
231 console.log(links);
232
Ubuntu82b8a832013-02-06 22:00:11 +0000233 force.on("tick", tick);
234 force.start();
235 }
236 }
237 function draw(nodes, links){
238 path = svg.append("svg:g").selectAll("path").data(links)
239 circle = svg.append("svg:g").selectAll("circle").data(nodes);
240 text = svg.append("svg:g").selectAll("text").data(nodes);
241
242 path.enter().append("svg:path")
243 .attr("class", function(d) { return "link"; })
244 .attr("marker-end", "url(#Triangle)");
245
246 circle.enter().append("svg:circle")
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +0000247 .attr("r", 8)
248 .call(force.drag);
Ubuntu82b8a832013-02-06 22:00:11 +0000249
250/* text.enter().append("svg:text")
251 .attr("x", 8)
252 .attr("y", ".31em")
253 .attr("class", "shadow")
254 .text(function(d) { return d.name.split(":")[7]; }); */
255
256 text.enter().append("svg:text")
257 .attr("x", 8)
258 .attr("y", ".31em")
259 .text(function(d) { return d.name.split(":")[7]; });
260
261 circle.append("title")
262 .text(function(d) { return d.name; });
263
Ubuntuaea2a682013-02-08 08:30:10 +0000264 circle.attr("fill", function(d) {
265 if (d.group == 1){return "red";}
266 else if (d.group == 2){return "blue";}
267 else if (d.group == 3){return "green";}
268 else{ return "gray"; }
269 });
Ubuntu82b8a832013-02-06 22:00:11 +0000270
271 force.on("tick", tick);
272 path.exit().remove();
273 circle.exit().remove();
274// text.exit().remove();
275
276 force.start();
277
278 setInterval(function() {
279 $.ajax({
280// url: 'http://onosnat.onlab.us:8080/topology',
281 url: data_source,
282 success: function(json) {
283 update(json)
284 },
285 dataType: "json"
286 });
287 }, 3000);
288 }
Masayoshi Kobayashi274c07d2013-02-20 21:38:16 +0000289
290/*
291 $("#more").click( function() {
292 $.ajax({
293 url: 'http://onosnat.onlab.us:8080/topology_more',
294 success: function(json) {
295 update(json)
296 },
297 dataType: "json"
298 });
299 });
300 $("#less").click( function() {
301 $.ajax({
302 url: 'http://onosnat.onlab.us:8080/topology_less',
303 success: function(json) {
304 update(json)
305 },
306 dataType: "json"
307 });
308 });
309*/
310
Ubuntu82b8a832013-02-06 22:00:11 +0000311 function tick() {
312 path.attr("d", function(d) {
313 var dx = d.target.x - d.source.x,
314 dy = d.target.y - d.source.y,
315 dr = 1/d.linknum; //linknum is defined above
316 dr = 300;
317 return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
318 });
319// circle.attr("cx", function(d) { return d.x; }).attr("cy", function(d) { return d.y; });
320 circle.attr("transform", function(d) {
321 return "translate(" + d.x + "," + d.y + ")";
322 })
Ubuntuaea2a682013-02-08 08:30:10 +0000323 circle.attr("fill", function(d) {
324 if (d.group == 1){return "red";}
325 else if (d.group == 2){return "blue";}
326 else if (d.group == 3){return "green";}
327 else{ return "gray"; }
328 });
Ubuntu82b8a832013-02-06 22:00:11 +0000329// text.attr("x", function(d) { return d.x; }).attr("y", function(d) { return d.y; });
330// text.attr("x", function(d) { return d.x; }).attr("y", function(d) { return d.y; });
331 text.attr("transform", function(d) {
332 return "translate(" + d.x + "," + d.y + ")";
333 });
334 }
335}