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

Commit b8cd672

Browse files
committed
feat: implement stats.vwbitswap and stats.repo
1 parent 48881a3 commit b8cd672

File tree

21 files changed

+302
-42
lines changed

21 files changed

+302
-42
lines changed

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,6 @@ A complete API definition is in the works. Meanwhile, you can learn how to you u
256256
- [`ipfs.block.put(block, cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md#put)
257257
- [`ipfs.block.stat(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md#stat)
258258

259-
- [repo](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/)
260-
- `ipfs.repo.init`
261-
- `ipfs.repo.version`
262-
- `ipfs.repo.gc` (not implemented, yet!)
263-
264259
#### `Graph`
265260

266261
- [dag](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/DAG.md)
@@ -334,6 +329,17 @@ A complete API definition is in the works. Meanwhile, you can learn how to you u
334329
- `ipfs.start([callback])`
335330
- `ipfs.stop([callback])`
336331
- `ipfs.isOnline()`
332+
333+
- [repo](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md)
334+
- `ipfs.repo.init`
335+
- [`ipfs.repo.stat([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#stat)
336+
- [`ipfs.repo.version([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#version)
337+
- `ipfs.repo.gc([options, callback])` (not implemented, yet!)
338+
339+
- [stats](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md)
340+
- [`ipfs.stats.bitswap([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#bitswap)
341+
- [`ipfs.stats.bw([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#bw) (not implemented, yet!)
342+
- [`ipfs.stats.repo([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#repo)
337343

338344
- [config](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/CONFIG.md)
339345
- [`ipfs.config.get([key, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/CONFIG.md#configget)

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
},
9191
"dependencies": {
9292
"async": "^2.6.0",
93+
"big.js": "^5.0.3",
9394
"binary-querystring": "~0.1.2",
9495
"bl": "^1.2.1",
9596
"boom": "^7.1.1",
@@ -107,6 +108,7 @@
107108
"hoek": "^5.0.3",
108109
"ipfs-api": "^18.0.0",
109110
"ipfs-bitswap": "~0.19.0",
111+
"human-to-milliseconds": "^1.0.0",
110112
"ipfs-block": "~0.6.1",
111113
"ipfs-block-service": "~0.13.0",
112114
"ipfs-multipart": "~0.1.0",

src/cli/commands/bitswap/stat.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ module.exports = {
1616
throw err
1717
}
1818

19-
stats.Wantlist = stats.Wantlist || []
20-
stats.Wantlist = stats.Wantlist.map((entry) => {
19+
stats.wantlist = stats.wantlist || []
20+
stats.wantlist = stats.wantlist.map((entry) => {
2121
const buf = Buffer.from(entry.cid.hash.data)
2222
const cid = new CID(entry.cid.version, entry.cid.codec, buf)
2323
return cid.toBaseEncodedString()
2424
})
25-
stats.Peers = stats.Peers || []
25+
stats.peers = stats.peers || []
2626

2727
print(`bitswap status
28-
blocks received: ${stats.BlocksReceived}
29-
dup blocks received: ${stats.DupBlksReceived}
30-
dup data received: ${stats.DupDataReceived}B
31-
wantlist [${stats.Wantlist.length} keys]
32-
${stats.Wantlist.join('\n ')}
33-
partners [${stats.Peers.length}]
34-
${stats.Peers.join('\n ')}`)
28+
blocks received: ${stats.blocksReceived}
29+
dup blocks received: ${stats.dupBlksReceived}
30+
dup data received: ${stats.dupDataReceived}B
31+
wantlist [${stats.wantlist.length} keys]
32+
${stats.wantlist.join('\n ')}
33+
partners [${stats.peers.length}]
34+
${stats.peers.join('\n ')}`)
3535
})
3636
}
3737
}

src/cli/commands/repo/stat.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
const print = require('../../utils').print
4+
5+
module.exports = {
6+
command: 'stat',
7+
8+
describe: 'Get stats for the currently used repo',
9+
10+
builder: {
11+
human: {
12+
type: 'boolean',
13+
default: false
14+
}
15+
},
16+
17+
handler (argv) {
18+
argv.ipfs.repo.stat({human: argv.human}, (err, stats) => {
19+
if (err) {
20+
throw err
21+
}
22+
23+
print(`repo status
24+
number of objects: ${stats.numObjects}
25+
repo size: ${stats.repoSize}
26+
repo path: ${stats.repoPath}
27+
version: ${stats.version}
28+
maximum storage: ${stats.storageMax}`)
29+
})
30+
}
31+
}

src/cli/commands/repo/version.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ module.exports = {
1010
builder: {},
1111

1212
handler (argv) {
13-
argv.ipfs.repo.version(function (err, version) {
13+
argv.ipfs.repo.version((err, version) => {
1414
if (err) {
1515
throw err
1616
}
17+
1718
print(version)
1819
})
1920
}

src/cli/commands/stats.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
module.exports = {
4+
command: 'stats <command>',
5+
6+
description: 'Query IPFS statistics.',
7+
8+
builder (yargs) {
9+
return yargs.commandDir('stats')
10+
},
11+
12+
handler (argv) {
13+
}
14+
}

src/cli/commands/stats/bitswap.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
const CID = require('cids')
4+
const print = require('../../utils').print
5+
6+
module.exports = {
7+
command: 'bitswap',
8+
9+
describe: 'Show some diagnostic information on the bitswap agent.',
10+
11+
builder: {},
12+
13+
handler (argv) {
14+
argv.ipfs.stats.bitswap((err, stats) => {
15+
if (err) {
16+
throw err
17+
}
18+
19+
stats.wantlist = stats.wantlist || []
20+
stats.wantlist = stats.wantlist.map((entry) => {
21+
const buf = Buffer.from(entry.cid.hash.data)
22+
const cid = new CID(entry.cid.version, entry.cid.codec, buf)
23+
return cid.toBaseEncodedString()
24+
})
25+
stats.peers = stats.peers || []
26+
27+
print(`bitswap status
28+
blocks received: ${stats.blocksReceived}
29+
dup blocks received: ${stats.dupBlksReceived}
30+
dup data received: ${stats.dupDataReceived}B
31+
wantlist [${stats.wantlist.length} keys]
32+
${stats.wantlist.join('\n ')}
33+
partners [${stats.peers.length}]
34+
${stats.peers.join('\n ')}`)
35+
})
36+
}
37+
}

src/cli/commands/stats/repo.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
const print = require('../../utils').print
4+
5+
module.exports = {
6+
command: 'repo',
7+
8+
describe: 'Get stats for the currently used repo',
9+
10+
builder: {
11+
human: {
12+
type: 'boolean',
13+
default: false
14+
}
15+
},
16+
17+
handler (argv) {
18+
argv.ipfs.stats.repo({human: argv.human}, (err, stats) => {
19+
if (err) {
20+
throw err
21+
}
22+
23+
print(`repo status
24+
number of objects: ${stats.numObjects}
25+
repo size: ${stats.repoSize}
26+
repo path: ${stats.repoPath}
27+
version: ${stats.version}
28+
maximum storage: ${stats.storageMax}`)
29+
})
30+
}
31+
}

src/core/components/bitswap.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

33
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
4+
const promisify = require('promisify-es6')
5+
const Big = require('big.js')
46

57
function formatWantlist (list) {
68
return Array.from(list).map((e) => e[1])
@@ -16,16 +18,27 @@ module.exports = function bitswap (self) {
1618
const list = self._bitswap.getWantlist()
1719
return formatWantlist(list)
1820
},
19-
stat: () => {
21+
22+
stat: promisify((callback) => {
2023
if (!self.isOnline()) {
21-
throw new Error(OFFLINE_ERROR)
24+
callback(new Error(OFFLINE_ERROR))
2225
}
2326

24-
return Object.assign({}, self._bitswap.stat().snapshot, {
27+
const snapshot = self._bitswap.stat().snapshot
28+
29+
callback(null, {
30+
provideBufLen: parseInt(snapshot.providesBufferLength.toString()),
31+
blocksReceived: new Big(snapshot.blocksReceived),
2532
wantlist: formatWantlist(self._bitswap.getWantlist()),
26-
peers: self._bitswap.peers().map((id) => id.toB58String())
33+
peers: self._bitswap.peers().map((id) => id.toB58String()),
34+
dupBlksReceived: new Big(snapshot.dupBlksReceived),
35+
dupDataReceived: new Big(snapshot.dupDataReceived),
36+
dataReceived: new Big(snapshot.dataReceived),
37+
blocksSent: new Big(snapshot.blocksSent),
38+
dataSent: new Big(snapshot.dataSent)
2739
})
28-
},
40+
}),
41+
2942
unwant: (key) => {
3043
if (!self.isOnline()) {
3144
throw new Error(OFFLINE_ERROR)

src/core/components/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ exports.pubsub = require('./pubsub')
2222
exports.dht = require('./dht')
2323
exports.dns = require('./dns')
2424
exports.key = require('./key')
25+
exports.stats = require('./stats')

src/core/components/repo.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
'use strict'
22

3+
const promisify = require('promisify-es6')
4+
35
module.exports = function repo (self) {
46
return {
57
init: (bits, empty, callback) => {
68
// 1. check if repo already exists
79
},
810

9-
version: (callback) => {
11+
version: promisify((callback) => {
1012
self._repo.version.get(callback)
11-
},
13+
}),
14+
15+
gc: () => {},
16+
17+
stat: promisify((options, callback) => {
18+
if (typeof options === 'function') {
19+
callback = options
20+
options = {}
21+
}
22+
23+
self._repo.stat(options, (err, stats) => {
24+
if (err) return callback(err)
1225

13-
gc: function () {},
26+
callback(null, {
27+
numObjects: stats.numObjects,
28+
repoSize: stats.repoSize,
29+
repoPath: stats.repoPath,
30+
version: stats.version.toString(),
31+
storageMax: stats.storageMax
32+
})
33+
})
34+
}),
1435

1536
path: () => self._repo.path
1637
}

src/core/components/start.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ module.exports = (self) => {
4545
self._bitswap = new Bitswap(
4646
self._libp2pNode,
4747
self._repo.blocks,
48-
self._peerInfoBook
48+
Object.assign({
49+
statsEnabled: true
50+
}, self._peerInfoBook)
4951
)
5052

5153
self._bitswap.start()

src/core/components/stats.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
module.exports = function stats (self) {
4+
return {
5+
bitswap: require('./bitswap')(self).stat,
6+
repo: require('./repo')(self).stat
7+
}
8+
}

src/core/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class IPFS extends EventEmitter {
9696
this.dht = components.dht(this)
9797
this.dns = components.dns(this)
9898
this.key = components.key(this)
99+
this.stats = components.stats(this)
99100

100101
if (this._options.EXPERIMENTAL.pubsub) {
101102
this.log('EXPERIMENTAL pubsub is enabled')

src/http/api/resources/bitswap.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,27 @@ exports.wantlist = (request, reply) => {
2121
}
2222

2323
exports.stat = (request, reply) => {
24-
let stats
25-
try {
26-
stats = request.server.app.ipfs.bitswap.stat()
27-
} catch (err) {
28-
return reply(boom.badRequest(err))
29-
}
30-
31-
reply({
32-
BlocksReceived: stats.blocksReceived,
33-
Wantlist: stats.wantlist,
34-
Peers: stats.peers,
35-
DupBlksReceived: stats.dupBlksReceived,
36-
DupDataReceived: stats.dupDataReceived
24+
const ipfs = request.server.app.ipfs
25+
26+
ipfs.bitswap.stat((err, stats) => {
27+
if (err) {
28+
return reply({
29+
Message: err.toString(),
30+
Code: 0
31+
}).code(500)
32+
}
33+
34+
reply({
35+
ProvideBufLen: stats.provideBufLen,
36+
BlocksReceived: stats.blocksReceived,
37+
Wantlist: stats.wantlist,
38+
Peers: stats.peers,
39+
DupBlksReceived: stats.dupBlksReceived,
40+
DupDataReceived: stats.dupDataReceived,
41+
DataReceived: stats.dataReceived,
42+
BlocksSent: stats.blocksSent,
43+
DataSent: stats.dataSent
44+
})
3745
})
3846
}
3947

src/http/api/resources/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ exports.files = require('./files')
1414
exports.pubsub = require('./pubsub')
1515
exports.dns = require('./dns')
1616
exports.key = require('./key')
17+
exports.stats = require('./stats')

0 commit comments

Comments
 (0)