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