@@ -4,6 +4,8 @@ var last_traffic = 0 // Last received traffic trace
44var time_sync = 0 ; // Time difference (seconds) between server and client
55var 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+
79function 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
2224function 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
231268function handleBlockedMessage ( data ) {
0 commit comments