Completed
Pull Request — future (#197)
by Xaver
46s
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(helper.online);
27
        var offline = nodes.filter(helper.offline);
28
29
        var newnodes = helper.limit('firstseen', age, helper.sortByKey('firstseen', online));
30
        var lostnodes = helper.limit('lastseen', age, helper.sortByKey('lastseen', offline));
31
32
        nodes.forEach(function (d) {
33
          d.neighbours = [];
34
          nodeDict[d.node_id] = d;
35
        });
36
37
        links.forEach(function (d) {
38
          d.source = nodeDict[d.source];
39
          d.target = nodeDict[d.target];
40
41
          d.id = [d.source.node_id, d.target.node_id].join('-');
42
          d.source.neighbours.push({ node: d.target, link: d });
43
          d.target.neighbours.push({ node: d.source, link: d });
44
45
          try {
46
            d.latlngs = [];
47
            d.latlngs.push(L.latLng(d.source.location.latitude, d.source.location.longitude));
48
            d.latlngs.push(L.latLng(d.target.location.latitude, d.target.location.longitude));
49
50
            d.distance = d.latlngs[0].distanceTo(d.latlngs[1]);
51
          } catch (e) {
52
            // ignore exception
53
          }
54
        });
55
56
        return {
57
          now: now,
58
          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...
59
          nodes: {
60
            online: online,
61
            offline: offline,
62
            new: newnodes,
63
            lost: lostnodes
64
          },
65
          links: links,
66
          nodeDict: nodeDict
67
        };
68
      }
69
70
      var language = new Language(config);
71
      var router = new Router(language);
72
73
      config.dataPath.forEach(function (d, i) {
74
        config.dataPath[i] += 'meshviewer.json';
75
      });
76
77
      language.init(router);
78
79
      function update() {
80
        return Promise.all(config.dataPath.map(helper.getJSON))
81
          .then(handleData);
82
      }
83
84
      update()
85
        .then(function (d) {
86
          var gui = new GUI(config, router, language);
87
          gui.setData(d);
88
          router.setData(d);
89
          router.resolve();
90
91
          window.setInterval(function () {
92
            update().then(function (n) {
93
              gui.setData(n);
94
              router.setData(n);
95
            });
96
          }, 60000);
97
        })
98
        .catch(function (e) {
99
          document.querySelector('.loader').innerHTML += e.message
100
            + '<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';
101
          console.warn(e);
102
        });
103
    };
104
  });
105