2017-03-05 11:29:21 +00:00
define ( [ 'moment' , 'utils/router' , 'leaflet' , 'gui' , 'helper' , 'utils/language' ] ,
function ( moment , Router , L , GUI , helper , Language ) {
2017-01-29 23:51:08 +00:00
'use strict' ;
2016-05-27 21:59:01 +00:00
2016-05-22 12:51:30 +00:00
return function ( config ) {
function handleData ( data ) {
2017-10-29 14:11:24 +00:00
var timestamp ;
var nodes = [ ] ;
var links = [ ] ;
2017-04-22 22:03:42 +00:00
var gateways = { } ;
2016-05-22 12:51:30 +00:00
for ( var i = 0 ; i < data . length ; ++ i ) {
2017-10-29 14:11:24 +00:00
nodes = nodes . concat ( data [ i ] . nodes ) ;
timestamp = data [ i ] . timestamp ;
2017-10-27 19:17:43 +00:00
links = links . concat ( data [ i ] . links ) ;
2016-05-22 12:51:30 +00:00
}
2015-04-07 19:10:37 +00:00
2016-05-22 12:51:30 +00:00
nodes . forEach ( function ( node ) {
node . firstseen = moment . utc ( node . firstseen ) . local ( ) ;
node . lastseen = moment . utc ( node . lastseen ) . local ( ) ;
} ) ;
2015-03-24 23:54:00 +00:00
2016-05-22 12:51:30 +00:00
var now = moment ( ) ;
2017-01-29 23:51:08 +00:00
var age = moment ( now ) . subtract ( config . maxAge , 'days' ) ;
2015-03-24 23:54:00 +00:00
2017-01-29 23:51:08 +00:00
var newnodes = helper . limit ( 'firstseen' , age , helper . sortByKey ( 'firstseen' , nodes ) . filter ( helper . online ) ) ;
var lostnodes = helper . limit ( 'lastseen' , age , helper . sortByKey ( 'lastseen' , nodes ) . filter ( helper . offline ) ) ;
2015-03-24 23:54:00 +00:00
2017-02-12 23:31:31 +00:00
nodes . forEach ( function ( d ) {
d . neighbours = [ ] ;
2017-10-29 14:11:24 +00:00
if ( d . is _gateway && d . network . mesh ) {
var mesh = d . network . mesh ;
2017-04-22 22:03:42 +00:00
mesh [ Object . keys ( mesh ) [ 0 ] ] . interfaces . tunnel . forEach ( function ( mac ) {
2017-10-29 14:11:24 +00:00
gateways [ mac ] = d . hostname ;
2017-04-22 22:03:42 +00:00
} ) ;
}
2017-02-12 23:31:31 +00:00
} ) ;
2016-05-22 12:51:30 +00:00
links . forEach ( function ( d ) {
var ids ;
2017-02-12 23:31:31 +00:00
2017-10-29 14:11:24 +00:00
d . source = nodes . find ( function ( a ) {
return a . node _id === d . source ;
} ) ;
d . target = nodes . find ( function ( a ) {
return a . node _id === d . target ;
} ) ;
ids = [ d . source . node _id , d . target . node _id ] ;
d . source . neighbours . push ( { node : d . target , link : d , incoming : false } ) ;
d . target . neighbours . push ( { node : d . source , link : d , incoming : true } ) ;
2016-05-22 12:51:30 +00:00
2017-02-12 23:31:31 +00:00
d . id = ids . join ( '-' ) ;
2016-05-22 12:51:30 +00:00
2017-02-12 23:31:31 +00:00
try {
d . latlngs = [ ] ;
2017-10-29 14:11:24 +00:00
d . latlngs . push ( L . latLng ( d . source . location . latitude , d . source . location . longitude ) ) ;
d . latlngs . push ( L . latLng ( d . target . location . latitude , d . target . location . longitude ) ) ;
2016-05-22 12:51:30 +00:00
2017-02-12 23:31:31 +00:00
d . distance = d . latlngs [ 0 ] . distanceTo ( d . latlngs [ 1 ] ) ;
} catch ( e ) {
// ignore exception
2016-05-22 12:51:30 +00:00
}
} ) ;
links . sort ( function ( a , b ) {
2017-10-29 14:11:24 +00:00
return b . source _tq - a . source _tq ;
2016-05-22 12:51:30 +00:00
} ) ;
return {
now : now ,
2017-10-29 14:11:24 +00:00
timestamp : moment . utc ( timestamp ) . local ( ) ,
2016-05-22 12:51:30 +00:00
nodes : {
all : nodes ,
new : newnodes ,
lost : lostnodes
} ,
2017-10-29 14:11:24 +00:00
links : links ,
2016-05-22 12:51:30 +00:00
graph : {
2017-10-29 14:11:24 +00:00
links : [ ] ,
nodes : [ ]
2017-04-22 22:03:42 +00:00
} ,
gateways : gateways
2016-05-22 12:51:30 +00:00
} ;
}
2016-05-26 16:37:24 +00:00
2017-02-12 00:29:37 +00:00
var language = new Language ( config ) ;
2017-03-05 11:29:21 +00:00
var router = new Router ( language ) ;
2016-05-22 12:51:30 +00:00
var urls = [ ] ;
2017-01-29 23:51:08 +00:00
if ( typeof config . dataPath === 'string' || config . dataPath instanceof String ) {
2016-05-22 12:51:30 +00:00
config . dataPath = [ config . dataPath ] ;
}
for ( var i in config . dataPath ) {
2017-04-30 12:59:39 +00:00
if ( config . dataPath . hasOwnProperty ( i ) ) {
2017-10-29 14:11:24 +00:00
urls . push ( config . dataPath [ i ] + 'meshviewer.json' ) ;
2017-04-30 12:59:39 +00:00
}
2016-05-22 12:51:30 +00:00
}
function update ( ) {
2017-03-05 11:29:21 +00:00
language . init ( router ) ;
2016-05-26 16:37:24 +00:00
return Promise . all ( urls . map ( helper . getJSON ) )
2016-05-22 12:51:30 +00:00
. then ( handleData ) ;
}
update ( )
. then ( function ( d ) {
2017-02-12 00:29:37 +00:00
var gui = new GUI ( config , router , language ) ;
2016-05-22 12:51:30 +00:00
gui . setData ( d ) ;
router . setData ( d ) ;
2017-03-05 11:29:21 +00:00
router . resolve ( ) ;
2016-05-22 12:51:30 +00:00
window . setInterval ( function ( ) {
2017-01-29 23:51:08 +00:00
update ( ) . then ( function ( n ) {
gui . setData ( n ) ;
router . setData ( n ) ;
2016-05-22 12:51:30 +00:00
} ) ;
} , 60000 ) ;
} )
. catch ( function ( e ) {
2017-03-18 17:53:59 +00:00
document . querySelector ( '.loader' ) . innerHTML += e . message
2017-10-21 00:06:42 +00:00
+ '<br /><br /><button onclick="location.reload(true)" class="btn text" aria-label="Try to reload">Try to reload</button><br /> or report to your community' ;
2016-05-26 16:37:24 +00:00
console . warn ( e ) ;
2016-05-22 12:51:30 +00:00
} ) ;
} ;
} ) ;