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.

50 lines
1.0 KiB

const chart = require('ascii-chart');
function pointsFromBandwidthData(values, numPoints) {
8 years ago
// Define vars
const len = values.length;
const points = [];
let i = 0;
let size;
8 years ago
// 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));
}
}
8 years ago
// Return points
return points
8 years ago
// Calculate average value of each point
.map(point => Math.round(point.reduce((a, b) => a + b) / point.length))
8 years ago
// Convert bytes to megabytes
.map(bytes => Number((bytes / 1000000).toPrecision(3)));
}
module.exports = values => {
8 years ago
if (!values || values.length < 1) {
return '';
}
const points = pointsFromBandwidthData(values, 57);
return chart(points, {
width: 120,
height: 20,
padding: 0,
pointChar: '*',
negativePointChar: '.',
axisChar: '.'
});
};