Completed
Push — future ( e02ec5...470785 )
by Xaver
39s
created

main.js ➔ ... ➔ config.dataPath.forEach   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 2
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 nodeDict = {};
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);
16
        }
17
18
        nodes.forEach(function (node) {
19
          node.firstseen = moment.utc(node.firstseen).local();
20
          node.lastseen = moment.utc(node.lastseen).local();
21
        });
22
23
        var now = moment();
24
        var age = moment(now).subtract(config.maxAge, 'days');
25
26
        var online = nodes.filter(function () {
27
          return d.is_online;
28
        });
29
        var offline = nodes.filter(function () {
30
          return !d.is_online;
31
        });
32
33
        var newnodes = helper.limit('firstseen', age, helper.sortByKey('firstseen', online));
34
        var lostnodes = helper.limit('lastseen', age, helper.sortByKey('lastseen', offline));
35
36
        nodes.forEach(function (d) {
37
          d.neighbours = [];
38
          nodeDict[d.node_id] = d;
39
        });
40
41
        links.forEach(function (d) {
42
          d.source = nodeDict[d.source];
43
          d.target = nodeDict[d.target];
44
45
          d.id = [d.source.node_id, d.target.node_id].join('-');
46
          d.source.neighbours.push({ node: d.target, link: d });
47
          d.target.neighbours.push({ node: d.source, link: d });
48
49
          try {
50
            d.latlngs = [];
51
            d.latlngs.push(L.latLng(d.source.location.latitude, d.source.location.longitude));
52
            d.latlngs.push(L.latLng(d.target.location.latitude, d.target.location.longitude));
53
54
            d.distance = d.latlngs[0].distanceTo(d.latlngs[1]);
55
          } catch (e) {
56
            // ignore exception
57
          }
58
        });
59
60
        return {
61
          now: now,
62
          timestamp: moment.utc(timestamp).local(),
1 ignored issue
show
Bug introduced by
The variable timestamp seems to not be initialized for all possible execution paths. Are you sure utc handles undefined variables?
Loading history...
63
          nodes: {
64
            all: nodes,
65
            online: online,
66
            offline: offline,
67
            new: newnodes,
68
            lost: lostnodes
69
          },
70
          links: links,
71
          nodeDict: nodeDict
72
        };
73
      }
74
75
      var language = new Language(config);
76
      var router = new Router(language);
77
78
      config.dataPath.forEach(function (d, i) {
79
        config.dataPath[i] += 'meshviewer.json';
80
      });
81
82
      language.init(router);
83
84
      function update() {
85
        return Promise.all(config.dataPath.map(helper.getJSON))
86
          .then(handleData);
87
      }
88
89
      update()
90
        .then(function (d) {
91
          var gui = new GUI(config, router, language);
92
          gui.setData(d);
93
          router.setData(d);
94
          router.resolve();
95
96
          window.setInterval(function () {
97
            update().then(function (n) {
98
              gui.setData(n);
99
              router.setData(n);
100
            });
101
          }, 60000);
102
        })
103
        .catch(function (e) {
104
          document.querySelector('.loader').innerHTML += e.message
105
            + '<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';
106
          console.warn(e);
107
        });
108
    };
109
  });
110