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