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