Completed
Pull Request — future (#174)
by Xaver
32s
created

node.js ➔ ... ➔ nodeLink   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A node.js ➔ ... ➔ V.h(ꞌaꞌ).on.click 0 3 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 V.h('td',
12
        V.h('a',
13
          { props: { href: 'geo:' + d.location.latitude + ',' + d.location.longitude } },
14
          Number(d.location.latitude.toFixed(6)) + ', ' + Number(d.location.longitude.toFixed(6))
15
        )
16
      );
17
    }
18
19
    function showStatus(d) {
20
      return V.h('td',
21
        { props: { className: d.is_unseen ? 'unseen' : (d.is_online ? 'online' : 'offline') } },
22
        _.t((d.is_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
    function showFirmware(d) {
29
      return [
30
        helper.dictGet(d, ['firmware', 'release']),
31
        helper.dictGet(d, ['firmware', 'base'])
32
      ].filter(function (n) {
33
        return n !== null;
34
      }).join(' / ') || undefined;
35
    }
36
37
    function showSite(d, config) {
38
      var site = helper.dictGet(d, ['site_code']);
39
      var rt = site;
40
      if (config.siteNames) {
41
        config.siteNames.forEach(function (t) {
42
          if (site === t.site) {
43
            rt = t.name;
44
          }
45
        });
46
      }
47
      return rt || undefined;
48
    }
49
50
    function showUptime(d) {
51
      if (!('uptime' in d)) {
52
        return undefined;
53
      }
54
55
      return moment.utc(d.uptime).local().fromNow(true);
56
    }
57
58
    function showFirstseen(d) {
59
      if (!('firstseen' in d)) {
60
        return undefined;
61
      }
62
63
      return d.firstseen.fromNow(true);
64
    }
65
66
    function showClients(d) {
67
      if (!d.is_online) {
68
        return undefined;
69
      }
70
71
      var clients = [
72
        V.h('span', [
73
          d.clients > 0 ? d.clients : _.t('none'),
74
          V.h('br'),
75
          V.h('i', { props: { className: 'ion-people', title: _.t('node.clients') } })
76
        ]),
77
        V.h('span',
78
          { props: { className: 'legend-24ghz' } },
79
          [
80
            d.clients_wifi24,
81
            V.h('br'),
82
            V.h('span', { props: { className: 'symbol', title: '2,4 Ghz' } })
83
          ]),
84
        V.h('span',
85
          { props: { className: 'legend-5ghz' } },
86
          [
87
            d.clients_wifi5,
88
            V.h('br'),
89
            V.h('span', { props: { className: 'symbol', title: '5 Ghz' } })
90
          ]),
91
        V.h('span',
92
          { props: { className: 'legend-others' } },
93
          [
94
            d.clients_other,
95
            V.h('br'),
96
            V.h('span', { props: { className: 'symbol', title: _.t('others') } })
97
          ])
98
      ];
99
100
      return V.h('td', { props: { className: 'clients' } }, clients);
101
    }
102
103
    function showIPs(d) {
104
      var ips = helper.dictGet(d, ['network', 'addresses']);
105
      if (ips === null) {
106
        return undefined;
107
      }
108
109
      ips.sort();
110
111
      var string = [];
112
      ips.forEach(function (ip, i) {
113
        var link = !ip.startsWith('fe80:');
114
115
        if (i > 0) {
116
          string.push(V.h('br'));
117
        }
118
119
        if (link) {
120
          string.push(V.h('a', { props: { href: 'http://[' + ip + ']/', target: '_blank' } }, ip));
121
        } else {
122
          string.push(ip);
123
        }
124
      });
125
      return V.h('td', string);
126
    }
127
128
    function showBar(v, width, warning) {
129
      return V.h('span',
130
        { props: { className: 'bar' + (warning ? ' warning' : '') } },
131
        [
132
          V.h('span',
133
            {
134
              style: { width: (width * 100) + '%' }
135
            }),
136
          V.h('label', v)
137
        ]
138
      );
139
    }
140
141
    function showLoad(d) {
142
      if (!('loadavg' in d)) {
143
        return undefined;
144
      }
145
146
      var value = d.loadavg.toFixed(2);
147
      var width = d.loadavg % 1;
148
      var warning = false;
149
      if (d.loadavg >= d.nproc) {
150
        warning = true;
151
      }
152
      return showBar(value, width, warning);
153
    }
154
155
    function showRAM(d) {
156
      if (!('memory_usage' in d)) {
157
        return undefined;
158
      }
159
160
      var value = Math.round(d.memory_usage * 100) + ' %';
161
      var width = d.memory_usage;
162
      var warning = false;
163
      if (d.memory_usage >= 0.8) {
164
        warning = true;
165
      }
166
      return showBar(value, width, warning);
167
    }
168
169
    function showAutoupdate(d) {
170
      var au = helper.dictGet(d, ['autoupdater']);
171
      if (!au) {
172
        return undefined;
173
      }
174
175
      return au.enabled ? _.t('node.activated', { branch: au.branch }) : _.t('node.deactivated');
176
    }
177
178
    function showStatImg(o, d) {
179
      var subst = {};
180
      subst['{NODE_ID}'] = d.node_id;
181
      subst['{NODE_NAME}'] = d.hostname.replace(/[^a-z0-9\-]/ig, '_');
182
      subst['{TIME}'] = d.lastseen.format('DDMMYYYYHmmss');
183
      subst['{LOCALE}'] = _.locale();
184
      return helper.showStat(V, o, subst);
185
    }
186
187
    return function (config, el, router, d, linkScale, nodeDict) {
188
      function nodeLink(node) {
189
        return V.h('a', {
190
          props: {
191
            className: node.is_online ? 'online' : 'offline',
192
            href: router.generateLink({ node: node.node_id })
193
          }, on: {
194
            click: function (e) {
195
              router.fullUrl({ node: node.node_id }, e);
196
            }
197
          }
198
        }, node.hostname);
199
      }
200
201
      function nodeidLink(nodeid) {
202
        if (nodeDict[nodeid]) {
203
          return nodeLink(nodeDict[nodeid]);
204
        }
205
        return nodeid;
206
      }
207
208
      function showGateway(node) {
209
        var nexthop = nodeidLink(helper.dictGet(node, ['gateway_nexthop']));
210
        var gateway = nodeidLink(helper.dictGet(node, ['gateway']));
211
        var gateway6 = nodeidLink(helper.dictGet(node, ['gateway6']));
212
213
        var bar = [
214
          V.h('span', [
215
            nexthop,
216
            V.h('br'),
217
            _.t('node.nexthop')
218
          ]),
219
          V.h('span', [
220
            V.h('i', { props: { className: 'ion-arrow-right-c' } })
221
          ]),
222
          V.h('span', [
223
            gateway,
224
            V.h('br'),
225
            'IPv4'
226
          ]),
227
          V.h('span', [
228
            gateway6,
229
            V.h('br'),
230
            'IPv6'
231
          ])
232
        ];
233
234
        return V.h('td', { props: { className: 'gateway' } }, bar);
235
      }
236
237
      function renderNeighbourRow(n) {
238
        var icons = [];
239
        if (helper.hasLocation(n.node)) {
240
          icons.push(V.h('span', { props: { className: 'ion-location' } }));
241
        }
242
243
        var td1 = V.h('td', icons);
244
        var td2 = V.h('td', nodeLink(n.node));
245
        var td3 = V.h('td', (n.node.clients ? n.node.clients.toString() : '0'));
246
        var td4 = V.h('td', { style: { color: linkScale((n.link.source_tq + n.link.target_tq) / 2) } }, helper.showTq(n.link.source_tq) + ' - ' + helper.showTq(n.link.target_tq));
247
        var td5 = V.h('td', helper.showDistance(n.link));
248
249
        return V.h('tr', [td1, td2, td3, td4, td5]);
250
      }
251
252
      var self = this;
253
      var header = document.createElement('h2');
254
      var table = document.createElement('table');
255
      var images = document.createElement('div');
256
      var neighbours = document.createElement('h3');
257
      var headings = [{
258
        name: ''
259
      }, {
260
        name: 'node.nodes',
261
        sort: function (a, b) {
262
          return a.node.hostname.localeCompare(b.node.hostname);
263
        },
264
        reverse: false
265
      }, {
266
        name: 'node.clients',
267
        class: 'ion-people',
268
        sort: function (a, b) {
269
          return ('clients' in a.node ? a.node.clients : -1) -
270
            ('clients' in b.node ? b.node.clients : -1);
271
        },
272
        reverse: true
273
      }, {
274
        name: 'node.tq',
275
        class: 'ion-connection-bars',
276
        sort: function (a, b) {
277
          return a.link.source_tq - b.link.source_tq;
278
        },
279
        reverse: true
280
      }, {
281
        name: 'node.distance',
282
        class: 'ion-arrow-resize',
283
        sort: function (a, b) {
284
          return (a.link.distance === undefined ? -1 : a.link.distance) -
285
            (b.link.distance === undefined ? -1 : b.link.distance);
286
        },
287
        reverse: true
288
      }];
289
      var tableNeighbour = new SortTable(headings, 1, renderNeighbourRow);
290
291
      el.appendChild(header);
292
      el.appendChild(table);
293
      el.appendChild(neighbours);
294
      el.appendChild(tableNeighbour.el);
295
      el.appendChild(images);
296
297
      self.render = function render() {
298
        V.patch(header, V.h('h2', d.hostname));
299
300
        var children = [];
301
302
        children.push(helper.attributeEntry(V, 'node.status', showStatus(d)));
303
        children.push(helper.attributeEntry(V, 'node.gateway', d.is_gateway ? 'ja' : null));
304
        children.push(helper.attributeEntry(V, 'node.coordinates', showGeoURI(d)));
305
306
        if (config.nodeInfobox && config.nodeInfobox.contact) {
307
          children.push(helper.attributeEntry(V, 'node.contact', helper.dictGet(d, ['owner', 'contact'])));
308
        }
309
310
        children.push(helper.attributeEntry(V, 'node.hardware', helper.dictGet(d, ['model'])));
311
        children.push(helper.attributeEntry(V, 'node.primaryMac', helper.dictGet(d, ['network', 'mac'])));
312
        children.push(helper.attributeEntry(V, 'node.firmware', showFirmware(d)));
313
        children.push(helper.attributeEntry(V, 'node.site', showSite(d, config)));
314
        children.push(helper.attributeEntry(V, 'node.uptime', showUptime(d)));
315
        children.push(helper.attributeEntry(V, 'node.firstSeen', showFirstseen(d)));
316
        if (config.nodeInfobox && config.nodeInfobox.hardwareUsage) {
317
          children.push(helper.attributeEntry(V, 'node.systemLoad', showLoad(d)));
318
          children.push(helper.attributeEntry(V, 'node.ram', showRAM(d)));
319
        }
320
        children.push(helper.attributeEntry(V, 'node.ipAddresses', showIPs(d)));
321
        children.push(helper.attributeEntry(V, 'node.update', showAutoupdate(d)));
322
        children.push(helper.attributeEntry(V, 'node.clients', showClients(d)));
323
        children.push(helper.attributeEntry(V, 'node.gateway', showGateway(d)));
324
325
        var elNew = V.h('table', children);
326
        table = V.patch(table, elNew);
327
        table.elm.classList.add('attributes');
328
329
        V.patch(neighbours, V.h('h3', _.t('node.link', d.neighbours.length) + ' (' + d.neighbours.length + ')'));
330
        if (d.neighbours.length > 0) {
331
          tableNeighbour.setData(d.neighbours);
332
          tableNeighbour.el.elm.classList.add('node-links');
333
        }
334
335
        if (config.nodeInfos) {
336
          var img = [];
337
          config.nodeInfos.forEach(function (nodeInfo) {
338
            img.push(V.h('h4', nodeInfo.name));
339
            img.push(showStatImg(nodeInfo, d));
340
          });
341
          images = V.patch(images, V.h('div', img));
342
        }
343
      };
344
345
      self.setData = function setData(data) {
346
        d = data.nodes.all.find(function (a) {
347
          return a.node_id === d.node_id;
348
        });
349
        self.render();
350
      };
351
      return self;
352
    };
353
  });
354