Browse Source

Get bandwidth data for relay and create ascii chart

pm2
Luke Childs 8 years ago
parent
commit
1b858d2eeb
  1. 11
      controllers/node.js
  2. 52
      lib/bandwidth-chart.js
  3. 13
      lib/tor.js
  4. 1
      package.json

11
controllers/node.js

@ -1,9 +1,14 @@
const tor = require('../lib/tor');
const bandwidthChart = require('../lib/bandwidth-chart');
module.exports = (req, res, next) => {
tor.node(req.params.id)
.then(node => res.render('node.html', {
node: node,
Promise.all([
tor.node(req.params.id),
tor.bandwidth(req.params.id)
])
.then(data => res.render('node.html', {
node: data[0],
bandwidth: bandwidthChart(data[1])
}))
.catch(error => res.render('node.html', {
error: error

52
lib/bandwidth-chart.js

@ -0,0 +1,52 @@
const chart = require('ascii-chart');
function pointsFromBandwidthData(values, numPoints) {
// Define vars
const len = values.length;
const points = [];
let i = 0;
let size;
// Split values into n points
if(numPoints < 2) {
points.push(values);
} else {
if(len % numPoints === 0) {
size = Math.floor(len / numPoints);
while (i < len) {
points.push(values.slice(i, i += size));
}
}
while (i < len) {
size = Math.ceil((len - i) / numPoints--);
points.push(values.slice(i, i += size));
}
}
// Return points
const result = points
// Calculate average value of each point
.map(point => Math.round(point.reduce((a,b) => a + b) / point.length))
// Convert bytes to megabytes
.map(bytes => Number((bytes / 1000000).toPrecision(3)));
return result;
}
module.exports = values => {
if(values && values.length) {
const points = pointsFromBandwidthData(values, 57);
return chart(points, {
width: 125,
height: 20,
padding: 0,
pointChar: '*',
negativePointChar: '.'
});
} else {
return '';
}
}

13
lib/tor.js

@ -21,5 +21,18 @@ module.exports = {
return details.bridges[0];
}
});
},
bandwidth: id => {
return onionoo
.bandwidth({ lookup: id })
.then(bandwidth => {
try {
const lastMonth = bandwidth.relays[0].write_history['1_month'];
return lastMonth.values.map(value => value * lastMonth.factor)
}
catch(e) {
return [];
}
});
}
};

1
package.json

@ -4,6 +4,7 @@
"description": "Explore the Tor network without JS",
"main": "index.js",
"dependencies": {
"ascii-chart": "^1.2.0",
"compression": "^1.6.2",
"express": "^4.14.0",
"express-minify": "^0.2.0",

Loading…
Cancel
Save