Completed
Pull Request — future (#184)
by Xaver
30s
created

node.js ➔ ... ➔ nodeIdLink   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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