meshviewer/lib/nodelist.js

113 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

define(['sorttable', 'snabbdom', 'helper'], function (SortTable, V, helper) {
'use strict';
V = V.default;
2016-05-27 21:59:01 +00:00
2015-04-07 15:41:17 +00:00
function getUptime(now, d) {
if (d.flags.online && 'uptime' in d.statistics) {
return Math.round(d.statistics.uptime);
} else if (!d.flags.online && 'lastseen' in d) {
return Math.round(-(now.unix() - d.lastseen.unix()));
}
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) {
var s = '';
uptime /= 3600;
2015-03-26 00:31:46 +00:00
if (uptime !== undefined) {
if (Math.abs(uptime) >= 24) {
s = Math.round(uptime / 24) + 'd';
} else {
s = Math.round(uptime) + 'h';
}
}
2015-03-26 00:31:46 +00:00
return s;
2015-04-07 15:41:17 +00:00
}
2015-03-26 00:31:46 +00:00
var headings = [{
name: ''
}, {
2017-01-28 14:33:13 +00:00
name: 'node.nodes',
sort: function (a, b) {
return a.nodeinfo.hostname.localeCompare(b.nodeinfo.hostname);
},
reverse: false
2017-02-04 15:14:24 +00:00
}, {
2017-01-28 14:33:13 +00:00
name: 'node.uptime',
class: 'ion-time',
sort: function (a, b) {
return a.uptime - b.uptime;
},
reverse: true
2017-02-04 15:14:24 +00:00
}, {
2017-01-28 14:33:13 +00:00
name: 'node.links',
class: 'ion-share-alt',
sort: function (a, b) {
2017-05-09 13:22:32 +00:00
return a.neighbours.length - b.neighbours.length;
},
reverse: true
2017-02-04 15:14:24 +00:00
}, {
2017-01-28 14:33:13 +00:00
name: 'node.clients',
class: 'ion-people',
sort: function (a, b) {
return ('clients' in a.statistics ? a.statistics.clients : -1) -
2017-02-04 15:14:24 +00:00
('clients' in b.statistics ? b.statistics.clients : -1);
},
reverse: true
}];
2015-03-26 00:31:46 +00:00
return function (router) {
2015-04-07 15:41:17 +00:00
function renderRow(d) {
var td0Content = [];
var td1Content = [];
var aClass = ['hostname', d.flags.online ? 'online' : 'offline'];
2015-03-26 00:31:46 +00:00
td1Content.push(V.h('a', {
props: {
className: aClass.join(' '),
href: router.generateLink({ node: d.nodeinfo.node_id })
}, on: {
click: function (e) {
router.fullUrl({ node: d.nodeinfo.node_id }, e);
}
2017-03-05 11:29:21 +00:00
}
}, d.nodeinfo.hostname));
2016-05-26 16:37:24 +00:00
if (helper.hasLocation(d)) {
td0Content.push(V.h('span', { props: { className: 'icon ion-location' } }));
}
2015-03-26 00:31:46 +00:00
var td0 = V.h('td', td0Content);
var td1 = V.h('td', td1Content);
var td2 = V.h('td', showUptime(d.uptime));
2017-05-09 13:22:32 +00:00
var td3 = V.h('td', d.neighbours.length);
var td4 = V.h('td', Number('clients' in d.statistics ? d.statistics.clients : 0).toFixed(0));
2015-03-26 00:31:46 +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
var table = new SortTable(headings, 1, renderRow);
2015-03-26 00:31:46 +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');
d.appendChild(h2);
table.el.elm.classList.add('node-list');
d.appendChild(table.el.elm);
};
2015-03-26 00:31:46 +00:00
this.setData = function setData(d) {
2015-04-07 15:41:17 +00:00
var data = d.nodes.all.map(function (e) {
var n = Object.create(e);
n.uptime = getUptime(d.now, e);
2017-05-09 13:22:32 +00:00
n.neighbours = e.neighbours;
return n;
});
2015-03-26 00:31:46 +00:00
table.setData(data);
};
};
});