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

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

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

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: 18 additions & 5 deletions
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

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)