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

feat(bitswap humanize): add humanize flag support to bitswap cli command #2109

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
13 changes: 11 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 { humanize } = require('../../utils')

module.exports = {
command: 'stat',
Expand All @@ -14,20 +15,28 @@ 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 = humanize.Bytes(stats.dupDataReceived)
}

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
7 changes: 6 additions & 1 deletion src/cli/commands/repo/stat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const print = require('../../utils').print
const humanize = require('../../utils').humanize

module.exports = {
command: 'stat',
Expand All @@ -17,7 +18,11 @@ module.exports = {
handler (argv) {
argv.resolve((async () => {
const ipfs = await argv.getIpfs()
const stats = await ipfs.repo.stat({ human: argv.human })
const stats = await ipfs.repo.stat()
if (argv.human) {
stats.repoSize = humanize.Bytes(stats.repoSize)
stats.storageMax = humanize.Bytes(stats.storageMax)
}
print(`repo status
number of objects: ${stats.numObjects}
repo size: ${stats.repoSize}
Expand Down
7 changes: 6 additions & 1 deletion src/cli/commands/stats/repo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const print = require('../../utils').print
const humanize = require('../../utils').humanize

module.exports = {
command: 'repo',
Expand All @@ -17,7 +18,11 @@ module.exports = {
handler (argv) {
argv.resolve((async () => {
const ipfs = await argv.getIpfs()
const stats = await ipfs.stats.repo({ human: argv.human })
const stats = await ipfs.stats.repo()
if (argv.human) {
stats.repoSize = humanize.Bytes(stats.repoSize)
stats.storageMax = humanize.Bytes(stats.storageMax)
}
print(`repo status
number of objects: ${stats.numObjects}
repo size: ${stats.repoSize}
Expand Down
15 changes: 15 additions & 0 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,18 @@ exports.singleton = create => {
})
return getter
}
exports.humanize = {
Bytes: (bytes) => {
const thresh = 1024
if (Math.abs(bytes) < thresh) {
return bytes + ' B'
}
const units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
let unit = -1
do {
bytes /= thresh
++unit
} while (Math.abs(bytes) >= thresh && unit < units.length - 1)
return bytes.toFixed(1) + ' ' + units[unit]
}
}