Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit bd30cfa

Browse files
Merge pull request #41 from ElmerLastdrager/improvegui
Updates on the visualisation
2 parents bbff758 + d822a7e commit bd30cfa

File tree

2 files changed

+88
-44
lines changed

2 files changed

+88
-44
lines changed

html/js/mqtt_client.js

Lines changed: 78 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var last_traffic = 0 // Last received traffic trace
44
var time_sync = 0; // Time difference (seconds) between server and client
55
var active = false; // Determines whether we are active or not
66

7+
var datacache = []; // array of all data items to be added on the next iteration.
8+
79
function init() {
810
connectToMQTT();
911
initGraphs();
@@ -16,7 +18,7 @@ function init() {
1618
client.connect({onSuccess:onTrafficOpen});
1719

1820
// Make smooth traffic graph when no data is received
19-
setInterval(fillEmptiness, 200);
21+
setInterval(redrawTrafficGraph, 1000);
2022
}
2123

2224
function connectToMQTT() {
@@ -80,7 +82,8 @@ function onTrafficMessage(msg) {
8082
// First, update time_sync to account for timing differences
8183
time_sync = Math.floor(Date.now()/1000 - new Date(result['timestamp']))
8284
// update the Graphs
83-
handleTrafficMessage(result);
85+
//handleTrafficMessage(result);
86+
datacache.push(result); // push to cache
8487
break;
8588
case 'blocked':
8689
//console.log("Got blocked command: " + msg);
@@ -163,14 +166,20 @@ function initTrafficDataView() {
163166
handleTrafficMessage(data);
164167
}
165168

169+
// Takes data from cache and redraws the graph
166170
// Sometimes, no data is received for some time
167171
// Fill that void by adding 0-value datapoints to the graph
168-
function fillEmptiness() {
169-
if (active && last_traffic != 0 && Date.now() - last_traffic >= 1000) {
172+
function redrawTrafficGraph() {
173+
// FIXME
174+
if (active && datacache.length == 0) {
170175
var data = { 'timestamp': Math.floor(Date.now() / 1000) - time_sync,
171176
'total_size': 0, 'total_count': 0,
172177
'flows': []}
173178
handleTrafficMessage(data);
179+
} else if (active) {
180+
var d = datacache;
181+
datacache = [];
182+
handleTrafficMessage(d);
174183
}
175184
}
176185

@@ -181,51 +190,79 @@ function handleTrafficMessage(data) {
181190
// Do not update if we have not received data yet
182191
if (!(last_traffic == 0 && data['total_count'] == 0)) {
183192
last_traffic = Date.now();
193+
} else {
194+
moveTimeline();
195+
}
196+
var aData = Array.isArray(data) ? data : [data];
197+
var elements = [];
198+
var elements_cnt = [];
199+
var timestamp_max = Date.now()/1000 - 60*5; // 5 minutes in the past
200+
while (dataitem = aData.shift()) {
201+
var timestamp = dataitem['timestamp']
202+
if (timestamp > timestamp_max) {
203+
timestamp_max = timestamp;
204+
}
205+
var d = new Date(timestamp * 1000);
206+
elements.push({
207+
x: d,
208+
y: dataitem['total_size'],
209+
group: 0
210+
});
211+
elements_cnt.push({
212+
x: d,
213+
y: dataitem['total_count'],
214+
group: 1
215+
});
216+
217+
// Add the new flows
218+
var arr = dataitem['flows'];
219+
for (var i = 0, len = arr.length; i < len; i++) {
220+
var f = arr[i];
221+
// defined in spingraph.js
222+
//alert("FIND NODE: " + f['from'])
223+
var from_node = f['from'];
224+
var to_node = f['to'];
225+
226+
if (from_node != null && to_node != null) {
227+
addFlow(timestamp + time_sync, from_node, to_node, f['count'], f['size']);
228+
} else {
229+
console.error("partial message: " + JSON.stringify(data))
230+
}
231+
}
184232
}
233+
traffic_dataset.add(elements.concat(elements_cnt));
234+
// console.log("Graph updated")
235+
// var ids = traffic_dataset.getIds();
185236

186-
var timestamp = data['timestamp']
187-
188-
// update traffic graph
189-
var d = new Date(timestamp * 1000);
190-
traffic_dataset.add({
191-
x: d,
192-
y: data['total_size'],
193-
group: 0
194-
});
195-
traffic_dataset.add({
196-
x: d,
197-
y: data['total_count'],
198-
group: 1
199-
});
237+
var graph_end = Date.parse(graph2d_1.options.end);
238+
if (Date.now() + 10000 >= graph_end) {
239+
moveTimeline(timestamp_max);
240+
}
241+
}
200242

243+
// Function to redraw the graph
244+
function moveTimeline(maxtime) {
245+
// Only rescale graph every minute. Rescale if current date gets within 10 seconds of maximum
246+
if (typeof maxtime == 'undefined') {
247+
maxtime = Date.now()/1000;
248+
}
249+
var start = Date.parse(graph2d_1.options.start);
250+
201251
var options = {
202-
start: new Date((timestamp * 1000) - 600000),
203-
end: d,
252+
start: Date.now() - 700000 > start ? new Date(Date.now() - 600000) : graph2d_1.options.start,
253+
end: start > Date.now() - 600000 ? graph2d_1.options.end : new Date(maxtime*1000),
204254
height: '140px',
205255
drawPoints: false,
256+
dataAxis: {
257+
left: {
258+
range: {min: 0}
259+
},
260+
right: {
261+
range: {min: 0}
262+
}
263+
}
206264
};
207265
graph2d_1.setOptions(options);
208-
var ids = traffic_dataset.getIds();
209-
210-
//
211-
// update network view
212-
//
213-
214-
// Add the new flows
215-
var arr = data['flows'];
216-
for (var i = 0, len = arr.length; i < len; i++) {
217-
var f = arr[i];
218-
// defined in spingraph.js
219-
//alert("FIND NODE: " + f['from'])
220-
var from_node = f['from'];
221-
var to_node = f['to'];
222-
223-
if (from_node != null && to_node != null) {
224-
addFlow(timestamp + time_sync, from_node, to_node, f['count'], f['size']);
225-
} else {
226-
console.error("partial message: " + JSON.stringify(data))
227-
}
228-
}
229266
}
230267

231268
function handleBlockedMessage(data) {

html/js/spingraphs.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,13 @@ function showGraph(dataset) {
517517

518518
// Graph options
519519
var options = {
520-
//start: '2017-01-26',
521-
//end: '2017-01-28',
520+
start: new Date(Date.now()),
521+
end: new Date(Date.now() + 600000),
522522
height: '140px',
523523
drawPoints: false,
524+
zoomable: false,
525+
moveable: false,
526+
showCurrentTime: true,
524527
//clickToUse: true
525528
};
526529

@@ -574,14 +577,16 @@ function showNetwork() {
574577
stabilization: {
575578
enabled: true,
576579
iterations: 5,
580+
updateInterval: 10,
577581
}
578582
},
579583
nodes: {
580584
shadow:shadowState
581585
},
582586
edges: {
583587
shadow:shadowState,
584-
arrows:'to',smooth:true
588+
arrows:'to',
589+
smooth:true
585590
}
586591
};
587592
network = new vis.Network(container, data, options);
@@ -638,6 +643,7 @@ function nodeSelected(event) {
638643

639644
function updateBlockedButton() {
640645
var node = nodes.get(selectedNodeId);
646+
if (node == null) return;
641647
var label = node.is_blocked ? "Unblock node" : "Block node";
642648
$("#block-node-button").button("option", {
643649
"label": label
@@ -646,6 +652,7 @@ function updateBlockedButton() {
646652

647653
function updateAllowedButton() {
648654
var node = nodes.get(selectedNodeId);
655+
if (node == null) return;
649656
var label = node.is_excepted ? "Stop allowing node" : "Allow node";
650657
$("#allow-node-button").button("option", {
651658
"label": label

0 commit comments

Comments
 (0)