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

feat: add bitswap stat cli human option #2619

Merged
merged 3 commits into from
Nov 22, 2019
Merged
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 @@ -164,6 +164,7 @@
"peer-book": "^0.9.1",
"peer-id": "^0.12.2",
"peer-info": "~0.15.1",
"pretty-bytes": "^5.3.0",
"progress": "^2.0.1",
"promise-nodeify": "^3.0.1",
"promisify-es6": "^1.0.3",
Expand Down
39 changes: 29 additions & 10 deletions src/cli/commands/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const multibase = require('multibase')
const { cidToString } = require('../../../utils/cid')
const prettyBytes = require('pretty-bytes')

module.exports = {
command: 'stat',
Expand All @@ -13,24 +14,42 @@ 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: {
type: 'boolean',
default: false
}
},

handler ({ getIpfs, print, cidBase, resolve }) {
handler ({ getIpfs, print, 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.blocksReceived = stats.blocksReceived.toNumber()
stats.blocksSent = stats.blocksSent.toNumber()
stats.dataReceived = prettyBytes(stats.dataReceived.toNumber()).toUpperCase()
stats.dataSent = prettyBytes(stats.dataSent.toNumber()).toUpperCase()
stats.dupBlksReceived = stats.dupBlksReceived.toNumber()
stats.dupDataReceived = prettyBytes(stats.dupDataReceived.toNumber()).toUpperCase()
stats.wantlist = `[${stats.wantlist.length} keys]`
} else {
const wantlist = stats.wantlist.map((elem) => cidToString(elem['/'], { base: cidBase, upgrade: false }))
stats.wantlist = `[${wantlist.length} keys]
${wantlist.join('\n ')}`
}

print(`bitswap status
blocks received: ${stats.blocksReceived}
dup blocks received: ${stats.dupBlksReceived}
dup data received: ${stats.dupDataReceived}B
wantlist [${stats.wantlist.length} keys]
${stats.wantlist.join('\n ')}
partners [${stats.peers.length}]
${stats.peers.join('\n ')}`)
provides buffer: ${stats.provideBufLen}
blocks received: ${stats.blocksReceived}
blocks sent: ${stats.blocksSent}
data received: ${stats.dataReceived}
data sent: ${stats.dataSent}
dup blocks received: ${stats.dupBlksReceived}
dup data received: ${stats.dupDataReceived}
wantlist ${stats.wantlist}
partners [${stats.peers.length}]`)
})())
}
}
39 changes: 30 additions & 9 deletions test/cli/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,38 @@ describe('bitswap', () => runOn((thing) => {
this.timeout(20 * 1000)

const out = await ipfs('bitswap stat')
expect(out).to.include([
'bitswap status',
' blocks received: 0',
' dup blocks received: 0',
' dup data received: 0B',
// 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]'
].join('\n'))

expect(out).to.include('bitswap status')
expect(out).to.match(/provides buffer:\s\d+$/m)
expect(out).to.match(/blocks received:\s\d+$/m)
expect(out).to.match(/blocks sent:\s\d+$/m)
expect(out).to.match(/data received:\s\d+$/m)
expect(out).to.match(/data sent:\s\d+$/m)
expect(out).to.match(/dup blocks received:\s\d+$/m)
expect(out).to.match(/dup data received:\s\d+$/m)
expect(out).to.match(/wantlist\s\[\d+\skeys\]$/m)
expect(out).to.include(key0)
expect(out).to.include(key1)
expect(out).to.match(/partners\s\[\d+\]$/m)
})

it('stat --human', async function () {
this.timeout(20 * 1000)

const out = await ipfs('bitswap stat --human')

expect(out).to.include('bitswap status')
expect(out).to.match(/provides buffer:\s\d+$/m)
expect(out).to.match(/blocks received:\s\d+$/m)
expect(out).to.match(/blocks sent:\s\d+$/m)
expect(out).to.match(/data received:\s+[\d.]+\s[PTGMK]?B$/m)
expect(out).to.match(/data sent:\s+[\d.]+\s[PTGMK]?B$/m)
expect(out).to.match(/dup blocks received:\s\d+$/m)
expect(out).to.match(/dup data received:\s+[\d.]+\s[PTGMK]?B$/m)
expect(out).to.match(/wantlist\s\[\d+\skeys\]$/m)
expect(out).to.not.include(key0)
expect(out).to.not.include(key1)
expect(out).to.match(/partners\s\[\d+\]$/m)
})

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