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

node.js ➔ ... ➔   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

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