1
|
|
|
define(['moment', 'utils/router', 'leaflet', 'gui', 'helper', 'utils/language'], |
2
|
|
|
function (moment, Router, L, GUI, helper, Language) { |
3
|
|
|
'use strict'; |
4
|
|
|
|
5
|
|
|
return function (config) { |
6
|
|
|
function handleData(data) { |
7
|
|
|
var timestamp; |
8
|
|
|
var nodes = []; |
9
|
|
|
var links = []; |
10
|
|
|
var gateways = {}; |
11
|
|
|
|
12
|
|
|
for (var i = 0; i < data.length; ++i) { |
13
|
|
|
nodes = nodes.concat(data[i].nodes); |
14
|
|
|
timestamp = data[i].timestamp; |
15
|
|
|
links = links.concat(data[i].links.filter(function (d) { |
16
|
|
|
return d.source !== undefined; |
17
|
|
|
})); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
nodes.forEach(function (node) { |
21
|
|
|
node.firstseen = moment.utc(node.firstseen).local(); |
22
|
|
|
node.lastseen = moment.utc(node.lastseen).local(); |
23
|
|
|
}); |
24
|
|
|
|
25
|
|
|
var now = moment(); |
26
|
|
|
var age = moment(now).subtract(config.maxAge, 'days'); |
27
|
|
|
|
28
|
|
|
var newnodes = helper.limit('firstseen', age, helper.sortByKey('firstseen', nodes).filter(helper.online)); |
29
|
|
|
var lostnodes = helper.limit('lastseen', age, helper.sortByKey('lastseen', nodes).filter(helper.offline)); |
30
|
|
|
|
31
|
|
|
nodes.forEach(function (d) { |
32
|
|
|
d.neighbours = []; |
33
|
|
|
if (d.is_gateway && d.network.mesh) { |
34
|
|
|
var mesh = d.network.mesh; |
35
|
|
|
mesh[Object.keys(mesh)[0]].interfaces.tunnel.forEach(function (mac) { |
36
|
|
|
gateways[mac] = d.hostname; |
37
|
|
|
}); |
38
|
|
|
} |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
links.forEach(function (d) { |
42
|
|
|
var ids; |
43
|
|
|
|
44
|
|
|
d.source = nodes.find(function (a) { |
45
|
|
|
return a.node_id === d.source; |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
d.target = nodes.find(function (a) { |
49
|
|
|
return a.node_id === d.target; |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
ids = [d.source.node_id, d.target.node_id]; |
53
|
|
|
d.source.neighbours.push({ node: d.target, link: d, incoming: false }); |
54
|
|
|
d.target.neighbours.push({ node: d.source, link: d, incoming: true }); |
55
|
|
|
|
56
|
|
|
d.id = ids.join('-'); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
d.latlngs = []; |
60
|
|
|
d.latlngs.push(L.latLng(d.source.location.latitude, d.source.location.longitude)); |
61
|
|
|
d.latlngs.push(L.latLng(d.target.location.latitude, d.target.location.longitude)); |
62
|
|
|
|
63
|
|
|
d.distance = d.latlngs[0].distanceTo(d.latlngs[1]); |
64
|
|
|
} catch (e) { |
65
|
|
|
// ignore exception |
66
|
|
|
} |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
links.sort(function (a, b) { |
70
|
|
|
return b.source_tq - a.source_tq; |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
return { |
74
|
|
|
now: now, |
75
|
|
|
timestamp: moment.utc(timestamp).local(), |
|
|
|
|
76
|
|
|
nodes: { |
77
|
|
|
all: nodes, |
78
|
|
|
new: newnodes, |
79
|
|
|
lost: lostnodes |
80
|
|
|
}, |
81
|
|
|
links: links, |
82
|
|
|
graph: { |
83
|
|
|
links: [], |
84
|
|
|
nodes: [] |
85
|
|
|
}, |
86
|
|
|
gateways: gateways |
87
|
|
|
}; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
var language = new Language(config); |
91
|
|
|
var router = new Router(language); |
92
|
|
|
|
93
|
|
|
var urls = []; |
94
|
|
|
|
95
|
|
|
if (typeof config.dataPath === 'string' || config.dataPath instanceof String) { |
96
|
|
|
config.dataPath = [config.dataPath]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
for (var i in config.dataPath) { |
100
|
|
|
if (config.dataPath.hasOwnProperty(i)) { |
101
|
|
|
urls.push(config.dataPath[i] + 'meshviewer.json'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
function update() { |
106
|
|
|
language.init(router); |
107
|
|
|
return Promise.all(urls.map(helper.getJSON)) |
108
|
|
|
.then(handleData); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
update() |
112
|
|
|
.then(function (d) { |
113
|
|
|
var gui = new GUI(config, router, language); |
114
|
|
|
gui.setData(d); |
115
|
|
|
router.setData(d); |
116
|
|
|
router.resolve(); |
117
|
|
|
|
118
|
|
|
window.setInterval(function () { |
119
|
|
|
update().then(function (n) { |
120
|
|
|
gui.setData(n); |
121
|
|
|
router.setData(n); |
122
|
|
|
}); |
123
|
|
|
}, 60000); |
124
|
|
|
}) |
125
|
|
|
.catch(function (e) { |
126
|
|
|
document.querySelector('.loader').innerHTML += e.message |
127
|
|
|
+ '<br /><br /><button onclick="location.reload(true)" class="btn text">Try to reload</button><br /> or report to your community'; |
128
|
|
|
console.warn(e); |
129
|
|
|
}); |
130
|
|
|
}; |
131
|
|
|
}); |
132
|
|
|
|