Completed
Pull Request — develop (#126)
by Xaver
01:08
created

eys[?!?]].interfaces.tunnel.forEach   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(['d3-interpolate', 'virtual-dom', 'filters/genericnode', 'helper'],
2
  function (d3Interpolate, V, Filter, helper) {
3
    'use strict';
4
5
    return function (config, filterManager) {
6
      var self = this;
7
      var scale = d3Interpolate.interpolate('#770038', '#dc0067');
8
      var gateways = {};
9
10
      var statusTable = document.createElement('table');
11
      statusTable.classList.add('proportion');
12
      var fwTable = statusTable.cloneNode(false);
13
      var hwTable = statusTable.cloneNode(false);
14
      var geoTable = statusTable.cloneNode(false);
15
      var autoTable = statusTable.cloneNode(false);
16
      var gatewayTable = statusTable.cloneNode(false);
17
      var siteTable = statusTable.cloneNode(false);
18
19
      function showStatGlobal(o) {
20
        return helper.showStat(o);
21
      }
22
23
      function count(nodes, key, f) {
24
        var dict = {};
25
26
        nodes.forEach(function (d) {
27
          var v = helper.dictGet(d, key.slice(0));
28
29
          if (f !== undefined) {
30
            v = f(v);
31
          }
32
33
          if (v === null) {
34
            return;
35
          }
36
37
          dict[v] = 1 + (v in dict ? dict[v] : 0);
38
        });
39
40
        return Object.keys(dict).map(function (d) {
41
          return [d, dict[d], key, f];
42
        });
43
      }
44
45
      function addFilter(filter) {
46
        return function () {
47
          filterManager.addFilter(filter);
48
          return false;
49
        };
50
      }
51
52
      function fillTable(name, table, data) {
53
        if (!table.last) {
54
          table.last = V.h('table');
55
        }
56
57
        var max = Math.max.apply(Math, data.map(function (o) {
58
          return o[1];
59
        }));
60
61
        var items = data.map(function (d) {
62
          var v = d[1] / max;
63
64
          var filter = new Filter(_.t(name), d[2], d[0], d[3]);
65
66
          var a = V.h('a', { href: '#', onclick: addFilter(filter) }, d[0]);
67
68
          var th = V.h('th', a);
69
          var td = V.h('td', V.h('span', {
70
            style: {
71
              width: Math.round(v * 100) + '%',
72
              backgroundColor: scale(v),
73
              color: 'white'
74
            }
75
          }, d[1].toFixed(0)));
76
77
          return V.h('tr', [th, td]);
78
        });
79
80
        var tableNew = V.h('table', items);
81
        table = V.patch(table, V.diff(table.last, tableNew));
82
        table.last = tableNew;
83
      }
84
85
      self.setData = function setData(data) {
86
        var onlineNodes = data.nodes.all.filter(helper.online);
87
        var nodes = onlineNodes.concat(data.nodes.lost);
88
        var nodeDict = {};
89
90
        data.nodes.all.forEach(function (d) {
91
          nodeDict[d.nodeinfo.node_id] = d;
92
        });
93
94
        var statusDict = count(nodes, ['flags', 'online'], function (d) {
95
          return d ? 'online' : 'offline';
96
        });
97
        var fwDict = count(nodes, ['nodeinfo', 'software', 'firmware', 'release']);
98
        var hwDict = count(nodes, ['nodeinfo', 'hardware', 'model']);
99
        var geoDict = count(nodes, ['nodeinfo', 'location'], function (d) {
100
          return d && d.longitude && d.latitude ? _.t('yes') : _.t('no');
101
        });
102
103
        var autoDict = count(nodes, ['nodeinfo', 'software', 'autoupdater'], function (d) {
104
          if (d === null) {
105
            return null;
106
          } else if (d.enabled) {
107
            return d.branch;
108
          }
109
          return _.t('node.deactivated');
110
        });
111
112
        nodes.forEach(function (node) {
113
          if (node.flags.gateway) {
114
            var mesh = node.nodeinfo.network.mesh;
115
            mesh[Object.keys(mesh)[0]].interfaces.tunnel.forEach(function (mac) {
116
              gateways[mac] = node.nodeinfo.hostname;
117
            });
118
          }
119
        });
120
121
        var gatewayDict = count(nodes, ['statistics', 'gateway'], function (d) {
122
          for (var mac in gateways) {
123
            if (gateways.hasOwnProperty(mac) && mac === d) {
124
              d = gateways[mac];
125
              return d;
126
            }
127
          }
128
          return null;
129
        });
130
131
        var siteDict = count(nodes, ['nodeinfo', 'system', 'site_code'], function (d) {
132
          if (config.siteNames) {
133
            config.siteNames.forEach(function (t) {
134
              if (d === t.site) {
135
                d = t.name;
136
              }
137
            });
138
          }
139
          return d;
140
        });
141
142
        fillTable('node.status', statusTable, statusDict.sort(function (a, b) {
143
          return b[1] - a[1];
144
        }));
145
        fillTable('node.firmware', fwTable, fwDict.sort(function (a, b) {
146
          if (b[0] < a[0]) {
147
            return -1;
148
          }
149
          if (b[0] > a[0]) {
150
            return 1;
151
          }
152
          return 0;
153
        }));
154
        fillTable('node.hardware', hwTable, hwDict.sort(function (a, b) {
155
          return b[1] - a[1];
156
        }));
157
        fillTable('node.visible', geoTable, geoDict.sort(function (a, b) {
158
          return b[1] - a[1];
159
        }));
160
        fillTable('node.update', autoTable, autoDict.sort(function (a, b) {
161
          return b[1] - a[1];
162
        }));
163
        fillTable('node.gateway', gatewayTable, gatewayDict.sort(function (a, b) {
164
          return b[1] - a[1];
165
        }));
166
        fillTable('node.site', siteTable, siteDict.sort(function (a, b) {
167
          return b[1] - a[1];
168
        }));
169
      };
170
171
      self.render = function render(el) {
172
        var h2;
173
        self.renderSingle(el, 'node.status', statusTable);
174
        self.renderSingle(el, 'node.firmware', fwTable);
175
        self.renderSingle(el, 'node.hardware', hwTable);
176
        self.renderSingle(el, 'node.visible', geoTable);
177
        self.renderSingle(el, 'node.update', autoTable);
178
        self.renderSingle(el, 'node.gateway', gatewayTable);
179
        self.renderSingle(el, 'node.site', siteTable);
180
181
        if (config.globalInfos) {
182
          config.globalInfos.forEach(function (globalInfo) {
183
            h2 = document.createElement('h2');
184
            h2.textContent = globalInfo.name;
185
            el.appendChild(h2);
186
            el.appendChild(showStatGlobal(globalInfo));
187
          });
188
        }
189
      };
190
191
      self.renderSingle = function renderSingle(el, heading, table) {
192
        var h2 = document.createElement('h2');
193
        h2.classList.add('proportion-header');
194
        h2.textContent = _.t(heading);
195
        h2.onclick = function onclick() {
196
          table.classList.toggle('hide');
197
        };
198
        el.appendChild(h2);
199
        el.appendChild(table);
200
      };
201
      return self;
202
    };
203
  });
204