Completed
Pull Request — develop (#147)
by
unknown
40s
created

node.js ➔ ... ➔   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

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 8
rs 9.4285
nop 1
1
define(['sorttable', 'snabbdom', 'd3-interpolate', 'moment', 'helper'],
2
  function (SortTable, V, d3Interpolate, moment, helper) {
3
    'use strict';
4
    V = V.default;
5
6
    function showGeoURI(d) {
7
      if (!helper.hasLocation(d)) {
8
        return undefined;
9
      }
10
11
      return function (el) {
12
        var a = document.createElement('a');
13
        a.textContent = Number(d.nodeinfo.location.latitude.toFixed(6)) + ', ' + Number(d.nodeinfo.location.longitude.toFixed(6));
14
        a.href = 'geo:' + d.nodeinfo.location.latitude + ',' + d.nodeinfo.location.longitude;
15
        el.appendChild(a);
16
      };
17
    }
18
19
    function showStatus(d) {
20
      return function (el) {
21
        el.classList.add(d.flags.unseen ? 'unseen' : (d.flags.online ? 'online' : 'offline'));
22
        el.textContent = _.t((d.flags.online ? 'node.lastOnline' : 'node.lastOffline'), {
23
          time: d.lastseen.fromNow(),
24
          date: d.lastseen.format('DD.MM.YYYY, H:mm:ss')
25
        });
26
      };
27
    }
28
29
    function showFirmware(d) {
30
      var release = helper.dictGet(d.nodeinfo, ['software', 'firmware', 'release']);
31
      var base = helper.dictGet(d.nodeinfo, ['software', 'firmware', 'base']);
32
33
      if (release === null || base === null) {
34
        return undefined;
35
      }
36
37
      return release + ' / ' + base;
38
    }
39
40
    function showSite(d, config) {
41
      var site = helper.dictGet(d.nodeinfo, ['system', 'site_code']);
42
      var name = site;
43
      var link = null;
44
45
      if (config.siteNames) {
46
        config.siteNames.forEach(function (t) {
47
          if (site === t.site) {
48
            name = t.name;
49
            link = t.link;
50
          }
51
        });
52
      }
53
54
      return function (el) {
55
        var a = document.createElement('a');
56
        if (link) {
57
          a.href = link;
58
        }
59
        a.textContent = name;
60
        el.appendChild(a);
61
      };
62
    }
63
64
    function showUptime(d) {
65
      if (!('uptime' in d.statistics)) {
66
        return undefined;
67
      }
68
69
      return moment.duration(d.statistics.uptime, 'seconds').humanize();
70
    }
71
72
    function showFirstseen(d) {
73
      if (!('firstseen' in d)) {
74
        return undefined;
75
      }
76
77
      return d.firstseen.fromNow(true);
78
    }
79
80
    function showClients(d) {
81
      if (!d.flags.online) {
82
        return undefined;
83
      }
84
85
      return function (el) {
86
        el.appendChild(document.createTextNode(d.statistics.clients > 0 ? d.statistics.clients : _.t('none')));
87
        el.appendChild(document.createElement('br'));
88
89
        var span = document.createElement('span');
90
        span.classList.add('clients');
91
        span.innerHTML = '<i class="ion-person"></i>'.repeat(d.statistics.clients);
92
        el.appendChild(span);
93
      };
94
    }
95
96
    function showIPs(d) {
97
      var ips = helper.dictGet(d.nodeinfo, ['network', 'addresses']);
98
      if (ips === null) {
99
        return undefined;
100
      }
101
102
      ips.sort();
103
104
      return function (el) {
105
        ips.forEach(function (ip, i) {
106
          var link = !ip.startsWith('fe80:');
107
108
          if (i > 0) {
109
            el.appendChild(document.createElement('br'));
110
          }
111
112
          if (link) {
113
            var a = document.createElement('a');
114
            a.href = 'http://[' + ip + ']/';
115
            a.textContent = ip;
116
            el.appendChild(a);
117
          } else {
118
            el.appendChild(document.createTextNode(ip));
119
          }
120
        });
121
      };
122
    }
123
124
    function showBar(v, width, warning) {
125
      var span = document.createElement('span');
126
      span.classList.add('bar');
127
128
      var bar = document.createElement('span');
129
      bar.style.width = (width * 100) + '%';
130
      if (warning) {
131
        span.classList.add('warning');
132
      }
133
      span.appendChild(bar);
134
135
      var label = document.createElement('label');
136
      label.textContent = v;
137
      span.appendChild(label);
138
139
      return span;
140
    }
141
142
    function showLoad(d) {
143
      if (!('loadavg' in d.statistics)) {
144
        return undefined;
145
      }
146
147
      return function (el) {
148
        var value = d.statistics.loadavg.toFixed(2);
149
        var width = d.statistics.loadavg % 1;
150
        var warning = false;
151
        if (d.statistics.loadavg >= d.nodeinfo.hardware.nproc) {
152
          warning = true;
153
        }
154
        el.appendChild(showBar(value, width, warning));
155
      };
156
    }
157
158
    function showRAM(d) {
159
      if (!('memory_usage' in d.statistics)) {
160
        return undefined;
161
      }
162
163
      return function (el) {
164
        var value = Math.round(d.statistics.memory_usage * 100) + ' %';
165
        var width = d.statistics.memory_usage;
166
        var warning = false;
167
        if (d.statistics.memory_usage >= 0.8) {
168
          warning = true;
169
        }
170
        el.appendChild(showBar(value, width, warning));
171
      };
172
    }
173
174
    function showAutoupdate(d) {
175
      var au = helper.dictGet(d.nodeinfo, ['software', 'autoupdater']);
176
      if (!au) {
177
        return undefined;
178
      }
179
180
      return au.enabled ? _.t('node.activated', { branch: au.branch }) : _.t('node.deactivated');
181
    }
182
183
    function showStatImg(o, d) {
184
      var subst = {};
185
      subst['{NODE_ID}'] = d.nodeinfo.node_id;
186
      subst['{NODE_NAME}'] = d.nodeinfo.hostname.replace(/[^a-z0-9\-]/ig, '_');
187
      subst['{TIME}'] = d.lastseen.format('DDMMYYYYHmmss');
188
      subst['{LOCALE}'] = _.locale();
189
      return helper.showStat(o, subst);
190
    }
191
192
    return function (config, el, router, d, gateways) {
193
      var linkScale = d3Interpolate.interpolate('#F02311', '#04C714');
194
195
      function renderNeighbourRow(n) {
196
        var icons = [];
197
        icons.push(V.h('span', { props: { className: n.incoming ? 'ion-arrow-left-c' : 'ion-arrow-right-c' } }));
198
        if (helper.hasLocation(n.node)) {
199
          icons.push(V.h('span', { props: { className: 'ion-location' } }));
200
        }
201
202
        var name = V.h('a', {
203
          props: {
204
            className: 'online',
205
            href: router.generateLink({ node: n.node.nodeinfo.node_id })
206
          }, on: {
207
            click: function (e) {
208
              router.fullUrl({ node: n.node.nodeinfo.node_id }, e);
209
            }
210
          }
211
        }, n.node.nodeinfo.hostname);
212
213
        var td1 = V.h('td', icons);
214
        var td2 = V.h('td', name);
215
        var td3 = V.h('td', (n.node.statistics.clients ? n.node.statistics.clients.toString() : '0'));
216
        var td4 = V.h('td', { style: { color: linkScale(1 / n.link.tq) } }, helper.showTq(n.link));
217
        var td5 = V.h('td', helper.showDistance(n.link));
218
219
        return V.h('tr', [td1, td2, td3, td4, td5]);
220
      }
221
222
      var h2 = document.createElement('h2');
223
      h2.textContent = d.nodeinfo.hostname;
224
      el.appendChild(h2);
225
226
      var attributes = document.createElement('table');
227
      attributes.classList.add('attributes');
228
229
      helper.attributeEntry(attributes, 'node.status', showStatus(d));
230
      helper.attributeEntry(attributes, 'node.gateway', d.flags.gateway ? 'ja' : null);
231
      helper.attributeEntry(attributes, 'node.coordinates', showGeoURI(d));
232
233
      if (config.nodeInfobox && config.nodeInfobox.contact) {
234
        helper.attributeEntry(attributes, 'node.contact', helper.dictGet(d.nodeinfo, ['owner', 'contact']));
235
      }
236
237
      helper.attributeEntry(attributes, 'node.hardware', helper.dictGet(d.nodeinfo, ['hardware', 'model']));
238
      helper.attributeEntry(attributes, 'node.primaryMac', helper.dictGet(d.nodeinfo, ['network', 'mac']));
239
      helper.attributeEntry(attributes, 'node.id', helper.dictGet(d.nodeinfo, ['node_id']));
240
      helper.attributeEntry(attributes, 'node.firmware', showFirmware(d));
241
      helper.attributeEntry(attributes, 'node.site', showSite(d, config));
242
      helper.attributeEntry(attributes, 'node.uptime', showUptime(d));
243
      helper.attributeEntry(attributes, 'node.firstSeen', showFirstseen(d));
244
      if (config.nodeInfobox && config.nodeInfobox.hardwareUsage) {
245
        helper.attributeEntry(attributes, 'node.systemLoad', showLoad(d));
246
        helper.attributeEntry(attributes, 'node.ram', showRAM(d));
247
      }
248
      helper.attributeEntry(attributes, 'node.ipAddresses', showIPs(d));
249
      helper.attributeEntry(attributes, 'node.selectedGateway', gateways[helper.dictGet(d.statistics, ['gateway'])]);
250
      helper.attributeEntry(attributes, 'node.update', showAutoupdate(d));
251
      helper.attributeEntry(attributes, 'node.clients', showClients(d));
252
253
      el.appendChild(attributes);
254
255
      if (d.neighbours.length > 0) {
256
        var h3 = document.createElement('h3');
257
        h3.textContent = _.t('node.link', d.neighbours.length) + '(' + d.neighbours.length + ')';
258
        el.appendChild(h3);
259
260
        var headings = [{
261
          name: ''
262
        }, {
263
          name: 'node.nodes',
264
          sort: function (a, b) {
265
            return a.node.nodeinfo.hostname.localeCompare(b.node.nodeinfo.hostname);
266
          },
267
          reverse: false
268
        }, {
269
          name: 'node.clients',
270
          class: 'ion-people',
271
          sort: function (a, b) {
272
            return ('clients' in a.node.statistics ? a.node.statistics.clients : -1) -
273
              ('clients' in b.node.statistics ? b.node.statistics.clients : -1);
274
          },
275
          reverse: true
276
        }, {
277
          name: 'node.tq',
278
          class: 'ion-connection-bars',
279
          sort: function (a, b) {
280
            return a.link.tq - b.link.tq;
281
          },
282
          reverse: true
283
        }, {
284
          name: 'node.distance',
285
          class: 'ion-arrow-resize',
286
          sort: function (a, b) {
287
            return (a.link.distance === undefined ? -1 : a.link.distance) -
288
              (b.link.distance === undefined ? -1 : b.link.distance);
289
          },
290
          reverse: true
291
        }];
292
293
        var table = new SortTable(headings, 1, renderNeighbourRow);
294
        table.setData(d.neighbours);
295
        table.el.elm.classList.add('node-links');
296
        el.appendChild(table.el.elm);
297
      }
298
299
      if (config.nodeInfos) {
300
        config.nodeInfos.forEach(function (nodeInfo) {
301
          var h4 = document.createElement('h4');
302
          h4.textContent = nodeInfo.name;
303
          el.appendChild(h4);
304
          el.appendChild(showStatImg(nodeInfo, d));
305
        });
306
      }
307
    };
308
  });
309