Browse Source

Add uptime nunjuck filter

pm2
Luke Childs 8 years ago
parent
commit
d71cfd06c8
  1. 31
      lib/nunjuck-filters.js
  2. 1
      package.json
  3. 2
      views/listing.html

31
lib/nunjuck-filters.js

@ -1,7 +1,36 @@
const prettyBytes = require('pretty-bytes'); const prettyBytes = require('pretty-bytes');
const moment = require('moment');
const filters = { const filters = {
bandwidth: node => `${prettyBytes(node.advertised_bandwidth)}/s` bandwidth: node => `${prettyBytes(node.advertised_bandwidth)}/s`,
uptime: node => {
// Check node is up
if(!node.running) {
return 'Down';
}
// Check uptime
const lastRestarted = moment.utc(node.last_restarted);
const diff = moment.utc().diff(lastRestarted);
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.m ? ` ${uptime.m}m` : '';
return readableUptime.trim();
}
}; };
module.exports = app => Object.keys(filters).forEach(filter => { module.exports = app => Object.keys(filters).forEach(filter => {

1
package.json

@ -5,6 +5,7 @@
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {
"express": "^4.14.0", "express": "^4.14.0",
"moment": "^2.14.1",
"nunjucks": "^2.4.2", "nunjucks": "^2.4.2",
"onionoo": "^0.3.1", "onionoo": "^0.3.1",
"pretty-bytes": "^4.0.2" "pretty-bytes": "^4.0.2"

2
views/listing.html

@ -17,7 +17,7 @@
<tr> <tr>
<td><a href="/node/{{ node.fingerprint if node.fingerprint else node.hashed_fingerprint }}">{{ node.nickname }}</a></td> <td><a href="/node/{{ node.fingerprint if node.fingerprint else node.hashed_fingerprint }}">{{ node.nickname }}</a></td>
<td>{{ node | bandwidth }}</td> <td>{{ node | bandwidth }}</td>
<td>{{ node.last_restarted if node.running else 'Down' }}</td> <td>{{ node | uptime }}</td>
<td>{{ node.country_name }}</td> <td>{{ node.country_name }}</td>
<td>{{ node.flags | join(', ') }}</td> <td>{{ node.flags | join(', ') }}</td>
<td>{{ node.type | title }}</td> <td>{{ node.type | title }}</td>

Loading…
Cancel
Save