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

Commit 19adbce

Browse files
hacdiasdryajov
authored andcommitted
feat: stats API (stats.bitswap and stats.repo) (#1198)
1 parent a533118 commit 19adbce

File tree

23 files changed

+304
-46
lines changed

23 files changed

+304
-46
lines changed

README.md

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

265-
- [repo](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/)
266-
- `ipfs.repo.init`
267-
- `ipfs.repo.version`
268-
- `ipfs.repo.gc` (not implemented, yet!)
269-
270265
#### `Graph`
271266

272267
- [dag](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/DAG.md)
@@ -340,6 +335,17 @@ A complete API definition is in the works. Meanwhile, you can learn how to you u
340335
- `ipfs.start([callback])`
341336
- [`ipfs.stop([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/MISCELLANEOUS.md#stop)
342337
- `ipfs.isOnline()`
338+
339+
- [repo](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md)
340+
- `ipfs.repo.init`
341+
- [`ipfs.repo.stat([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#stat)
342+
- [`ipfs.repo.version([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/REPO.md#version)
343+
- `ipfs.repo.gc([options, callback])` (not implemented, yet!)
344+
345+
- [stats](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md)
346+
- [`ipfs.stats.bitswap([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#bitswap)
347+
- [`ipfs.stats.bw([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#bw) (not implemented, yet!)
348+
- [`ipfs.stats.repo([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/STATS.md#repo)
343349

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

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"form-data": "^2.3.2",
7474
"go-ipfs-dep": "^0.4.13",
7575
"hat": "0.0.3",
76-
"interface-ipfs-core": "~0.51.0",
76+
"interface-ipfs-core": "~0.52.0",
7777
"ipfsd-ctl": "~0.28.0",
7878
"left-pad": "^1.2.0",
7979
"lodash": "^4.17.5",
@@ -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

Lines changed: 10 additions & 10 deletions
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

Lines changed: 31 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 14 additions & 0 deletions
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

Lines changed: 37 additions & 0 deletions
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

Lines changed: 31 additions & 0 deletions
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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'use strict'
22

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

58
function formatWantlist (list) {
69
return Array.from(list).map((e) => e[1])
@@ -16,16 +19,27 @@ module.exports = function bitswap (self) {
1619
const list = self._bitswap.getWantlist()
1720
return formatWantlist(list)
1821
},
19-
stat: () => {
22+
23+
stat: promisify((callback) => {
2024
if (!self.isOnline()) {
21-
throw new Error(OFFLINE_ERROR)
25+
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
2226
}
2327

24-
return Object.assign({}, self._bitswap.stat().snapshot, {
28+
const snapshot = self._bitswap.stat().snapshot
29+
30+
callback(null, {
31+
provideBufLen: parseInt(snapshot.providesBufferLength.toString()),
32+
blocksReceived: new Big(snapshot.blocksReceived),
2533
wantlist: formatWantlist(self._bitswap.getWantlist()),
26-
peers: self._bitswap.peers().map((id) => id.toB58String())
34+
peers: self._bitswap.peers().map((id) => id.toB58String()),
35+
dupBlksReceived: new Big(snapshot.dupBlksReceived),
36+
dupDataReceived: new Big(snapshot.dupDataReceived),
37+
dataReceived: new Big(snapshot.dataReceived),
38+
blocksSent: new Big(snapshot.blocksSent),
39+
dataSent: new Big(snapshot.dataSent)
2740
})
28-
},
41+
}),
42+
2943
unwant: (key) => {
3044
if (!self.isOnline()) {
3145
throw new Error(OFFLINE_ERROR)

src/core/components/index.js

Lines changed: 1 addition & 0 deletions
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')

0 commit comments

Comments
 (0)