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

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jan 29, 2018
1 parent f426801 commit a3ddcbb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 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
38 changes: 21 additions & 17 deletions src/http/api/resources/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +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({
ProvideBufLen: stats.providesBufferLength,
BlocksReceived: stats.blocksReceived,
Wantlist: stats.wantlist,
Peers: stats.peers,
DupBlksReceived: stats.dupBlksReceived,
DupDataReceived: stats.dupDataReceived,
DataReceived: stats.dataReceived,
BlocksSent: stats.blocksSent,
DataSent: stats.dataSent
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

0 comments on commit a3ddcbb

Please sign in to comment.