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

Commit 6a2ea52

Browse files
PedroMiguelSSAlan Shaw
authored andcommitted
feat: add bitswap stat human option in CLI (#2619)
1 parent 5a1d266 commit 6a2ea52

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
"peer-book": "^0.9.1",
165165
"peer-id": "^0.12.2",
166166
"peer-info": "~0.15.1",
167+
"pretty-bytes": "^5.3.0",
167168
"progress": "^2.0.1",
168169
"promise-nodeify": "^3.0.1",
169170
"promisify-es6": "^1.0.3",

src/cli/commands/bitswap/stat.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multibase = require('multibase')
44
const { cidToString } = require('../../../utils/cid')
5+
const prettyBytes = require('pretty-bytes')
56

67
module.exports = {
78
command: 'stat',
@@ -13,24 +14,42 @@ module.exports = {
1314
describe: 'Number base to display CIDs in. Note: specifying a CID base for v0 CIDs will have no effect.',
1415
type: 'string',
1516
choices: multibase.names
17+
},
18+
human: {
19+
type: 'boolean',
20+
default: false
1621
}
1722
},
1823

19-
handler ({ getIpfs, print, cidBase, resolve }) {
24+
handler ({ getIpfs, print, cidBase, resolve, human }) {
2025
resolve((async () => {
2126
const ipfs = await getIpfs()
2227
const stats = await ipfs.bitswap.stat()
23-
stats.wantlist = stats.wantlist.map(k => cidToString(k['/'], { base: cidBase, upgrade: false }))
24-
stats.peers = stats.peers || []
28+
29+
if (human) {
30+
stats.blocksReceived = stats.blocksReceived.toNumber()
31+
stats.blocksSent = stats.blocksSent.toNumber()
32+
stats.dataReceived = prettyBytes(stats.dataReceived.toNumber()).toUpperCase()
33+
stats.dataSent = prettyBytes(stats.dataSent.toNumber()).toUpperCase()
34+
stats.dupBlksReceived = stats.dupBlksReceived.toNumber()
35+
stats.dupDataReceived = prettyBytes(stats.dupDataReceived.toNumber()).toUpperCase()
36+
stats.wantlist = `[${stats.wantlist.length} keys]`
37+
} else {
38+
const wantlist = stats.wantlist.map((elem) => cidToString(elem['/'], { base: cidBase, upgrade: false }))
39+
stats.wantlist = `[${wantlist.length} keys]
40+
${wantlist.join('\n ')}`
41+
}
2542

2643
print(`bitswap status
27-
blocks received: ${stats.blocksReceived}
28-
dup blocks received: ${stats.dupBlksReceived}
29-
dup data received: ${stats.dupDataReceived}B
30-
wantlist [${stats.wantlist.length} keys]
31-
${stats.wantlist.join('\n ')}
32-
partners [${stats.peers.length}]
33-
${stats.peers.join('\n ')}`)
44+
provides buffer: ${stats.provideBufLen}
45+
blocks received: ${stats.blocksReceived}
46+
blocks sent: ${stats.blocksSent}
47+
data received: ${stats.dataReceived}
48+
data sent: ${stats.dataSent}
49+
dup blocks received: ${stats.dupBlksReceived}
50+
dup data received: ${stats.dupDataReceived}
51+
wantlist ${stats.wantlist}
52+
partners [${stats.peers.length}]`)
3453
})())
3554
}
3655
}

test/cli/bitswap.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,38 @@ describe('bitswap', () => runOn((thing) => {
6767
this.timeout(20 * 1000)
6868

6969
const out = await ipfs('bitswap stat')
70-
expect(out).to.include([
71-
'bitswap status',
72-
' blocks received: 0',
73-
' dup blocks received: 0',
74-
' dup data received: 0B',
75-
// We sometimes pick up partners while the tests run and the order of
76-
// wanted keys is not defined so our assertion ends here.
77-
' wantlist [2 keys]'
78-
].join('\n'))
70+
71+
expect(out).to.include('bitswap status')
72+
expect(out).to.match(/provides buffer:\s\d+$/m)
73+
expect(out).to.match(/blocks received:\s\d+$/m)
74+
expect(out).to.match(/blocks sent:\s\d+$/m)
75+
expect(out).to.match(/data received:\s\d+$/m)
76+
expect(out).to.match(/data sent:\s\d+$/m)
77+
expect(out).to.match(/dup blocks received:\s\d+$/m)
78+
expect(out).to.match(/dup data received:\s\d+$/m)
79+
expect(out).to.match(/wantlist\s\[\d+\skeys\]$/m)
7980
expect(out).to.include(key0)
8081
expect(out).to.include(key1)
82+
expect(out).to.match(/partners\s\[\d+\]$/m)
83+
})
84+
85+
it('stat --human', async function () {
86+
this.timeout(20 * 1000)
87+
88+
const out = await ipfs('bitswap stat --human')
89+
90+
expect(out).to.include('bitswap status')
91+
expect(out).to.match(/provides buffer:\s\d+$/m)
92+
expect(out).to.match(/blocks received:\s\d+$/m)
93+
expect(out).to.match(/blocks sent:\s\d+$/m)
94+
expect(out).to.match(/data received:\s+[\d.]+\s[PTGMK]?B$/m)
95+
expect(out).to.match(/data sent:\s+[\d.]+\s[PTGMK]?B$/m)
96+
expect(out).to.match(/dup blocks received:\s\d+$/m)
97+
expect(out).to.match(/dup data received:\s+[\d.]+\s[PTGMK]?B$/m)
98+
expect(out).to.match(/wantlist\s\[\d+\skeys\]$/m)
99+
expect(out).to.not.include(key0)
100+
expect(out).to.not.include(key1)
101+
expect(out).to.match(/partners\s\[\d+\]$/m)
81102
})
82103

83104
it('should get stats with wantlist CIDs encoded in specified base', async function () {

0 commit comments

Comments
 (0)