Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: implement bitswap.stat and stats.bitswap according to SPEC
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jan 29, 2018
1 parent 3bca165 commit 6d4962d
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 18 deletions.
20 changes: 15 additions & 5 deletions src/core/components/bitswap.js
Original file line number Diff line number Diff line change
@@ -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])
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/core/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ exports.bitswap = require('./bitswap')
exports.pubsub = require('./pubsub')
exports.dht = require('./dht')
exports.dns = require('./dns')
exports.stats = require('./stats')
13 changes: 13 additions & 0 deletions src/core/components/stats.js
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
34 changes: 21 additions & 13 deletions src/http/api/resources/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})
}

Expand Down
1 change: 1 addition & 0 deletions src/http/api/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ exports.file = require('./file')
exports.files = require('./files')
exports.pubsub = require('./pubsub')
exports.dns = require('./dns')
exports.stats = require('./stats')
5 changes: 5 additions & 0 deletions src/http/api/resources/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

exports = module.exports

exports.bitswap = require('./bitswap').stat
1 change: 1 addition & 0 deletions src/http/api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ module.exports = (server) => {
require('./debug')(server)
require('./webui')(server)
require('./dns')(server)
require('./stats')(server)
}
31 changes: 31 additions & 0 deletions src/http/api/routes/stats.js
Original file line number Diff line number Diff line change
@@ -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
}
}) */
}

0 comments on commit 6d4962d

Please sign in to comment.