Passed
Push — develop ( be0d3a...8fdc8d )
by Xaver
01:02
created

proportions.js ➔ ... ➔ config.domainNames.some   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
dl 0
loc 6
rs 10
nop 1
1
define(['d3-interpolate', 'snabbdom', 'utils/version', 'filters/genericnode', 'helper'],
2
  function (d3Interpolate, V, versionCompare, Filter, helper) {
3
    'use strict';
4
    V = V.default;
5
6
    return function (filterManager) {
7
      var self = this;
8
      var scale = d3Interpolate.interpolate(config.forceGraph.tqFrom, config.forceGraph.tqTo);
9
      var time;
10
11
      var statusTable;
12
      var fwTable;
13
      var hwTable;
14
      var geoTable;
15
      var autoTable;
16
      var gatewayTable;
17
      var gateway6Table;
18
      var domainTable;
19
20
      function count(nodes, key, f) {
21
        var dict = {};
22
23
        nodes.forEach(function (d) {
24
          var v = helper.dictGet(d, key.slice(0));
25
26
          if (f !== undefined) {
27
            v = f(v);
28
          }
29
30
          if (v === null) {
31
            return;
32
          }
33
34
          dict[v] = 1 + (v in dict ? dict[v] : 0);
35
        });
36
37
        return Object.keys(dict).map(function (d) {
38
          return [d, dict[d], key, f];
39
        });
40
      }
41
42
      function addFilter(filter) {
43
        return function () {
44
          filterManager.addFilter(filter);
45
          return false;
46
        };
47
      }
48
49
      function fillTable(name, table, data) {
50
        if (!table) {
51
          table = document.createElement('table');
52
        }
53
54
        var max = Math.max.apply(Math, data.map(function (o) {
55
          return o[1];
56
        }));
57
58
        var items = data.map(function (d) {
59
          var v = d[1] / max;
60
61
          var filter = new Filter(_.t(name), d[2], d[0], d[3]);
62
63
          var a = V.h('a', { on: { click: addFilter(filter) } }, d[0]);
64
65
          var th = V.h('th', a);
66
          var td = V.h('td', V.h('span', {
67
            style: {
68
              width: 'calc(25px + ' + Math.round(v * 90) + '%)',
69
              backgroundColor: scale(v)
70
            }
71
          }, d[1].toFixed(0)));
72
73
          return V.h('tr', [th, td]);
74
        });
75
        var tableNew = V.h('table', { props: { className: 'proportion' } }, items);
76
        return V.patch(table, tableNew);
77
      }
78
79
      self.setData = function setData(data) {
80
        var onlineNodes = data.nodes.online;
81
        var nodes = onlineNodes.concat(data.nodes.lost);
82
        time = data.timestamp;
83
84
        function hostnameOfNodeID(nodeid) {
85
          var gateway = data.nodeDict[nodeid];
86
          if (gateway) {
87
            return gateway.hostname;
88
          }
89
          return null;
90
        }
91
92
        var gatewayDict = count(nodes, ['gateway'], hostnameOfNodeID);
93
        var gateway6Dict = count(nodes, ['gateway6'], hostnameOfNodeID);
94
95
        var statusDict = count(nodes, ['is_online'], function (d) {
96
          return d ? 'online' : 'offline';
97
        });
98
        var fwDict = count(nodes, ['firmware', 'release']);
99
        var hwDict = count(nodes, ['model']);
100
        var geoDict = count(nodes, ['location'], function (d) {
101
          return d && d.longitude && d.latitude ? _.t('yes') : _.t('no');
102
        });
103
104
        var autoDict = count(nodes, ['autoupdater'], function (d) {
105
          if (d.enabled) {
106
            return d.branch;
107
          }
108
          return _.t('node.deactivated');
109
        });
110
111
        var domainDict = count(nodes, ['domain'], function (d) {
112
          if (config.domainNames) {
113
            config.domainNames.some(function (t) {
114
              if (d === t.domain) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if d === t.domain is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
115
                d = t.name;
116
                return true;
117
              }
118
            });
119
          }
120
          return d;
121
        });
122
123
        statusTable = fillTable('node.status', statusTable, statusDict.sort(function (a, b) {
124
          return b[1] - a[1];
125
        }));
126
        fwTable = fillTable('node.firmware', fwTable, fwDict.sort(versionCompare));
127
        hwTable = fillTable('node.hardware', hwTable, hwDict.sort(function (a, b) {
128
          return b[1] - a[1];
129
        }));
130
        geoTable = fillTable('node.visible', geoTable, geoDict.sort(function (a, b) {
131
          return b[1] - a[1];
132
        }));
133
        autoTable = fillTable('node.update', autoTable, autoDict.sort(function (a, b) {
134
          return b[1] - a[1];
135
        }));
136
        gatewayTable = fillTable('node.selectedGatewayIPv4', gatewayTable, gatewayDict.sort(function (a, b) {
137
          return b[1] - a[1];
138
        }));
139
        gateway6Table = fillTable('node.selectedGatewayIPv6', gateway6Table, gateway6Dict.sort(function (a, b) {
140
          return b[1] - a[1];
141
        }));
142
        domainTable = fillTable('node.domain', domainTable, domainDict.sort(function (a, b) {
143
          return b[1] - a[1];
144
        }));
145
      };
146
147
      self.render = function render(el) {
148
        self.renderSingle(el, 'node.status', statusTable);
149
        self.renderSingle(el, 'node.firmware', fwTable);
150
        self.renderSingle(el, 'node.hardware', hwTable);
151
        self.renderSingle(el, 'node.visible', geoTable);
152
        self.renderSingle(el, 'node.update', autoTable);
153
        self.renderSingle(el, 'node.selectedGatewayIPv4', gatewayTable);
154
        self.renderSingle(el, 'node.selectedGatewayIPv6', gateway6Table);
155
        self.renderSingle(el, 'node.domain', domainTable);
156
157
        if (config.globalInfos) {
158
          var images = document.createElement('div');
159
          el.appendChild(images);
160
          var img = [];
161
          var subst = {
162
            '{TIME}': time,
163
            '{LOCALE}': _.locale()
164
          };
165
          config.globalInfos.forEach(function (globalInfo) {
166
            img.push(V.h('h2', globalInfo.name));
167
            img.push(helper.showStat(V, globalInfo, subst));
168
          });
169
          V.patch(images, V.h('div', img));
170
        }
171
      };
172
173
      self.renderSingle = function renderSingle(el, heading, table) {
174
        if (table.children.length > 0) {
175
          var h2 = document.createElement('h2');
176
          h2.classList.add('proportion-header');
177
          h2.textContent = _.t(heading);
178
          h2.onclick = function onclick() {
179
            table.elm.classList.toggle('hide');
180
          };
181
          el.appendChild(h2);
182
          el.appendChild(table.elm);
183
        }
184
      };
185
      return self;
186
    };
187
  });
188