summaryrefslogblamecommitdiffstats
path: root/static/new_status-dnbd3.html
blob: f4505c4f84c06476148ea2e935e7974d0fa82d5c (plain) (tree)
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581




































































































































































































































































































































































































































































































































































































                                                                                                                   
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
    <script type="text/javascript" src="jquery-1.7.1.min.js"></script>
    <script src="smoothie.js"></script>

    <style type="text/css">
        body { width:100%; height:100%; margin:0; padding:0; overflow: scroll; }
        
        svg, div, canvas { margin:0; padding:0; }
        
        table, td { border: 1px solid black; text-align: center; }
        .clientTable
        {
            display:  table;
            width:auto;
            background-color:#eee;
            border:1px solid  #666666;
            border-spacing:5px;
        }
        .speed 
        { 
            align-content: right;/*fix for  buggy browsers*/
            width:240px;
            background-color:#E0E0E0; 
            background: linear-gradient(90deg, #E0E0E0 100%, #9DA7AE 0%);

        }

        .noBorder
        {
           text-align: left;
           border: none;
        }
        
        .serverTd
        {
            border:none;
            font-weight: bold;
        }

        .serverLTd
        {
            border:none;
            font-weight: bold;
            text-align: left;
        }

        
    </style>
</head>
<body>
    <div id="bigDiv">
    <!--     <div style="float:left" >
    <table class="clientTable" id="Table">
      <tbody>
        <tr class="firstRow">
          <td>Client Ip</td><td class='speed'>Download Speed from </td>
        </tr>
      </tbody>
    </table> -->
    </div>
    <!--
    <div style="float:left">
    <table class="clientTable" id="otherTable">
      <tbody>
        <tr class="firstRow">
          <td>Client Ip</td><td class='speed'>Client Speed</td>
        </tr>
      </tbody>
    </table>
    </div> -->
   <div id="statistics"><div id="debug"></div></div>

    <div> <canvas id="traffic" width="100%" height="150"></canvas></div>

</body>

<script type="text/javascript">


var lock = false;
var lastTime = 0;
var posibleNrOfRows = 23;

var newAndOld = { 'new':{}, 'old':{} };
var servClSpeeds = {'fresh':0};

var sortedClSpeeds = {'fresh':0, 'nrOfServers':0};

var serverStats = {};
var serverNewAndOld = { 'new':{}, 'old':{} };

// Get new data
setInterval( function() {
    $.get('/data2.json', function(data) {
        if (data.timestamp < lastTime) lastTime = data.timestamp;
        if(data.timestamp === lastTime) return;
        
        var servers = data.servers;
        // take what we need; give nothing back
        var parsedServers = parseServers( servers );

        var parsedServersForStats = parseServersForStats( servers );

        newAndOld['new'] = parsedServers;

        serverNewAndOld['new'] = parsedServersForStats;

        update_servClSpeeds( newAndOld );

        update_serverStats( serverNewAndOld );

        if ( servClSpeeds.fresh !== 0 ) { update_sortedClSpeeds(servClSpeeds); }

        if ( sortedClSpeeds.fresh !== 0 ) {
            // TODO:
            // do stuff with the sorted stuff and stuff and stuff
            dostuff( sortedClSpeeds, serverStats );
        }

        servClSpeeds['fresh'] = 1;
        lastTime = data.timestamp;
        newAndOld['old'] = parsedServers;
        serverNewAndOld['old'] = parsedServersForStats;
}, 'json').always(function() {
    });
}, 2000);


// changes the colors of the tds holding the download speed
// color code and details follow bellow
function makeItPretty( clArray ) {
    for ( var key in clArray ) {
        for ( var client in clArray[key]) {
            var wrappedIp = ipWrapper( client );

            var Bps = clArray[key][client];
            var td = $( "." + wrappedIp + " > " + ".speed");
            td.css("background", changeBackground1( Bps ) );            
        } 
    }
}

function changeBackground1 ( speed ) {
    var result = "";
    switch ( true ) {
        case speed < 1024:
            var proc = ( ( speed * 10 ) / 1024 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(-90deg, #E0E0E0 "+(100-proc2)+"%, #8adb8b "+proc2+"%)";
            break;
        case ( speed >= 1024 && speed  < 10240 ):
            var proc = ( ( speed * 10 ) / 10240 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(-90deg, #E0E0E0 "+(100-proc2-10)+"%, #8adb8b "+(proc2+10)+"%)";
            break;
        case ( speed >= 10240 && speed < 102400 ):
            var proc = ((speed * 10) / 102400 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(-90deg, #E0E0E0 "+(100-proc2-20)+"%, #8adb8b "+(proc2+20)+"%)";
            break;

        case ( speed >= 102400 && speed < 1048576 ):
            var proc = ((speed * 10) / 1048576 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(-90deg, #E0E0E0 "+(100-proc2-30)+"%, #5cdb55 "+(proc2+30)+"%)";
            break;
        case ( speed >= 1048576 && speed < 10485760 ):
            var proc = ((speed * 10) / 10485760 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(-90deg, #E0E0E0 "+(100-proc2-40)+"%, #5cdb55 "+(proc2+40)+"%)";
            break;
        case ( speed >= 10485760 && speed < 104857600 ):
            var proc = ( ( speed * 10 ) / 104857600 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(90deg, #5cdb55 "+(proc2+50)+"%, #E0E0E0 "+(50-proc2)+"%)";
            break;

        case ( speed >= 104857600 && speed < 1073741824 ):
            var proc = ( ( speed * 10 ) / 1073741824 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(90deg, #26a81f "+(proc2+60)+"%, #E0E0E0 "+(40-proc2)+"%)";
            break;
        case ( speed >= 1073741824 && speed <= 10737418240 ):
            var proc = ( ( speed * 10 ) / 10737418240 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(90deg, #26a81f "+(proc2+70)+"%, #E0E0E0 "+(30-proc2)+"%)";
            break;
        case ( speed >= 10737418240 && speed <= 107374182400 ):
            var proc = ( ( speed * 10 ) / 107374182400 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(90deg, #26a81f "+(proc2+80)+"%, #E0E0E0 "+(20-proc2)+"%)";
            break;

        case ( speed >= 107374182400 && speed < 1099511627776 ):
            var proc = ( ( speed * 10 ) / 1099511627776 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(90deg, #056d00 "+(proc2+90)+"%, #E0E0E0 "+(10-proc2)+"%)";
            break;
        case ( speed > 10995116277760 ):
            var proc = ( ( speed * 10 ) / 10995116277760 );
            var proc2 = proc < 10 ? Math.round( proc ) : 10;
            result = " linear-gradient(90deg, #056d00 "+(proc2+90)+"%, #E0E0E0 "+(10-proc2)+"%)";
            break;
        }
    return result;
}

function changeBackground2 ( speed ) {
    var result = "";
    switch ( true ) {
        case speed < 524288:
            var proc = ( ( speed * 25 ) / 524288 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(-90deg, #E0E0E0 "+(100-proc2)+"%, #5fce33 "+proc2+"%)";
            break;
        case ( speed >= 524288 && speed  < 1048576 ):
            var proc = ( ( speed * 25 ) / 1048576 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(-90deg, #5fce33 "+(100-proc2-25)+"%, #1f6b01 "+(proc2+25)+"%)";
            break;
        
        case ( speed >= 1048576 && speed < 536870912 ):
            var proc = ( ( speed * 25 ) / 536870912 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(90deg, #ef396d "+(proc2+50)+"%, #1f6b01 "+(50-proc2)+"%)";
            break;

        case ( speed >= 536870912 && speed < 1073741824 ):
            var proc = ( ( speed * 25 ) / 1073741824 );
            var proc2 = Math.round( proc );
            result = " linear-gradient(90deg, #870014 "+(proc2+75)+"%, #ef396d "+(25-proc2)+"%)";
            break;
        
        case ( speed > 1073741824 ):
            result = " linear-gradient(90deg, #0285ea 100%, #ad001a 0%)";
            break;
        }
    return result;
}
// create as many divs as servers...append them to the bigDiv...if a small div already exists...move to 
// the next one
function createTheDivz ( servArray ) {
    lock = true;
    for( i = 0; i < servArray.length; i++ ) {

        var wrappedIp = ipWrapper( servArray[i] );
        if ( $( "#div-" + wrappedIp ).length > 0 ) {
            $( "#div-" + wrappedIp ).remove();
        }
        var servDiv = $( '<div></div>' );

        servDiv.attr( 'id', "div-" + wrappedIp );
        servDiv.attr( 'style', "float:left" );

        $( "#bigDiv" ).append( servDiv );
    }
}

// simple function creates a table from a given server ip, its clients and a number of rows
function buildTable ( servIp, clArray , nrOfRows, statsObj ) {

    // test to see if the number of rows to be shown exceeds the number of existing clients
    // if yes replace number of shown rows with the length of clients array(represents the nr of clients)
    var localRows = nrOfRows;
    if ( nrOfRows > clArray.length ) { localRows = clArray.length; }


    // if table object already exists delete it
    var wrappedIp = ipWrapper( servIp );
    if ( $( "#tbl-" + wrappedIp ).length > 0 ) {
        $( "#tbl-" + wrappedIp ).remove();
    }
    var servTable = $( "<table></table>" ).addClass( "clientTable" );

    servTable.attr( "id", "tbl-" + wrappedIp );

    var row1 = $( "<tr></tr>" ).addClass( 'row1' );
    var lCol1 = $( "<td></td>" ).addClass("serverLTd").text( "Server Ip: " );
    var rCol1 = $( "<td></td>" ).addClass("serverTd").text( servIp );
    row1.append( lCol1 );
    row1.append( rCol1 );
    servTable.append( row1 );

    for ( var key in statsObj[servIp] ) {
        if ( key === "address" || key === "timestamp") { continue; }
        var server = statsObj[servIp];
        var row = $( "<tr></tr>" );
        var lCol = $( "<td></td>" ).addClass("noBorder").text( key +": " );
        var value  = server[key];

        var rCol = $( "<td></td>" ).addClass("noBorder").text( value );

        row.append( lCol );
        row.append( rCol );
        servTable.append( row );
    }

    for( var key in clArray ) {
        

        for( var client in clArray[key] ) {
            var row = $( "<tr></tr>" ).addClass( ipWrapper( client ) );
            var lCol = $( "<td></td>" ).text( client );
            var rCol = $( "<td></td>" ).addClass( 'speed' ).text( bytesToString( clArray[key][client] ) );
        }
        row.append( lCol );
        row.append( rCol );

        servTable.append( row );
    }

    $( "#div-" + wrappedIp ).append( servTable );    
}

// do stuff stuff stuff stuffy stuff
function dostuff( obj, statsObj ) {
    // the servers in an array sorted after the number of clients...index 0 the server with the most
    // clients...will also receive the leftmost table
    var leServers = sortServers( obj );
    if (lock == false ) { createTheDivz( leServers ) };
    
    for( i = 0; i < leServers.length; i++ ) {

        var wrappedIp = ipWrapper(leServers[i]);
        if(! $( "#div-" + wrappedIp ).length > 0 ) { continue; }
        
        var serverIp = leServers[i];
        var clientsArray = obj[serverIp];

        buildTable( serverIp, clientsArray, posibleNrOfRows, statsObj );

        makeItPretty( clientsArray );
        // -- TODO figure a way to play with the colours again...consider using the whole object or just each array
        // try both...
    }
}
// little helper function to wrap the Ip and replace all the . with - and add a c at the start of the ip
// needed for the html table because the rows cannot have an id with . in it
function ipWrapper( givenIp ) {
    var localIp = givenIp.replace(/\./g, "-");
    return "c"+localIp;
}

// function that give us the servers ip which has the most clients
function sortServers( obj ) {

    var keys = [];

    for(var key in obj) {
        if( key == 'fresh' || key == "nrOfServers" ) { continue; }
        keys.push( key );
    }
    var sortedKeys = keys.sort( function( a, b ) { return obj[b].length - obj[a].length; } );

    return sortedKeys;
}


function update_serverStats ( obj ) {    
    
    for( var key in obj['new'] ) {
        var localObject = {};
        if( key in obj['old'] ) {
            var newObject = obj['new'][key];

            var oldObject = obj['old'][key];

            var stime = ( newObject.timestamp <= oldObject.timestamp ) ? 
                        2 : 
                        ( newObject.timestamp - oldObject.timestamp ) / 1000;

            localObject['address'] = newObject.address;
            localObject['timestamp'] = newObject.timestamp < oldObject.timestamp ?
                                        newObject.timestamp :
                                        newObject.timestamp - oldObject.timestamp;
            localObject['uptime'] = Math.floor(newObject.uptime / ( 3600 * 24 ) ) + "d " +
                                    Math.floor(newObject.uptime / 3600 ) % 24 + "h " +
                                    Math.floor(newObject.uptime / 60 ) % 60 + "min";

            var uploadSpeed = newObject.bytesSent < oldObject.bytesSent ?
                                            0 :
                                        Math.round(
                                            (newObject.bytesSent - oldObject.bytesSent)
                                            / stime);
            localObject['uploadSpeed'] = bytesToString(uploadSpeed);

            var downloadSpeed = newObject.bytesReceived < oldObject.bytesReceived ?
                                            0 :
                                        Math.round(
                                            (newObject.bytesReceived - oldObject.bytesReceived)
                                            / stime);
            localObject['downloadSpeed'] = bytesToString(downloadSpeed);

            var localBytesSent = (newObject.bytesSent < oldObject.bytesSent) ? 
                                        oldObject.bytesSent : newObject.bytesSent;
            localObject['Total sent'] = bytesToString( localBytesSent );

            var localBytesReceived = (newObject.bytesReceived < oldObject.bytesReceived) ? 
                                        oldObject.bytesReceived : newObject.bytesReceived;

            localObject['Total received'] = bytesToString( localBytesReceived );

            serverStats[localObject.address] = localObject;
        } 
    }   
}

// update function for the servClSpeeds object
function update_servClSpeeds ( obj ) {
    for( var key in obj['new'] ) {
        if( key in obj['old'] ) {
            var newObject = obj['new'][key];
            var oldObject = obj['old'][key];
            var resultObject = speedCalc( newObject, oldObject );
            servClSpeeds[key] = resultObject;
        }
    }
}

// update function for the sortedClSpeeds
function update_sortedClSpeeds ( obj ) {
    var nrOfServ = 0;
    for ( var key in obj ) {
        if ( key === 'fresh' ) { continue; }
        var clients = obj[key];
        var sortedArray = getSortedKeys( clients );
        sortedClSpeeds[key] = sortedArray;
        nrOfServ++;
    }
    sortedClSpeeds['fresh'] = 1;
    sortedClSpeeds['nrOfServers'] = nrOfServ;
}

// helper function for the sortIps() function
function getSortedKeys( obj ) {
    sortedIps = [];
    var keys = []; 
    for( var key in obj ) keys.push( key );
    var sortedKeys = keys.sort( function( a, b ) { return obj[b] - obj[a]; } );

    for( var i = 0; i < sortedKeys.length; i++) {
        var smallObj = {};
        var Ip = sortedKeys[i];
        var speed = obj[sortedKeys[i]];
        smallObj[Ip] = speed;
        sortedIps.push( smallObj );
    }
    return sortedIps;
}

// function that takes the servers-with-clientspeeds object and returns another object that has 
// serverips as keys and as values an array of sorted ips with the leftmost element beeing the ip with the
// greatest speed and the rightmost one, the one with the smallest speed
// each element of the array is a string consisting of ip:socket#speedInBytesPerSecond
function sortIps ( obj ) {
    var localObject = {};
    for( var servip in obj ) {
        var localObject = obj[servip];
        var localArray = getSortedKeys( localObject );
        localObject[serverip] = localArray;
    }
    return localObject;
}

// function takes new and old parsedServer object and returns a new object where the traffic of each
// client is now replaced by speed in bytes/s
function speedCalc( newO, oldO ){
    var localObject = {};

    // time in seconds calculated from the difference of timestamps or default 2
    // TODO: ask about same timestamp
    var stime = ( newO.timestamp <= oldO.timestamp ) ? 
                    2 : 
                    ( newO.timestamp - oldO.timestamp ) / 1000;
    for( var ip in newO.clients ) {
        if( ip in oldO.clients ) {
            var bitesDelta = newO.clients[ip] - oldO.clients[ip];

            if( bitesDelta < 0 ) { bitesDelta = newO.clients[ip]; }
            // take only the active clients
            if( bitesDelta != 0 ) {
                var speed = Math.round( bitesDelta / stime );
 
                var newClIp = ip.split( ":" )[0];

                if( newClIp in localObject ) { 
                    localObject[newClIp] = localObject[newClIp] + speed;
                } else {
                    localObject[newClIp] = speed;    
                }
            }
        }
    }
    return localObject;
}

function parseServersForStats( servers ) {
    var parsedServers = {};
    var numberOfClients = 0;

    for (var i in servers ) {
        numberOfClients += servers[i].clientCount;
        newServerObject = {};
        var timestamp = servers[i].timestamp;
        newServerObject['timestamp'] = timestamp;

        var serverIp = servers[i].address;
        newServerObject['address'] = serverIp;

        newServerObject['uptime'] = servers[i].uptime;
        newServerObject['bytesSent'] = servers[i].bytesSent;
        newServerObject['bytesReceived'] = servers[i].bytesReceived;

        parsedServers[serverIp] = newServerObject;
    }
    return parsedServers;

}
// takes the whole lot of servers and returnes an object whose keys are the server ip and each ip has
// a timestamp and an adjecent clients object
// clients object is just key:value pairs
function parseServers( servers ) {
    var parsedServers = {};
    // servers is a array containing server objects
    for( var i in servers ) {
        // object has .clients argument and .timestamp argument
        newServerObject = {};
        // save and write timestamp of the server
        var timestamp = servers[i].timestamp;
        newServerObject['timestamp'] = timestamp;
        // iterate over clients array and save ip:bytes pairs in local object
        var clientsObject = {};

        var clients = servers[i].clients;
        for( var ii in clients ) {
            var key = clients[ii].address;
            var value = clients[ii].bytesSent;
            clientsObject[key] = value;
        }
        newServerObject['clients'] = clientsObject;

        var serverIp = servers[i].address;
        // object shoul know its own name; in this case its ip
        newServerObject['address'] = serverIp;

        parsedServers[serverIp] = newServerObject;
    }
    return parsedServers;
}


function serversForStats( servers ) {

}
// 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;
}

</script>