Completed
Pull Request — future (#174)
by
unknown
30s
created

node.js ➔ ... ➔ V.h(ꞌaꞌ).on.click   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 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.duration(d.uptime, 'seconds').humanize();
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
          d.clients_wifi24,
79
          V.h('br'),
80
          '2,4 Ghz'
81
        ]),
82
        V.h('span', [
83
          d.clients_wifi5,
84
          V.h('br'),
85
          '5 Ghz'
86
        ]),
87
        V.h('span', [
88
          d.clients_other,
89
          V.h('br'),
90
          _.t('other')
91
        ])
92
      ];
93
94
      return V.h('td', { props: { className: 'clients' } }, clients);
95
    }
96
97
    function showIPs(d) {
98
      var ips = helper.dictGet(d, ['network', 'addresses']);
99
      if (ips === null) {
100
        return undefined;
101
      }
102
103
      ips.sort();
104
105
      var string = [];
106
      ips.forEach(function (ip, i) {
107
        var link = !ip.startsWith('fe80:');
108
109
        if (i > 0) {
110
          string.push(V.h('br'));
111
        }
112
113
        if (link) {
114
          string.push(V.h('a', { props: { href: 'http://[' + ip + ']/', target: '_blank' } }, ip));
115
        } else {
116
          string.push(ip);
117
        }
118
      });
119
      return V.h('td', string);
120
    }
121
122
    function showBar(v, width, warning) {
123
      return V.h('span',
124
        { props: { className: 'bar' + (warning ? ' warning' : '') } },
125
        [
126
          V.h('span',
127
            {
128
              style: { width: (width * 100) + '%' }
129
            }),
130
          V.h('label', v)
131
        ]
132
      );
133
    }
134
135
    function showLoad(d) {
136
      if (!('loadavg' in d)) {
137
        return undefined;
138
      }
139
140
      var value = d.loadavg.toFixed(2);
141
      var width = d.loadavg % 1;
142
      var warning = false;
143
      if (d.loadavg >= d.nproc) {
144
        warning = true;
145
      }
146
      return showBar(value, width, warning);
147
    }
148
149
    function showRAM(d) {
150
      if (!('memory_usage' in d)) {
151
        return undefined;
152
      }
153
154
      var value = Math.round(d.memory_usage * 100) + ' %';
155
      var width = d.memory_usage;
156
      var warning = false;
157
      if (d.memory_usage >= 0.8) {
158
        warning = true;
159
      }
160
      return showBar(value, width, warning);
161
    }
162
163
    function showAutoupdate(d) {
164
      var au = helper.dictGet(d, ['autoupdater']);
165
      if (!au) {
166
        return undefined;
167
      }
168
169
      return au.enabled ? _.t('node.activated', { branch: au.branch }) : _.t('node.deactivated');
170
    }
171
172
    function showStatImg(o, d) {
173
      var subst = {};
174
      subst['{NODE_ID}'] = d.node_id;
175
      subst['{NODE_NAME}'] = d.hostname.replace(/[^a-z0-9\-]/ig, '_');
176
      subst['{TIME}'] = d.lastseen.format('DDMMYYYYHmmss');
177
      subst['{LOCALE}'] = _.locale();
178
      return helper.showStat(V, o, subst);
179
    }
180
181
    return function (config, el, router, d, linkScale, nodeDict) {
182
      function showGateway(gateway) {
183
        if (nodeDict[gateway]) {
184
          gateway = nodeDict[gateway];
185
          return V.h('a', {
186
            props: {
187
              className: gateway.is_online ? 'online' : 'offline',
188
              href: router.generateLink({ node: gateway.node_id })
189
            }, on: {
190
              click: function (e) {
191
                router.fullUrl({ node: gateway.node_id }, e);
192
              }
193
            }
194
          }, gateway.hostname);
195
        }
196
        return gateway;
197
      }
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.node_id })
210
          }, on: {
211
            click: function (e) {
212
              router.fullUrl({ node: n.node.node_id }, e);
213
            }
214
          }
215
        }, n.node.hostname);
216
217
        var td1 = V.h('td', icons);
218
        var td2 = V.h('td', name);
219
        var td3 = V.h('td', (n.node.clients ? n.node.clients.toString() : '0'));
220
        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));
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 self = this;
227
      var header = document.createElement('h2');
228
      var table = document.createElement('table');
229
      var images = document.createElement('div');
230
      var neighbours = document.createElement('h3');
231
      var headings = [{
232
        name: ''
233
      }, {
234
        name: 'node.nodes',
235
        sort: function (a, b) {
236
          return a.node.hostname.localeCompare(b.node.hostname);
237
        },
238
        reverse: false
239
      }, {
240
        name: 'node.clients',
241
        class: 'ion-people',
242
        sort: function (a, b) {
243
          return ('clients' in a.node ? a.node.clients : -1) -
244
            ('clients' in b.node ? b.node.clients : -1);
245
        },
246
        reverse: true
247
      }, {
248
        name: 'node.tq',
249
        class: 'ion-connection-bars',
250
        sort: function (a, b) {
251
          return a.link.source_tq - b.link.source_tq;
252
        },
253
        reverse: true
254
      }, {
255
        name: 'node.distance',
256
        class: 'ion-arrow-resize',
257
        sort: function (a, b) {
258
          return (a.link.distance === undefined ? -1 : a.link.distance) -
259
            (b.link.distance === undefined ? -1 : b.link.distance);
260
        },
261
        reverse: true
262
      }];
263
      var tableNeighbour = new SortTable(headings, 1, renderNeighbourRow);
264
265
      el.appendChild(header);
266
      el.appendChild(table);
267
      el.appendChild(neighbours);
268
      el.appendChild(tableNeighbour.el);
269
      el.appendChild(images);
270
271
      self.render = function render() {
272
        V.patch(header, V.h('h2', d.hostname));
273
274
        var children = [];
275
276
        children.push(helper.attributeEntry(V, 'node.status', showStatus(d)));
277
        children.push(helper.attributeEntry(V, 'node.gateway', d.is_gateway ? 'ja' : null));
278
        children.push(helper.attributeEntry(V, 'node.coordinates', showGeoURI(d)));
279
280
        if (config.nodeInfobox && config.nodeInfobox.contact) {
281
          children.push(helper.attributeEntry(V, 'node.contact', helper.dictGet(d, ['owner', 'contact'])));
282
        }
283
284
        children.push(helper.attributeEntry(V, 'node.hardware', helper.dictGet(d, ['model'])));
285
        children.push(helper.attributeEntry(V, 'node.primaryMac', helper.dictGet(d, ['network', 'mac'])));
286
        children.push(helper.attributeEntry(V, 'node.firmware', showFirmware(d)));
287
        children.push(helper.attributeEntry(V, 'node.site', showSite(d, config)));
288
        children.push(helper.attributeEntry(V, 'node.uptime', showUptime(d)));
289
        children.push(helper.attributeEntry(V, 'node.firstSeen', showFirstseen(d)));
290
        if (config.nodeInfobox && config.nodeInfobox.hardwareUsage) {
291
          children.push(helper.attributeEntry(V, 'node.systemLoad', showLoad(d)));
292
          children.push(helper.attributeEntry(V, 'node.ram', showRAM(d)));
293
        }
294
        children.push(helper.attributeEntry(V, 'node.ipAddresses', showIPs(d)));
295
        children.push(helper.attributeEntry(V, 'node.selectedNexthop', showGateway(helper.dictGet(d, ['gateway_nexthop']))));
296
        children.push(helper.attributeEntry(V, 'node.selectedGatewayIPv4', showGateway(helper.dictGet(d, ['gateway']))));
297
        children.push(helper.attributeEntry(V, 'node.selectedGatewayIPv6', showGateway(helper.dictGet(d, ['gateway6']))));
298
        children.push(helper.attributeEntry(V, 'node.update', showAutoupdate(d)));
299
        children.push(helper.attributeEntry(V, 'node.clients', showClients(d)));
300
301
        var elNew = V.h('table', children);
302
        table = V.patch(table, elNew);
303
        table.elm.classList.add('attributes');
304
305
        V.patch(neighbours, V.h('h3', _.t('node.link', d.neighbours.length) + ' (' + d.neighbours.length + ')'));
306
        if (d.neighbours.length > 0) {
307
          tableNeighbour.setData(d.neighbours);
308
          tableNeighbour.el.elm.classList.add('node-links');
309
        }
310
311
        if (config.nodeInfos) {
312
          var img = [];
313
          config.nodeInfos.forEach(function (nodeInfo) {
314
            img.push(V.h('h4', nodeInfo.name));
315
            img.push(showStatImg(nodeInfo, d));
316
          });
317
          images = V.patch(images, V.h('div', img));
318
        }
319
      };
320
321
      self.setData = function setData(data) {
322
        d = data.nodes.all.find(function (a) {
323
          return a.node_id === d.node_id;
324
        });
325
        self.render();
326
      };
327
      return self;
328
    };
329
  });
330