Completed
Pull Request — develop (#211)
by
unknown
31s
created

main.js ➔ ... ➔ ensureLanguageLoaded   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
dl 0
loc 17
rs 9.4285
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A main.js ➔ ... ➔ window.setInterval 0 6 1
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 () {
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 (d) {
27
          return d.is_online;
28
        });
29
        var offline = nodes.filter(function (d) {
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(),
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();
76
      window.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
          function ensureLanguageLoaded() {
92
            if (_.phrases.yes === undefined) {
93
              window.setTimeout(ensureLanguageLoaded, 20);
94
              return;
95
            }
96
            var gui = new GUI(language);
97
            gui.setData(d);
98
            router.setData(d);
99
            router.resolve();
100
101
            window.setInterval(function () {
102
              update().then(function (n) {
103
                gui.setData(n);
104
                router.setData(n);
105
              });
106
            }, 60000);
107
          }
108
109
          ensureLanguageLoaded();
110
        })
111
        .catch(function (e) {
112
          document.querySelector('.loader').innerHTML += e.message
113
            + '<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';
114
          console.warn(e);
115
        });
116
    };
117
  });
118