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

Commit

Permalink
feat: implement stats.vwbitswap and stats.repo
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 15, 2018
1 parent 48881a3 commit b8cd672
Show file tree
Hide file tree
Showing 21 changed files with 302 additions and 42 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,6 @@ A complete API definition is in the works. Meanwhile, you can learn how to you u
- [`ipfs.block.put(block, cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md#put)
- [`ipfs.block.stat(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md#stat)

- [repo](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/)
- `ipfs.repo.init`
- `ipfs.repo.version`
- `ipfs.repo.gc` (not implemented, yet!)

#### `Graph`

- [dag](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/DAG.md)
Expand Down Expand Up @@ -334,6 +329,17 @@ A complete API definition is in the works. Meanwhile, you can learn how to you u
- `ipfs.start([callback])`
- `ipfs.stop([callback])`
- `ipfs.isOnline()`

- [repo](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md)
- `ipfs.repo.init`
- [`ipfs.repo.stat([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#stat)
- [`ipfs.repo.version([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#version)
- `ipfs.repo.gc([options, callback])` (not implemented, yet!)

- [stats](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md)
- [`ipfs.stats.bitswap([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#bitswap)
- [`ipfs.stats.bw([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#bw) (not implemented, yet!)
- [`ipfs.stats.repo([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#repo)

- [config](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/CONFIG.md)
- [`ipfs.config.get([key, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/CONFIG.md#configget)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
},
"dependencies": {
"async": "^2.6.0",
"big.js": "^5.0.3",
"binary-querystring": "~0.1.2",
"bl": "^1.2.1",
"boom": "^7.1.1",
Expand All @@ -107,6 +108,7 @@
"hoek": "^5.0.3",
"ipfs-api": "^18.0.0",
"ipfs-bitswap": "~0.19.0",
"human-to-milliseconds": "^1.0.0",
"ipfs-block": "~0.6.1",
"ipfs-block-service": "~0.13.0",
"ipfs-multipart": "~0.1.0",
Expand Down
20 changes: 10 additions & 10 deletions src/cli/commands/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ module.exports = {
throw err
}

stats.Wantlist = stats.Wantlist || []
stats.Wantlist = stats.Wantlist.map((entry) => {
stats.wantlist = stats.wantlist || []
stats.wantlist = stats.wantlist.map((entry) => {
const buf = Buffer.from(entry.cid.hash.data)
const cid = new CID(entry.cid.version, entry.cid.codec, buf)
return cid.toBaseEncodedString()
})
stats.Peers = stats.Peers || []
stats.peers = stats.peers || []

print(`bitswap status
blocks received: ${stats.BlocksReceived}
dup blocks received: ${stats.DupBlksReceived}
dup data received: ${stats.DupDataReceived}B
wantlist [${stats.Wantlist.length} keys]
${stats.Wantlist.join('\n ')}
partners [${stats.Peers.length}]
${stats.Peers.join('\n ')}`)
blocks received: ${stats.blocksReceived}
dup blocks received: ${stats.dupBlksReceived}
dup data received: ${stats.dupDataReceived}B
wantlist [${stats.wantlist.length} keys]
${stats.wantlist.join('\n ')}
partners [${stats.peers.length}]
${stats.peers.join('\n ')}`)
})
}
}
31 changes: 31 additions & 0 deletions src/cli/commands/repo/stat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'stat',

describe: 'Get stats for the currently used repo',

builder: {
human: {
type: 'boolean',
default: false
}
},

handler (argv) {
argv.ipfs.repo.stat({human: argv.human}, (err, stats) => {
if (err) {
throw err
}

print(`repo status
number of objects: ${stats.numObjects}
repo size: ${stats.repoSize}
repo path: ${stats.repoPath}
version: ${stats.version}
maximum storage: ${stats.storageMax}`)
})
}
}
3 changes: 2 additions & 1 deletion src/cli/commands/repo/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ module.exports = {
builder: {},

handler (argv) {
argv.ipfs.repo.version(function (err, version) {
argv.ipfs.repo.version((err, version) => {
if (err) {
throw err
}

print(version)
})
}
Expand Down
14 changes: 14 additions & 0 deletions src/cli/commands/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

module.exports = {
command: 'stats <command>',

description: 'Query IPFS statistics.',

builder (yargs) {
return yargs.commandDir('stats')
},

handler (argv) {
}
}
37 changes: 37 additions & 0 deletions src/cli/commands/stats/bitswap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

const CID = require('cids')
const print = require('../../utils').print

module.exports = {
command: 'bitswap',

describe: 'Show some diagnostic information on the bitswap agent.',

builder: {},

handler (argv) {
argv.ipfs.stats.bitswap((err, stats) => {
if (err) {
throw err
}

stats.wantlist = stats.wantlist || []
stats.wantlist = stats.wantlist.map((entry) => {
const buf = Buffer.from(entry.cid.hash.data)
const cid = new CID(entry.cid.version, entry.cid.codec, buf)
return cid.toBaseEncodedString()
})
stats.peers = stats.peers || []

print(`bitswap status
blocks received: ${stats.blocksReceived}
dup blocks received: ${stats.dupBlksReceived}
dup data received: ${stats.dupDataReceived}B
wantlist [${stats.wantlist.length} keys]
${stats.wantlist.join('\n ')}
partners [${stats.peers.length}]
${stats.peers.join('\n ')}`)
})
}
}
31 changes: 31 additions & 0 deletions src/cli/commands/stats/repo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'repo',

describe: 'Get stats for the currently used repo',

builder: {
human: {
type: 'boolean',
default: false
}
},

handler (argv) {
argv.ipfs.stats.repo({human: argv.human}, (err, stats) => {
if (err) {
throw err
}

print(`repo status
number of objects: ${stats.numObjects}
repo size: ${stats.repoSize}
repo path: ${stats.repoPath}
version: ${stats.version}
maximum storage: ${stats.storageMax}`)
})
}
}
23 changes: 18 additions & 5 deletions src/core/components/bitswap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
const promisify = require('promisify-es6')
const Big = require('big.js')

function formatWantlist (list) {
return Array.from(list).map((e) => e[1])
Expand All @@ -16,16 +18,27 @@ 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: parseInt(snapshot.providesBufferLength.toString()),
blocksReceived: new Big(snapshot.blocksReceived),
wantlist: formatWantlist(self._bitswap.getWantlist()),
peers: self._bitswap.peers().map((id) => id.toB58String())
peers: self._bitswap.peers().map((id) => id.toB58String()),
dupBlksReceived: new Big(snapshot.dupBlksReceived),
dupDataReceived: new Big(snapshot.dupDataReceived),
dataReceived: new Big(snapshot.dataReceived),
blocksSent: new Big(snapshot.blocksSent),
dataSent: new Big(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 @@ -22,3 +22,4 @@ exports.pubsub = require('./pubsub')
exports.dht = require('./dht')
exports.dns = require('./dns')
exports.key = require('./key')
exports.stats = require('./stats')
27 changes: 24 additions & 3 deletions src/core/components/repo.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
'use strict'

const promisify = require('promisify-es6')

module.exports = function repo (self) {
return {
init: (bits, empty, callback) => {
// 1. check if repo already exists
},

version: (callback) => {
version: promisify((callback) => {
self._repo.version.get(callback)
},
}),

gc: () => {},

stat: promisify((options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

self._repo.stat(options, (err, stats) => {
if (err) return callback(err)

gc: function () {},
callback(null, {
numObjects: stats.numObjects,
repoSize: stats.repoSize,
repoPath: stats.repoPath,
version: stats.version.toString(),
storageMax: stats.storageMax
})
})
}),

path: () => self._repo.path
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ module.exports = (self) => {
self._bitswap = new Bitswap(
self._libp2pNode,
self._repo.blocks,
self._peerInfoBook
Object.assign({
statsEnabled: true
}, self._peerInfoBook)
)

self._bitswap.start()
Expand Down
8 changes: 8 additions & 0 deletions src/core/components/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

module.exports = function stats (self) {
return {
bitswap: require('./bitswap')(self).stat,
repo: require('./repo')(self).stat
}
}
1 change: 1 addition & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class IPFS extends EventEmitter {
this.dht = components.dht(this)
this.dns = components.dns(this)
this.key = components.key(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 @@ -14,3 +14,4 @@ exports.files = require('./files')
exports.pubsub = require('./pubsub')
exports.dns = require('./dns')
exports.key = require('./key')
exports.stats = require('./stats')
Loading

0 comments on commit b8cd672

Please sign in to comment.