summaryrefslogtreecommitdiffstats
path: root/static/status-dnbd3.html
blob: 184521dbc1a563e611ad5633691391de42a03ad9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
    <script src="d3.v3.min.js"></script>
    <script type="text/javascript" src="jquery-1.7.1.min.js"></script>
    <script src="smoothie.js"></script>
    <style type="text/css">
        #graph, #statistics, #traffic {position: absolute }
        .circle, .link { stroke: #56f; stroke-width: 3px; stroke-opacity: 0.75; }
        .nodetext { pointer-events: none; font: 10px sans-serif; }
        body { width:100%; height:100%; margin:0; padding:0; overflow: hidden; }
        svg, div, canvas { margin:0; padding:0; }
    </style>
</head>
<body>
<div id="graph"></div>
<div id="statistics"> </div>
<div> <canvas id="traffic" width="100%" height="150"></canvas></div>
</body>
<script type="text/javascript">

// Initialize the statistics chart at the bottom.
var smoothie = new SmoothieChart({'millisPerPixel': 300, 'grid': {'millisPerLine': 30000}, timestampFormatter:SmoothieChart.timeFormatter, yMaxFormatter: bytesToString, yMinFormatter: bytesToString, valueTransformFunction: logarithmicScaling, minValue: 0, maxValue: 262144000, labels: {fontSize: 13}});
smoothie.streamTo(document.getElementById("traffic"), 2000);

// For coloring the servers and the chart-lines.
var colorList = d3.scale.category10();

// Max intensity ~ 50MB/s
var maxIntensity = 50000;

var pendingLinks = [];
var intLinks = [];
var newLinks = [];
var links = {};
var nodes = {};

// color for links
// idle links (blue)
var idleColor = { "r": 85, "g": 102, "b": 255};
// active links (green)
var activeColor = { "r": 0, "g": 230, "b": 20};
// peaking links (red)
var peakColor = { "r": 255, "g": 40, "b": 30};

// IDs need to begin with a letter.
function makeId(prefix, text) {
    return prefix + text.replace(/\./g, "-");
}

// Log scaling for the traffic graph
function logarithmicScaling(value) {
    return Math.log(value/1000000+1);
}

function myGraph(el) {
    var changed = false;

    // Add/update and remove elements on the graph object
    this.updateNode = function (id) {
        var title = id;
        var id = makeId("a", id);

        var node = findNode(id);

        // Add node, if it doesn't exist
        if (!node) {
            nodes.push(node = {"id": id, "title": title, "x": w/2, "y":h/2});
            changed = true;
        } else if (!node.isServer && servers[title]) {
            node.isServer = true;
            // Change color and radius if server.
            var visNode = vis.select("#" + id);
            if (visNode) {
                visNode.select("circle")
                    .attr("r", 15)
                    .style("fill", servers[title].color);
            }
        }
        node.lifetime = 2;
    }

    // Remove nodes/edges that haven't reported for a while.
    this.decay = function () {
        for (var i = nodes.length - 1; i >= 0; --i) {
            if ( nodes[i].lifetime <= 0 ) {
                for (var j = links.length - 1; j >= 0; --j) {
                    if ((links[j].source === nodes[i]) || (links[j].target === nodes[i])) links.splice(j,1);
                }
                nodes.splice(i, 1);
                changed = true;
                continue;
            }
            nodes[i].lifetime--;
        }
        for (var i = links.length - 1; i >= 0; --i) {
            if (links[i].lifetime <= 0) {
                links.splice(i, 1);
                changed = true;
                continue;
            }
            links[i].lifetime--;
        }
    }

    // Add/update links in the graph
    this.updateLink = function (edge, timestamp) {
        var sourceId = edge.source;
        var targetId = edge.target;
        var width = '2.5px';

        // Change color to red for links between servers
        if (servers[sourceId] && servers[targetId]) width = '3.5px';

        sourceId = makeId("a", sourceId);
        targetId = makeId("a", targetId);
        var sourceNode = findNode(sourceId);
        var targetNode = findNode(targetId);

        if ((typeof(sourceNode) !== 'undefined') && (typeof(targetNode) !== 'undefined')) {
            var link = findLink(sourceNode, targetNode);
            // Add non existing links
            if (!link) {
                var index = $.inArray(sourceId + targetId, pendingLinks);
                if (index !== -1) {
                    links.push({"id": sourceId + "-" + targetId ,"source": sourceNode, "target": targetNode, "lifetime":2, "color": idleColor, "downloadRate" : 0, "lastReceived": 0, "lastTimestamp": 0, "lastUptime": 0, "colorIntensity": 0, "width": width });
                    changed = true;
                } else {
                    newLinks.push(sourceId + targetId);
                }
            } else {
                link.lifetime = 2;

                // Calculate download rate for clients and color links.
                if (link.lastTimestamp != 0) {
                    if(timestamp - lastTime > 0) {
                        // Download rate in KB/s
                        link.downloadRate = ((edge.received - link.lastReceived) / (timestamp - lastTime)) * 4 * 1000;
                        // Clients may have multiple connections with different received data -> prevent negative download rate...
                        if(link.downloadRate < 0) link.downloadRate = 0;

                        // only need fiddle with colors if needed.
                        if (link.downloadRate > 0) {
                            increasColorIntensity(link);
                            colorLink(link);
                            updateLinkColor(link);
                        }
                    }
                } else {
                    link['downloadRate'] = 0;
                }
                link['lastReceived'] = edge.received;
                link['lastTimestamp'] = timestamp;
            }
        }
    }

    // Color a link according to its intensity, does not apply the color in the svg! Use updateLinkColor function for that.
    function colorLink(link) {
        var red, green, blue;
        // Green colors between 0 - 1 MB/s
        if( link.colorIntensity <= 1000 ) {
            // Blending idle (blue) and active (green) color
            var factor =  link.colorIntensity / 1000;
            var cFactor = 1 - factor;

            red = Math.sqrt(cFactor * Math.pow(idleColor.r, 2) + factor * Math.pow(activeColor.r, 2));
            green = Math.sqrt(cFactor * Math.pow(idleColor.g, 2) + factor * Math.pow(activeColor.g, 2));
            blue = Math.sqrt(cFactor * Math.pow(idleColor.b, 2) + factor * Math.pow(activeColor.b, 2));

        } else { // Red over 1MB/s
            // Blending active (green) and peak (red) color
            var factor =  (link.colorIntensity - 1000)/ (maxIntensity - 1000);
            var cFactor = 1 - factor;

            red = Math.sqrt(cFactor * Math.pow(activeColor.r, 2) + factor * Math.pow(peakColor.r, 2));
            green = Math.sqrt(cFactor * Math.pow(activeColor.g, 2) + factor * Math.pow(peakColor.g, 2));
            blue = Math.sqrt(cFactor * Math.pow(activeColor.b, 2) + factor * Math.pow(peakColor.b, 2));
        }
        link.color = "rgb("+ Math.floor(red) + ", " + Math.floor(green) + ", " + Math.floor(blue) + ")";
    }

    // Update the color of a link in the svg.
    function updateLinkColor(link) {
        // Prevent future searches for links to improve performance.
        if(!link.visObject) {
            link.visObject = vis.select("#" + link.source.id + "-" + link.target.id);
        }
        var visLink = link.visObject;
        visLink.style({stroke: function(d) { return d.color; }});
    }

    // Interval for fading out the color of active links
    function decayLinkColor() {
        for (var i = 0; i < links.length ; i++) {
            var link = links[i];
            // Do nothing if there is no intensity to decay.
            if(link.colorIntensity > 0) {
                // Fading rate of colored links
                var fadingValue = (link.colorIntensity * 0.15) + 10;
                link.colorIntensity -= fadingValue;
                if (link.colorIntensity < 0) {
                    link.colorIntensity = 0;
                } else if (link.colorIntensity < link.downloadRate) {
                    link.colorIntensity = link.downloadRate;
                }
                colorLink(link);
                updateLinkColor(link);
            }
        }
    }
    window.setInterval(decayLinkColor, 500);
    window.setInterval(function() {
        if (force.alpha() == 0) {
            force.alpha(0.015);
            force.tick();
            force.stop();
        }
    }, 125);


    // Set the color intensity of link according to its download rate.
    function increasColorIntensity(link) {
        if (link.colorIntensity < link.downloadRate) {
            link.colorIntensity = link.downloadRate;
        }
        if (link.colorIntensity >= maxIntensity) {
            link.colorIntensity = maxIntensity;
        }
    }

    function findLink(sourceId, targetId) {
       for (var i = 0; i < links.length; ++i) {
               if ( (links[i].source === sourceId && links[i].target === targetId)
                    || (links[i].source === targetId && links[i].target === sourceId) )
                return links[i];
        }
    }

    var findNode = function (id) {
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].id === id)
                return nodes[i];
        }
    }


    var findNodeIndex = function (id) {
        for (var i=0; i < nodes.length; i++) {
            if (nodes[i].id === id)
                return i;
        };
    }

    var w = 100, h = 100;
    var lastW = -1, lastH = -1;

    var updateBounds = function() {
        var width = window.innerWidth;
        // Chart needs to fit under the graph and the statistics
        h = window.innerHeight - 150;
        // Width for the graph and the statistics div
        w = width/3*2;
        if (lastW === w && lastH === h) return;
        lastW = w;
        lastH = h;
        vis.attr("width", w);
        vis.attr("height", h);
        force.size([w, h]);
        $(el).attr("width", w);
        $(el).attr("height", h);
        // Positions for statistics and the traffic graph
        $("#statistics").attr("width", w/2).css("width", w/2 + "px").css("left", w + "px");
        $("#statistics").attr("height", h).css("height", h + "px");
        $("#traffic").attr("width", width).css("top", h + "px");
    }

    var vis = this.vis = d3.select(el).append("svg:svg")
        .attr("width", w)
        .attr("height", h);

    // Settings for movement of the graph
    var force = d3.layout.force()
        // Force of center gravitation
        .gravity(.12)
        // Desired distance of the links
        .distance(function(bla) { if (bla.source.isServer && bla.target.isServer) return 10; return 4; })
        // Strength of the links (how strongly the distance is enforced) (0-1)
        .linkStrength(0.3)
        // Force with which all nodes attract each other
        .charge(function(bla) { return -0.9; })
        // Friction for the graph nodes
        .friction(0.5)
        .theta(0.75)
        .size([w, h]);

    nodes = force.nodes();
    links = force.links();


    this.update = function () {
        if(changed) {
            var first = svgLinks.length === 0;
            update();
            if (first) force.alpha(0.9);
        }
    }

    // Update the complete svg.
    var svgNodes = [];
    var svgLinks = [];
    function update() {
        changed = false;

        svgNodes = vis.selectAll("g.node")
            .data(nodes, function(d) { return d.id; });

        var nodeEnter = svgNodes.enter().append("g")
            .attr("class", "node")
            .attr("id", function(d) { return d.id; })
            .on("mouseover", function(d) {
                // Highlight statistics div
                var statDiv = document.getElementById(makeId("b", d.title));
                if (statDiv){
                    statDiv.setAttribute("style", "background-color:blue; color:white;");
                }

                // Increase line width of server in traffic chart
                var s = servers[d.title];
                if (!s) return;
                smoothie.seriesSet[s.index].options.lineWidth = 4;
            })
            .on("mouseout", function(d) {
                // Make statistics div normal again
                var statDiv = document.getElementById(makeId("b", d.title));
                if (statDiv){
                    statDiv.setAttribute("style", "background-color:white; color:black;");
                }

                // Reset line width
                var s = servers[d.title];
                if (!s) return;
                smoothie.seriesSet[s.index].options.lineWidth = 2;
            });

        nodeEnter.append("circle")
            .attr("class", "circle")
            .attr("r", 4).style("fill", "#00f");

        nodeEnter.append("text")
            .attr("class", "nodetext")
            .attr("dx", -32)
            .attr("dy", "-1em")
            .text(function(d) {return d.title});

        svgNodes.exit().remove();

        svgLinks = vis.selectAll("line.link")
            .data(links, function(d) { return d.source.id + "-" + d.target.id; });

        svgLinks.enter().insert("line")
            .attr("class", "link")
            .attr("id", function(d) {return d.id; })
            .style({stroke: function(d) { return d.color }, "stroke-width": function(d) { return d.width } });

        svgLinks.exit().remove();

        // Put all nodes on top again by adding them again
        var jGraph = $('#graph').find('svg');
        vis.selectAll('g.node').forEach(function(e){jGraph.append(e)});


        force.on("tick", renderTick);

        updateBounds();
        force.start();
    }

    // Render function
    function renderTick() {
        var lx = 1000000, ly = 1000000, ux = -1000000, uy = -1000000;
        for (var i = 0; i < nodes.length; ++i) {
            if (nodes[i].x < lx) lx = nodes[i].x;
            if (nodes[i].x > ux) ux = nodes[i].x;
            if (nodes[i].y < ly) ly = nodes[i].y;
            if (nodes[i].y > uy) uy = nodes[i].y;
        }
        var width = (ux - lx), height = (uy - ly);
        var scale;
        var offX = 0, offY = 0;
        if ( (width / w) > (height / h) ) {
                scale = (w - 40) / width;
                offY = (h - (height * scale + 20)) / 2;
        } else {
                scale = (h - 40) / height;
                offX = (w - (width * scale + 20)) / 2;
        }
        var fixX = function(x) {
            return (x - lx) * scale + 20 + offX;
        }
        var fixY = function(y) {
            return (y - ly) * scale + 20 + offY;
        }
        svgLinks.attr("x1", function(d) { return fixX(d.source.x); })
            .attr("y1", function(d) { return fixY(d.source.y); })
            .attr("x2", function(d) { return fixX(d.target.x); })
            .attr("y2", function(d) { return fixY(d.target.y); });

        svgNodes.attr("transform", function(d) {
            return "translate(" + fixX(d.x) + "," + fixY(d.y) + ")";
        });
    };

    window.onresize = update;
    update();
}

graph = new myGraph("#graph");

var servers = {};
var serverCount = 0;
var lastTime = 0;

// Get new data
setInterval( function() {
    $.get('/data.json', function(data) {
        if (data.timestamp < lastTime) lastTime = data.timestamp;
        if(data.timestamp === lastTime) return;
        var g = data.graph;
        var stats = data.servers;
        updateGraph(g, data);
        // updateTrafficGraph has to be called before updateTextStatistics to populate servers
        updateTrafficGraph(stats, data);
        updateTextStatistics(stats);
        lastTime = data.timestamp;
}, 'json').always(function() {
    graph.decay();
    graph.update();
    });
}, 2000);


// Update data of the graph
function updateGraph(g, data) {
    if (g) {
        for (var i = 0; i < g.nodes.length; ++i) {
            graph.updateNode(g.nodes[i].name);
        }
        for (var i = 0; i < g.edges.length; ++i) {
            graph.updateLink(g.edges[i], data.timestamp);
        }
        pendingLinks = intLinks;
        intLinks = newLinks;
        newLinks = [];
    }
}


// Convert bytes to GiB or TiB and return a string in form "10,23 GiB"
function bytesToString( bytes ) {
    var convertedValue;
    var unit;
    if (bytes >= 1099511627776 ) {
        convertedValue = Math.round( (bytes / 1099511627776) * 100 ) / 100 ;
        unit = " TiB";
    } else if (bytes >= 1073741824 ) {
        convertedValue = Math.round( (bytes / 1073741824) * 100 ) / 100 ;
        unit = " GiB";
    } else if (bytes >= 1048576 ) {
        convertedValue = Math.round( (bytes / 1048576) * 100 ) / 100 ;
        unit = " MiB";
    } else if ( bytes >= 1024 ) {
        convertedValue = Math.round( (bytes / 1024) * 100 ) / 100 ;
        unit = " KiB";
    } else {
        convertedValue = Math.round(bytes);
        unit = " B";
    }
    return convertedValue + unit;
}

// Update data of the statistics divs
function updateTextStatistics(stats) {
    var glob = $('#gstats');
    if (glob.length === 0) {
        $('#statistics').append('<div id="gstats"></div>');
        glob = $('#gstats');
    }
    glob.html('<p><b>Global</b><br>'
        + 'Upload: ' + bytesToString(globalUploadRate) + '/s<br>'
        + 'Upload Peak: ' + bytesToString(globalUploadMax) + '/s<p>');
    if (stats) {
        for (var i = 0; i < stats.length; ++i) {
            var divId = makeId("b", stats[i].address);
            var server = $('#' + divId);
            if (server.length === 0) {
                    $("#statistics").append('<div id="' + makeId ("b", stats[i].address) + '"></div>');
                    server = $("#" + divId);
            }

            var upload = servers[stats[i].address].uploadRate;
            upload = upload ? upload : 0;
            // Generate the HTML string
            server.html( "<p><b> Server: " + stats[i].address + "</b><br>"
                + "Number of clients: "
                + stats[i].clientCount + "<br>"
                + "uptime: " + Math.floor(stats[i].uptime / (3600 * 24)) + "d "
                + Math.floor(stats[i].uptime / 3600) % 24 + "h " + Math.floor((stats[i].uptime) / 60) % 60 + "min" + "<br>"
                + "Sent: " + bytesToString(stats[i].bytesSent)  + "<br>"
                + "Received: " + bytesToString(stats[i].bytesReceived) + "<br>"
                + "Upload: " + bytesToString(servers[stats[i].address].uploadRate) + "/s<br>"
                + "Upload Peak: " + bytesToString(servers[stats[i].address].uploadPeak) + "/s" + "</p>"
                );
        }
    }
}

var globalUploadRate = 0, globalUploadMax = 0;

// Update the traffic graph
function updateTrafficGraph(stats, data) {
    if (stats) {
        globalUploadRate = 0;
        for (var i = 0; i < stats.length; ++i) {
            var server = servers[stats[i].address];
            if (!server) {
                servers[stats[i].address] = server = { 'lastUptime': 0, 'lastTimestamp': 0, 'lastSent': 0, 'line': new TimeSeries({logarithmicScale: true}), 'index': serverCount++ , 'uploadRate': 0, 'downloadRate': 0, 'uploadPeak': 0 }
                server.color = colorList(stats[i].address);
                smoothie.addTimeSeries(server['line'], {lineWidth:2, strokeStyle: server.color});
            }
            // Server seems to have rebootet, redo values but add no point to chart.
            if (server['lastUptime'] > stats[i].uptime) {
                server['lastUptime'] = 0;
            }

            // Add points to graph and set the upload rate for the statistics
            if (server['lastUptime'] != 0) {
                // Upload rate in bytes/s
                server['uploadRate'] = (stats[i].bytesSent - server['lastSent'])/(stats[i].timestamp - server.lastTimestamp) * 1000;
                if (server['uploadRate'] > server['uploadPeak']) server['uploadPeak'] = server['uploadRate'];
                // Rate in MiB/s
                server['line'].append(new Date().getTime(), server['uploadRate']);
            } else {
                server['uploadRate'] = 0;
            }

            server['lastUptime'] = stats[i].uptime;
            server['lastSent'] = stats[i].bytesSent;
            server['lastTimestamp'] = stats[i].timestamp;
            globalUploadRate += server['uploadRate'];
        }
        if (globalUploadRate > globalUploadMax) globalUploadMax = globalUploadRate;
    }
}

</script>