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

feat(bitswap humanize): add humanize flag (--human) support to bitswap stat #2150

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"dlv": "^1.1.3",
"err-code": "^1.1.2",
"file-type": "^11.1.0",
"filesize": "^4.1.2",
"fnv1a": "^1.0.1",
"fsm-event": "^2.1.0",
"get-folder-size": "^2.0.0",
Expand Down
15 changes: 13 additions & 2 deletions src/cli/commands/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const multibase = require('multibase')
const { print } = require('../../utils')
const { cidToString } = require('../../../utils/cid')
const filesize = require('filesize')

module.exports = {
command: 'stat',
Expand All @@ -14,20 +15,30 @@ module.exports = {
describe: 'Number base to display CIDs in. Note: specifying a CID base for v0 CIDs will have no effect.',
type: 'string',
choices: multibase.names
},
human: {
describe: 'Print sizes in human readable format (e.g., 1K 234M 2G).',
type: 'boolean',
default: false
}
},

handler ({ getIpfs, cidBase, resolve }) {
handler ({ getIpfs, cidBase, resolve, human }) {
resolve((async () => {
const ipfs = await getIpfs()
const stats = await ipfs.bitswap.stat()
stats.wantlist = stats.wantlist.map(k => cidToString(k['/'], { base: cidBase, upgrade: false }))
stats.peers = stats.peers || []
if (human) {
stats.dupDataReceived = filesize(stats.dupDataReceived)
} else {
stats.dupDataReceived += ' B'
}

print(`bitswap status
blocks received: ${stats.blocksReceived}
dup blocks received: ${stats.dupBlksReceived}
dup data received: ${stats.dupDataReceived}B
dup data received: ${stats.dupDataReceived}
wantlist [${stats.wantlist.length} keys]
${stats.wantlist.join('\n ')}
partners [${stats.peers.length}]
Expand Down
9 changes: 8 additions & 1 deletion test/cli/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('bitswap', () => runOn((thing) => {
'bitswap status',
' blocks received: 0',
' dup blocks received: 0',
' dup data received: 0B',
' dup data received: 0 B',
// We sometimes pick up partners while the tests run and the order of
// wanted keys is not defined so our assertion ends here.
' wantlist [2 keys]'
Expand All @@ -88,6 +88,13 @@ describe('bitswap', () => runOn((thing) => {
})
})

it('stat with --human flag', function () {
this.timeout(20 * 1000)
return ipfs('bitswap stat --human').then((out) => {
expect(out).to.include('dup data received: 0 B')
})
})

it('should get stats with wantlist CIDs encoded in specified base', function () {
this.timeout(20 * 1000)
return ipfs('bitswap stat --cid-base=base64').then((out) => {
Expand Down