You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.7 KiB
67 lines
1.7 KiB
8 years ago
|
const prettyBytes = require('pretty-bytes');
|
||
8 years ago
|
const moment = require('moment');
|
||
8 years ago
|
const querystring = require('querystring');
|
||
8 years ago
|
|
||
8 years ago
|
function humanTimeAgo(utcDate) {
|
||
|
const diff = moment.utc().diff(moment.utc(utcDate));
|
||
|
const uptime = {};
|
||
|
|
||
|
uptime.s = Math.round(diff / 1000);
|
||
|
uptime.m = Math.floor(uptime.s / 60);
|
||
|
uptime.h = Math.floor(uptime.m / 60);
|
||
|
uptime.d = Math.floor(uptime.h / 24);
|
||
|
|
||
|
uptime.s %= 60;
|
||
|
uptime.m %= 60;
|
||
|
uptime.h %= 24;
|
||
|
|
||
|
let readableUptime = '';
|
||
|
readableUptime += uptime.d ? ` ${uptime.d}d` : '';
|
||
|
readableUptime += uptime.h ? ` ${uptime.h}h` : '';
|
||
|
readableUptime += !uptime.d || !uptime.h && uptime.m ? ` ${uptime.m}m` : '';
|
||
|
|
||
|
return readableUptime.trim();
|
||
|
}
|
||
|
|
||
8 years ago
|
const filters = {
|
||
8 years ago
|
bandwidth: node => `${prettyBytes(node.advertised_bandwidth)}/s`,
|
||
|
uptime: node => {
|
||
|
|
||
|
// Check node is up
|
||
|
if(!node.running) {
|
||
|
return 'Down';
|
||
|
}
|
||
|
|
||
|
// Check uptime
|
||
8 years ago
|
return humanTimeAgo(node.last_restarted);
|
||
8 years ago
|
},
|
||
|
pagination: (req, direction) => {
|
||
|
|
||
|
// Clone query string
|
||
|
const query = Object.assign({}, req.query);
|
||
|
|
||
|
// Set page as 1 by default
|
||
|
query.p = query.p ? query.p : 1;
|
||
|
|
||
|
// Update page
|
||
|
if(direction == 'next') {
|
||
|
query.p++;
|
||
|
} else if(direction == 'prev') {
|
||
|
query.p--;
|
||
|
}
|
||
|
|
||
|
// Encode query string
|
||
|
return `/?${querystring.encode(query)}`;
|
||
8 years ago
|
},
|
||
8 years ago
|
name: node => node.nickname
|
||
8 years ago
|
|| node.fingerprint && node.fingerprint.slice(0, 8)
|
||
8 years ago
|
|| node.hashed_fingerprint && node.hashed_fingerprint.slice(0, 8),
|
||
|
status: node => node.running ?
|
||
|
`Up for ${humanTimeAgo(node.last_restarted)}` :
|
||
|
`Down for ${humanTimeAgo(node.last_seen)}`
|
||
8 years ago
|
};
|
||
|
|
||
|
module.exports = app => Object.keys(filters).forEach(filter => {
|
||
|
app.settings.nunjucksEnv.addFilter(filter, filters[filter])
|
||
|
});
|