forked from Thoralf-M/goshimmer-node-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.html
260 lines (219 loc) · 7.74 KB
/
stats.html
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
<html>
<head>
<title>goshimmer stats</title>
<style>
body {
background-color: #202225;
margin: 0;
color: white;
}
table {
border-spacing: 1;
width: 40%;
border: 1px solid rgb(110, 109, 109);
}
th, td {
text-align: left;
padding: 16px;
}
tr:nth-child(even) {
background-color: #565a611c
}
</style>
<script src="./3d-force-graph"></script>
</head>
<body>
<div style="padding: 10px;">
<span style="color:#7fff7f; padding-right: 10px" id="nodes_online">Nodes online: 0</span>
<span style="color:#7fff7f; padding-right: 10px" id="all_accepted_connections">Accepted connections: 0</span>
<span style="color:#ffcc7f; padding-right: 10px" id="neighbors_avg">Avg. total links: 0</span>
<a style="padding-left: 120px" href="index.html">3d view</a>
</div>
<span>A higher number doesn't have to mean there are so many neighbors</span>
<div>
<table id="table">
<tr>
<th>ID</th>
<th>source</th>
<th>target links</th>
<th>total links</th>
<th>accepted links</th>
<th>accepted neighbors</th>
</tr>
</table>
</div>
<script>
var socket = new WebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") + "159.69.158.51" + "/datastream");
socket.onopen = function () {
setInterval(function() {
socket.send("_");
}, 1000);
};
socket.onmessage = function (e) {
switch (e.data[0]) {
case "_":
// do nothing - its just a ping
break;
case "A":
addNode(e.data.substr(1));
break;
case "a":
removeNode(e.data.substr(1));
break;
case "C":
connectNodes(e.data.substr(1, 40), e.data.substr(41, 40));
break;
case "c":
disconnectNodes(e.data.substr(1, 40), e.data.substr(41, 40));
break;
case "O":
setNodeOnline(e.data.substr(1));
break;
case "o":
setNodeOffline(e.data.substr(1));
break;
}
};
var onlineNodes = []
var total_links = []
var source_links = []
var target_links = []
var accepted_links = []
var accepted_neighbors = []
var nodesById = {};
var existingLinks = {};
const data = {
nodes: [],
links: []
};
firstrun()
async function firstrun(){
await new Promise(resolve => setTimeout(resolve, 300));
update()
}
setInterval(function() {
update()
}, 3000)
function update(){
data.links = removeDuplicates(data.links)
let onlinn = data.nodes.filter(n => n.online)
onlineNodes = onlinn.map(n => n.id)
document.getElementById("nodes_online").innerHTML = "Nodes online: "+onlineNodes.length
linkcount()
let avgneighbors = average( total_links);
document.getElementById("neighbors_avg").innerHTML = "Avg. total links: "+avgneighbors.toFixed(2)
document.getElementById("all_accepted_connections").innerHTML = "Accepted connections: "+accepted_links.reduce((a,b) => a + b, 0)
let nodeData = []
for (let k = 0; k < onlineNodes.length; k++) {
nodeData[k] = {"node":onlineNodes[k], "linkcount":total_links[k], "source_links":source_links[k], "target_links":target_links[k], "accepted_links": accepted_links[k], "accepted_neighbors":accepted_neighbors[k]}
}
updateTable(nodeData);
}
const average = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;
function linkcount() {
total_links=[]
source_links=[]
target_links=[]
accepted_links=[]
accepted_neighbors=[]
for (let k = 0; k < onlineNodes.length; k++) {
let nodeid = onlineNodes[k]
let source_links_count = 0
let target_links_count = 0
let all_links = 0
let neighbors = []
for (let j = 0; j < data.links.length; j++) {
if (data.links[j].source == nodeid && onlineNodes.indexOf(data.links[j].target) != -1){
neighbors.push(data.links[j].target)
source_links_count++
}
if (data.links[j].target == nodeid && onlineNodes.indexOf(data.links[j].target) != -1){
neighbors.push(data.links[j].source)
target_links_count++
}
}
let accepted_nodes = []
accepted_nodes=findDuplicates(neighbors)
accepted_neighbors.push(accepted_nodes)
all_links = neighbors.length
source_links.push(source_links_count)
target_links.push(target_links_count)
total_links.push(all_links)
accepted_links.push(accepted_nodes.length)
}
}
function findDuplicates(data) {
let result = [];
data.forEach(function (element, index) {
// Find if there is a duplicate or not
if (data.indexOf(element, index + 1) > -1) {
// Find if the element is already in the result array or not
if (result.indexOf(element) === -1) {
result.push(element);
}
}
});
return result;
}
function removeDuplicates(arr) {
return Object.keys(arr.reduce((acc, val) => {
acc[JSON.stringify(val)] = 1;
return acc;
}, {})).map((val, key, array) => JSON.parse(array[key]))
}
function updateTable(nodeData) {
var Table = document.getElementById("table");
Table.innerHTML = "<tr>\r\n<th>ID<\/th>\r\n<th>source<\/th>\r\n<th>target<\/th>\r\n<th>total links<\/th>\r\n<th>accepted links<\/th>\r\n<th>accepted neighbors<\/th>\r\n<\/tr>";
nodeData.sort(function (a, b) {
return ((a.accepted_links > b.accepted_links) ? -1 : ((a.accepted_links == b.accepted_links) ? 0 : 1));
});
for (let j = 0; j < nodeData.length; j++) {
var x = document.getElementById('table').insertRow();
var y = x.insertCell(0);
var z = x.insertCell(1);
var v = x.insertCell(2);
var w = x.insertCell(3);
var a = x.insertCell(4);
var b = x.insertCell(5);
y.innerHTML = nodeData[j].node;
z.innerHTML = nodeData[j].source_links;
v.innerHTML = nodeData[j].target_links;
w.innerHTML = nodeData[j].linkcount;
a.innerHTML = nodeData[j].accepted_links;
b.innerHTML = nodeData[j].accepted_neighbors;
}
}
function addNode(nodeId, displayImmediately) {
node = {id : nodeId, online: false};
if (!(node.id in nodesById)) {
data.nodes = [...data.nodes, node];
nodesById[node.id] = node;
}
}
function removeNode(nodeId) {
data.links = data.links.filter(l => l.source.id !== nodeId && l.target.id !== nodeId);
data.nodes = data.nodes.filter(currentNode => currentNode.id != nodeId)
delete nodesById[nodeId];
}
function setNodeOnline(nodeId) {
if (nodeId in nodesById) {
nodesById[nodeId].online = true;
}
}
function setNodeOffline(nodeId) {
if (nodeId in nodesById) {
nodesById[nodeId].online = false;
}
}
function connectNodes(sourceNodeId, targetNodeId) {
if(existingLinks[sourceNodeId + targetNodeId] == undefined && existingLinks[targetNodeId + sourceNodeId] == undefined) {
data.links = [...data.links, { source: sourceNodeId, target: targetNodeId }];
}
}
function disconnectNodes(sourceNodeId, targetNodeId) {
data.links = data.links.filter(l => !(l.source == sourceNodeId && l.target == targetNodeId) && !(l.source == targetNodeId && l.target == sourceNodeId));
delete existingLinks[sourceNodeId + targetNodeId];
delete existingLinks[targetNodeId + sourceNodeId];
}
</script>
</body></html>