This repository has been archived by the owner on Aug 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meshstats.js
94 lines (82 loc) · 3.34 KB
/
meshstats.js
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
// +---------------------------------------------------------------------------
// | File: meshstats.js UTF-8
// | Author: anoymouserver
// | Status:
// | Revision: 2016/01/01
// +---------------------------------------------------------------------------
'use strict';
var stats = {};
stats.jsonPath = "http://map.ffggrz.de/data.php/nodes.json";
stats.gateways = 0;
stats.nodes = {
total: 0,
online: 0,
offline: 0,
geo: 0
};
stats.users = 0;
$(document).ready(function () {
stats.getCurrentStats();
setInterval(function(){stats.getCurrentStats()}, 20 * 1000);
})
.on("usersupdated", function () {
console.log("Online Users: " + stats.users);
$('#users-online').text(stats.users);
})
.on("nodesupdated", function () {
console.log("Online Nodes: " + stats.nodes.online);
console.log("Nodes with GEO: " + stats.nodes.geo);
console.log("Total Nodes: " + stats.nodes.total);
console.log("Gateways: " + stats.gateways);
$('#nodes-total').text(stats.nodes.total);
$('#nodes-online').text(stats.nodes.online + " (" + stats.getPercent(
stats.nodes.total, stats.nodes.online).toFixed(2).replace(".", ",") + "%)");
$('#nodes-offline').text(stats.nodes.offline + " (" + stats.getPercent(
stats.nodes.total, stats.nodes.offline).toFixed(2).replace(".", ",") + "%)");
$('#nodes-geo').text(stats.nodes.geo + " (" + stats.getPercent(
stats.nodes.total, stats.nodes.geo).toFixed(2).replace(".", ",") + "%)");
$('#nodes-gateways').text(stats.gateways);
});
stats.getCurrentStats = function() {
$.getJSON(stats.jsonPath, function (data) {
var nodes = $.map(data.nodes, function(e) {return e;});
var date = new Date(data.timestamp);
var stats_tmp = {nodes: {}};
stats_tmp.nodes.online = nodes.filter(function (d) {
return d.flags.online && !d.flags.gateway;
}).length;
stats_tmp.nodes.total = nodes.filter(function (d) {
return !d.flags.gateway;
}).length;
stats_tmp.gateways = nodes.filter(function (d) {
return d.flags.gateway && d.flags.online;
}).length;
stats_tmp.users = nodes.reduce(function (previousValue, currentValue) {
if (typeof(previousValue) !== "number") {
previousValue = 0;
}
return previousValue + currentValue.statistics.clients;
});
stats_tmp.nodes.geo = nodes.filter(function (d) {
return d.nodeinfo.location;
}).length;
if (stats.users !== (stats_tmp.users)) {
stats.users = stats_tmp.users;
$(document).trigger("usersupdated");
}
if ( stats.nodes.total !== stats_tmp.nodes.total ||
stats.nodes.online !== stats_tmp.nodes.online ||
stats.nodes.geo !== stats_tmp.nodes.geo )
{
stats.gateways = stats_tmp.gateways;
stats.nodes.total = stats_tmp.nodes.total;
stats.nodes.online = stats_tmp.nodes.online;
stats.nodes.offline = stats_tmp.nodes.total - stats_tmp.nodes.online;
stats.nodes.geo = stats_tmp.nodes.geo;
$(document).trigger("nodesupdated");
}
});
};
stats.getPercent = function(base, portion) {
return (base && portion) ? (portion / base * 100) : 0;
};