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

proportions.js ➔ ... ➔ gatewayDict.sort   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 2
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
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) {
54
          table = document.createElement('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', { props: { href: '#' }, on: { click: 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
        var tableNew = V.h('table', { props: { className: 'proportion' } }, items);
80
        return V.patch(table, tableNew);
81
      }
82
83
      self.setData = function setData(data) {
84
        var onlineNodes = data.nodes.all.filter(helper.online);
85
        var nodes = onlineNodes.concat(data.nodes.lost);
86
        var nodeDict = {};
87
88
        data.nodes.all.forEach(function (d) {
89
          nodeDict[d.nodeinfo.node_id] = d;
90
        });
91
92
        var statusDict = count(nodes, ['flags', 'online'], function (d) {
93
          return d ? 'online' : 'offline';
94
        });
95
        var fwDict = count(nodes, ['nodeinfo', 'software', 'firmware', 'release']);
96
        var hwDict = count(nodes, ['nodeinfo', 'hardware', 'model']);
97
        var geoDict = count(nodes, ['nodeinfo', 'location'], function (d) {
98
          return d && d.longitude && d.latitude ? _.t('yes') : _.t('no');
99
        });
100
101
        var autoDict = count(nodes, ['nodeinfo', 'software', 'autoupdater'], function (d) {
102
          if (d === null) {
103
            return null;
104
          } else if (d.enabled) {
105
            return d.branch;
106
          }
107
          return _.t('node.deactivated');
108
        });
109
110
        var gatewayDict = count(nodes, ['statistics', 'gateway'], function (d) {
111
          for (var mac in data.gateways) {
112
            if (data.gateways.hasOwnProperty(mac) && mac === d) {
113
              d = data.gateways[mac];
114
              return d;
115
            }
116
          }
117
          return null;
118
        });
119
120
        var siteDict = count(nodes, ['nodeinfo', 'system', 'site_code'], function (d) {
121
          if (config.siteNames) {
122
            config.siteNames.forEach(function (t) {
123
              if (d === t.site) {
124
                d = t.name;
125
              }
126
            });
127
          }
128
          return d;
129
        });
130
131
        statusTable = fillTable('node.status', statusTable, statusDict.sort(function (a, b) {
132
          return b[1] - a[1];
133
        }));
134
        fwTable = fillTable('node.firmware', fwTable, fwDict.sort(function (a, b) {
135
          if (b[0] < a[0]) {
136
            return -1;
137
          }
138
          if (b[0] > a[0]) {
139
            return 1;
140
          }
141
          return 0;
142
        }));
143
        hwTable = fillTable('node.hardware', hwTable, hwDict.sort(function (a, b) {
144
          return b[1] - a[1];
145
        }));
146
        geoTable = fillTable('node.visible', geoTable, geoDict.sort(function (a, b) {
147
          return b[1] - a[1];
148
        }));
149
        autoTable = fillTable('node.update', autoTable, autoDict.sort(function (a, b) {
150
          return b[1] - a[1];
151
        }));
152
        gatewayTable = fillTable('node.gateway', gatewayTable, gatewayDict.sort(function (a, b) {
153
          return b[1] - a[1];
154
        }));
155
        siteTable = fillTable('node.site', siteTable, siteDict.sort(function (a, b) {
156
          return b[1] - a[1];
157
        }));
158
      };
159
160
      self.render = function render(el) {
161
        var h2;
162
        self.renderSingle(el, 'node.status', statusTable);
163
        self.renderSingle(el, 'node.firmware', fwTable);
164
        self.renderSingle(el, 'node.hardware', hwTable);
165
        self.renderSingle(el, 'node.visible', geoTable);
166
        self.renderSingle(el, 'node.update', autoTable);
167
        self.renderSingle(el, 'node.gateway', gatewayTable);
168
        self.renderSingle(el, 'node.site', siteTable);
169
170
        if (config.globalInfos) {
171
          config.globalInfos.forEach(function (globalInfo) {
172
            h2 = document.createElement('h2');
173
            h2.textContent = globalInfo.name;
174
            el.appendChild(h2);
175
            el.appendChild(showStatGlobal(globalInfo));
176
          });
177
        }
178
      };
179
180
      self.renderSingle = function renderSingle(el, heading, table) {
181
        var h2 = document.createElement('h2');
182
        h2.classList.add('proportion-header');
183
        h2.textContent = _.t(heading);
184
        h2.onclick = function onclick() {
185
          table.elm.classList.toggle('hide');
186
        };
187
        el.appendChild(h2);
188
        el.appendChild(table.elm);
189
      };
190
      return self;
191
    };
192
  });
193