From 6d4962dd3d996eb22908ddce2569d00878c1a6e1 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 29 Jan 2018 19:04:03 +0000 Subject: [PATCH] feat: implement bitswap.stat and stats.bitswap according to SPEC --- src/core/components/bitswap.js | 20 +++++++++++++----- src/core/components/index.js | 1 + src/core/components/stats.js | 13 ++++++++++++ src/core/index.js | 1 + src/http/api/resources/bitswap.js | 34 +++++++++++++++++++------------ src/http/api/resources/index.js | 1 + src/http/api/resources/stats.js | 5 +++++ src/http/api/routes/index.js | 1 + src/http/api/routes/stats.js | 31 ++++++++++++++++++++++++++++ 9 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 src/core/components/stats.js create mode 100644 src/http/api/resources/stats.js create mode 100644 src/http/api/routes/stats.js diff --git a/src/core/components/bitswap.js b/src/core/components/bitswap.js index 6a1bffefc2..67e753a821 100644 --- a/src/core/components/bitswap.js +++ b/src/core/components/bitswap.js @@ -1,6 +1,7 @@ 'use strict' const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR +const promisify = require('promisify-es6') function formatWantlist (list) { return Array.from(list).map((e) => e[1]) @@ -16,16 +17,25 @@ module.exports = function bitswap (self) { const list = self._bitswap.getWantlist() return formatWantlist(list) }, - stat: () => { + stat: promisify((callback) => { if (!self.isOnline()) { - throw new Error(OFFLINE_ERROR) + callback(new Error(OFFLINE_ERROR)) } - return Object.assign({}, self._bitswap.stat().snapshot, { + const snapshot = self._bitswap.stat().snapshot + + callback(null, { + provideBufLen: snapshot.providesBufferLength, + blocksReceived: snapshot.blocksReceived, wantlist: formatWantlist(self._bitswap.getWantlist()), - peers: self._bitswap.peers().map((id) => id.toB58String()) + peers: self._bitswap.peers().map((id) => id.toB58String()), + dupBlksReceived: snapshot.dupBlksReceived, + dupDataReceived: snapshot.dupDataReceived, + dataReceived: snapshot.dataReceived, + blocksSent: snapshot.blocksSent, + dataSent: snapshot.dataSent }) - }, + }), unwant: (key) => { if (!self.isOnline()) { throw new Error(OFFLINE_ERROR) diff --git a/src/core/components/index.js b/src/core/components/index.js index 248243a88e..d6a557393f 100644 --- a/src/core/components/index.js +++ b/src/core/components/index.js @@ -21,3 +21,4 @@ exports.bitswap = require('./bitswap') exports.pubsub = require('./pubsub') exports.dht = require('./dht') exports.dns = require('./dns') +exports.stats = require('./stats') diff --git a/src/core/components/stats.js b/src/core/components/stats.js new file mode 100644 index 0000000000..3e208ef6f7 --- /dev/null +++ b/src/core/components/stats.js @@ -0,0 +1,13 @@ +'use strict' + +const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR + +function formatWantlist (list) { + return Array.from(list).map((e) => e[1]) +} + +module.exports = function stats (self) { + return { + bitswap: require('./bitswap')(self).stat + } +} diff --git a/src/core/index.js b/src/core/index.js index fa60876061..0e0ee9c7b2 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -95,6 +95,7 @@ class IPFS extends EventEmitter { this.pubsub = components.pubsub(this) this.dht = components.dht(this) this.dns = components.dns(this) + this.stats = components.stats(this) if (this._options.EXPERIMENTAL.pubsub) { this.log('EXPERIMENTAL pubsub is enabled') diff --git a/src/http/api/resources/bitswap.js b/src/http/api/resources/bitswap.js index ed749ea004..da830c6415 100644 --- a/src/http/api/resources/bitswap.js +++ b/src/http/api/resources/bitswap.js @@ -21,19 +21,27 @@ exports.wantlist = (request, reply) => { } exports.stat = (request, reply) => { - let stats - try { - stats = request.server.app.ipfs.bitswap.stat() - } catch (err) { - return reply(boom.badRequest(err)) - } - - reply({ - BlocksReceived: stats.blocksReceived, - Wantlist: stats.wantlist, - Peers: stats.peers, - DupBlksReceived: stats.dupBlksReceived, - DupDataReceived: stats.dupDataReceived + const ipfs = request.server.app.ipfs + + ipfs.bitswap.stat((err, stats) => { + if (err) { + return reply({ + Message: err.toString(), + Code: 0 + }).code(500) + } + + reply({ + ProvideBufLen: stats.provideBufLen, + BlocksReceived: stats.blocksReceived, + Wantlist: stats.wantlist, + Peers: stats.peers, + DupBlksReceived: stats.dupBlksReceived, + DupDataReceived: stats.dupDataReceived, + DataReceived: stats.dataReceived, + BlocksSent: stats.blocksSent, + DataSent: stats.dataSent + }) }) } diff --git a/src/http/api/resources/index.js b/src/http/api/resources/index.js index 16b0f17128..2ea03c522b 100644 --- a/src/http/api/resources/index.js +++ b/src/http/api/resources/index.js @@ -13,3 +13,4 @@ exports.file = require('./file') exports.files = require('./files') exports.pubsub = require('./pubsub') exports.dns = require('./dns') +exports.stats = require('./stats') diff --git a/src/http/api/resources/stats.js b/src/http/api/resources/stats.js new file mode 100644 index 0000000000..1670951bbe --- /dev/null +++ b/src/http/api/resources/stats.js @@ -0,0 +1,5 @@ +'use strict' + +exports = module.exports + +exports.bitswap = require('./bitswap').stat diff --git a/src/http/api/routes/index.js b/src/http/api/routes/index.js index 908c0c0878..6e26962908 100644 --- a/src/http/api/routes/index.js +++ b/src/http/api/routes/index.js @@ -16,4 +16,5 @@ module.exports = (server) => { require('./debug')(server) require('./webui')(server) require('./dns')(server) + require('./stats')(server) } diff --git a/src/http/api/routes/stats.js b/src/http/api/routes/stats.js new file mode 100644 index 0000000000..37abf0e611 --- /dev/null +++ b/src/http/api/routes/stats.js @@ -0,0 +1,31 @@ +'use strict' + +const resources = require('./../resources') + +module.exports = (server) => { + const api = server.select('API') + + api.route({ + method: '*', + path: '/api/v0/stats/bitswap', + config: { + handler: resources.stats.bitswap + } + }) + + /* api.route({ + method: '*', + path: '/api/v0/stats/bw', + config: { + handler: resources.stats.bw + } + }) */ + + /* api.route({ + method: '*', + path: '/api/v0/stats/repo', + config: { + handler: resources.stats.repo + } + }) */ +}