2017-01-29 23:51:08 +00:00
|
|
|
define(['sorttable', 'virtual-dom', 'helper'], function (SortTable, V, helper) {
|
|
|
|
'use strict';
|
2016-05-27 21:59:01 +00:00
|
|
|
|
2015-04-07 15:41:17 +00:00
|
|
|
function getUptime(now, d) {
|
2017-01-29 23:51:08 +00:00
|
|
|
if (d.flags.online && 'uptime' in d.statistics) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return Math.round(d.statistics.uptime);
|
2017-01-29 23:51:08 +00:00
|
|
|
} else if (!d.flags.online && 'lastseen' in d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return Math.round(-(now.unix() - d.lastseen.unix()));
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2017-01-29 23:51:08 +00:00
|
|
|
return 0;
|
2015-04-07 15:41:17 +00:00
|
|
|
}
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2015-04-07 15:41:17 +00:00
|
|
|
function showUptime(uptime) {
|
2017-01-29 23:51:08 +00:00
|
|
|
var s = '';
|
2016-05-22 11:23:43 +00:00
|
|
|
uptime /= 3600;
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (uptime !== undefined) {
|
|
|
|
if (Math.abs(uptime) >= 24) {
|
2017-01-29 23:51:08 +00:00
|
|
|
s = Math.round(uptime / 24) + 'd';
|
2016-05-22 12:51:30 +00:00
|
|
|
} else {
|
2017-01-29 23:51:08 +00:00
|
|
|
s = Math.round(uptime) + 'h';
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
return s;
|
2015-04-07 15:41:17 +00:00
|
|
|
}
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
var headings = [{
|
2016-05-26 23:34:42 +00:00
|
|
|
name: ''
|
|
|
|
}, {
|
2017-01-28 14:33:13 +00:00
|
|
|
name: 'node.nodes',
|
2016-05-22 12:51:30 +00:00
|
|
|
sort: function (a, b) {
|
|
|
|
return a.nodeinfo.hostname.localeCompare(b.nodeinfo.hostname);
|
|
|
|
},
|
|
|
|
reverse: false
|
2017-01-28 14:33:13 +00:00
|
|
|
}, {
|
|
|
|
name: 'node.uptime',
|
2017-01-29 23:51:08 +00:00
|
|
|
sort: function (a, b) {
|
|
|
|
return a.uptime - b.uptime;
|
2016-05-22 12:51:30 +00:00
|
|
|
},
|
2017-01-29 23:51:08 +00:00
|
|
|
reverse: true
|
2017-01-28 14:33:13 +00:00
|
|
|
}, {
|
|
|
|
name: 'node.links',
|
2017-01-29 23:51:08 +00:00
|
|
|
sort: function (a, b) {
|
|
|
|
return a.meshlinks - b.meshlinks;
|
|
|
|
},
|
|
|
|
reverse: true
|
2017-01-28 14:33:13 +00:00
|
|
|
}, {
|
|
|
|
name: 'node.clients',
|
2017-01-29 23:51:08 +00:00
|
|
|
sort: function (a, b) {
|
|
|
|
return ('clients' in a.statistics ? a.statistics.clients : -1) -
|
2017-01-28 14:33:13 +00:00
|
|
|
('clients' in b.statistics ? b.statistics.clients : -1);
|
2016-05-22 12:51:30 +00:00
|
|
|
},
|
2017-01-29 23:51:08 +00:00
|
|
|
reverse: true
|
|
|
|
}];
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
return function (router) {
|
2015-04-07 15:41:17 +00:00
|
|
|
function renderRow(d) {
|
2016-05-26 23:34:42 +00:00
|
|
|
var td0Content = [];
|
2016-05-22 11:23:43 +00:00
|
|
|
var td1Content = [];
|
2017-01-29 23:51:08 +00:00
|
|
|
var aClass = ['hostname', d.flags.online ? 'online' : 'offline'];
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
td1Content.push(V.h('a', {
|
|
|
|
className: aClass.join(' '),
|
2016-05-22 12:51:30 +00:00
|
|
|
onclick: router.node(d),
|
2017-01-29 23:51:08 +00:00
|
|
|
href: '#'
|
2016-05-22 12:51:30 +00:00
|
|
|
}, d.nodeinfo.hostname));
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2016-05-26 16:37:24 +00:00
|
|
|
if (helper.hasLocation(d)) {
|
2017-01-29 23:51:08 +00:00
|
|
|
td0Content.push(V.h('span', { className: 'icon ion-location' }));
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
var td0 = V.h('td', td0Content);
|
|
|
|
var td1 = V.h('td', td1Content);
|
|
|
|
var td2 = V.h('td', showUptime(d.uptime));
|
|
|
|
var td3 = V.h('td', d.meshlinks.toString());
|
|
|
|
var td4 = V.h('td', Number('clients' in d.statistics ? d.statistics.clients : 0).toFixed(0));
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
return V.h('tr', [td0, td1, td2, td3, td4]);
|
2015-04-07 15:41:17 +00:00
|
|
|
}
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2016-05-26 23:34:42 +00:00
|
|
|
var table = new SortTable(headings, 1, renderRow);
|
|
|
|
table.el.classList.add('node-list');
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
this.render = function render(d) {
|
|
|
|
var h2 = document.createElement('h2');
|
2017-01-28 14:33:13 +00:00
|
|
|
h2.textContent = _.t('node.all');
|
2016-05-26 23:34:42 +00:00
|
|
|
d.appendChild(h2);
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2016-05-26 23:34:42 +00:00
|
|
|
d.appendChild(table.el);
|
2016-05-22 11:23:43 +00:00
|
|
|
};
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
this.setData = function setData(d) {
|
2015-04-07 15:41:17 +00:00
|
|
|
var data = d.nodes.all.map(function (e) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var n = Object.create(e);
|
2017-01-29 23:51:08 +00:00
|
|
|
n.uptime = getUptime(d.now, e);
|
2016-05-22 11:23:43 +00:00
|
|
|
n.meshlinks = e.meshlinks || 0;
|
|
|
|
return n;
|
|
|
|
});
|
2015-03-26 00:31:46 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
table.setData(data);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|