Passed
Push — develop ( 9a1b4b...96b452 )
by Xaver
03:40
created

lib/nodelist.js   A

Complexity

Total Complexity 17
Complexity/F 1.42

Size

Lines of Code 101
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 72
mnd 5
bc 5
fnc 12
dl 0
loc 101
rs 10
bpm 0.4166
cpm 1.4166
noi 0
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A nodelist.js ➔ showUptime 0 13 4
C nodelist.js ➔ renderRow 0 25 9
A nodelist.js ➔ render 0 7 1
A nodelist.js ➔ setData 0 13 3
1
define(['sorttable', 'snabbdom', 'helper'], function (SortTable, V, helper) {
2
  'use strict';
3
  V = V.default;
4
5
  function showUptime(uptime) {
6
    // 1000ms are 1 second and 60 second are 1min: 60 * 1000 =  60000
7
    var s = uptime / 60000;
8
    if (Math.abs(s) < 60) {
9
      return Math.round(s) + ' m';
10
    }
11
    s /= 60;
12
    if (Math.abs(s) < 24) {
13
      return Math.round(s) + ' h';
14
    }
15
    s /= 24;
16
    return Math.round(s) + ' d';
17
  }
18
19
  var headings = [{
20
    name: ''
21
  }, {
22
    name: 'node.nodes',
23
    sort: function (a, b) {
24
      return a.hostname.localeCompare(b.hostname);
25
    },
26
    reverse: false
27
  }, {
28
    name: 'node.uptime',
29
    class: 'ion-time',
30
    sort: function (a, b) {
31
      return a.uptime - b.uptime;
32
    },
33
    reverse: true
34
  }, {
35
    name: 'node.links',
36
    class: 'ion-share-alt',
37
    sort: function (a, b) {
38
      return a.neighbours.length - b.neighbours.length;
39
    },
40
    reverse: true
41
  }, {
42
    name: 'node.clients',
43
    class: 'ion-people',
44
    sort: function (a, b) {
45
      return a.clients - b.clients;
46
    },
47
    reverse: true
48
  }];
49
50
  return function () {
51
    function renderRow(d) {
52
      var td0Content = '';
53
      if (helper.hasLocation(d)) {
54
        td0Content = V.h('span', { props: { className: 'icon ion-location', title: _.t('location.location') } });
55
      }
56
57
      var td1Content = V.h('a', {
58
        props: {
59
          className: ['hostname', d.is_online ? 'online' : 'offline'].join(' '),
60
          href: router.generateLink({ node: d.node_id })
61
        }, on: {
62
          click: function (e) {
63
            router.fullUrl({ node: d.node_id }, e);
64
          }
65
        }
66
      }, d.hostname);
67
68
      return V.h('tr', [
69
        V.h('td', td0Content),
70
        V.h('td', td1Content),
71
        V.h('td', showUptime(d.uptime)),
72
        V.h('td', d.neighbours.length),
73
        V.h('td', d.clients)
74
      ]);
75
    }
76
77
    var table = new SortTable(headings, 1, renderRow);
78
79
    this.render = function render(d) {
80
      var h2 = document.createElement('h2');
81
      h2.textContent = _.t('node.all');
82
      d.appendChild(h2);
83
      table.el.elm.classList.add('node-list');
84
      d.appendChild(table.el.elm);
85
    };
86
87
    this.setData = function setData(d) {
88
      var data = d.nodes.all.map(function (e) {
89
        var n = Object.create(e);
90
        if (e.is_online) {
91
          n.uptime = d.now - new Date(e.uptime).getTime();
92
        } else {
93
          n.uptime = e.lastseen - d.now;
94
        }
95
        return n;
96
      });
97
98
      table.setData(data);
99
    };
100
  };
101
});
102