From a2560bfc2653ec77c7a21209de4b99bf0ca2cd0b Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 30 Jan 2018 17:55:40 +0000 Subject: [PATCH] update --- src/cli/commands/stats/bw.js | 47 +++++++++++++++++++++++++++++++++ src/core/components/stats.js | 33 ++++++++++++++++++++++- src/http/api/resources/stats.js | 32 ++++++++++++++++++++++ src/http/api/routes/stats.js | 8 ++++-- 4 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 src/cli/commands/stats/bw.js diff --git a/src/cli/commands/stats/bw.js b/src/cli/commands/stats/bw.js new file mode 100644 index 0000000000..e9a2dbe92c --- /dev/null +++ b/src/cli/commands/stats/bw.js @@ -0,0 +1,47 @@ +'use strict' + +const print = require('../../utils').print + +module.exports = { + command: 'bw', + + describe: 'Get bandwidth information.', + + builder: { + peer: { + type: 'string', + default: '' + }, + proto: { + type: 'string', + default: '' + }, + poll: { + type: 'boolean', + default: false + }, + interval: { + type: 'string', + default: '' + } + }, + + handler (argv) { + argv.ipfs.stats.bw({ + peer: argv.peer, + proto: argv.proto, + poll: argv.poll, + interval: argv.interval + }, (err, stats) => { + if (err) { + throw err + } + + print(`bandwidth status + total in: ${stats.totalIn}B + total out: ${stats.totalOut}B + rate in: ${stats.rateIn}B/s + rate out: ${stats.rateOut}B/s`) + }) + } +} diff --git a/src/core/components/stats.js b/src/core/components/stats.js index 6bc7121301..bf7e7b10f8 100644 --- a/src/core/components/stats.js +++ b/src/core/components/stats.js @@ -1,8 +1,39 @@ 'use strict' +const promisify = require('promisify-es6') + module.exports = function stats (self) { return { bitswap: require('./bitswap')(self).stat, - repo: require('./repo')(self).stat + + repo: require('./repo')(self).stat, + + bw: promisify((options, callback) => { + if (typeof options === 'function') { + callback = options + options = {} + } + + let stats + + if (options.peer) { + // TODO: stats for a specific peer + } else if (options.proto) { + // TODO: stats for a specific proto + } else { + const stat = self._bitswap.stat() + const snapshot = stat.snapshot + const movingAverages = stat.movingAverages + + stats = { + totalIn: snapshot.dataReceived, + totalOut: snapshot.dataSent, + rateIn: movingAverages.dataReceived[60000].movingAverage() / 60, // Bytes per second + rateOut: movingAverages.dataSent[60000].movingAverage() / 60 // Bytes per second + } + } + + callback(null, stats) + }) } } diff --git a/src/http/api/resources/stats.js b/src/http/api/resources/stats.js index 7de4c92c4c..d88bec4762 100644 --- a/src/http/api/resources/stats.js +++ b/src/http/api/resources/stats.js @@ -1,6 +1,38 @@ 'use strict' +const Readable = require('readable-stream').Readable + exports = module.exports exports.bitswap = require('./bitswap').stat + exports.repo = require('./repo').stat + +exports.bw = (request, reply) => { + const ipfs = request.server.app.ipfs + const human = request.query.human === 'true' + + const options = { + peer: request.query.peer, + proto: request.query.proto, + poll: request.query.poll === 'true', + interval: request.query.interval + } + + ipfs.stats.bw(options, (err, stat) => { + if (err) { + return reply({ + Message: err.toString(), + Code: 0 + }).code(500) + } + + reply({ + TotalIn: stat.totalIn, + TotalOut: stat.totalOut, + RateIn: stat.rateIn, + RateOut: stat.rateOut, + }) + }) +} + diff --git a/src/http/api/routes/stats.js b/src/http/api/routes/stats.js index a2a448cc23..f1a2c69035 100644 --- a/src/http/api/routes/stats.js +++ b/src/http/api/routes/stats.js @@ -21,11 +21,15 @@ module.exports = (server) => { } }) - /* api.route({ + api.route({ method: '*', path: '/api/v0/stats/bw', config: { + payload: { + output: 'stream', + parse: false + }, handler: resources.stats.bw } - }) */ + }) }