diff --git a/src/cli/commands/bitswap/stat.js b/src/cli/commands/bitswap/stat.js index 12ef6587c5..17406ae388 100644 --- a/src/cli/commands/bitswap/stat.js +++ b/src/cli/commands/bitswap/stat.js @@ -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', @@ -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}] diff --git a/src/cli/commands/repo/stat.js b/src/cli/commands/repo/stat.js index 660a7ff444..179adc5051 100644 --- a/src/cli/commands/repo/stat.js +++ b/src/cli/commands/repo/stat.js @@ -1,6 +1,7 @@ 'use strict' const print = require('../../utils').print +const humanize = require('../../utils').humanize module.exports = { command: 'stat', @@ -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} diff --git a/src/cli/commands/stats/repo.js b/src/cli/commands/stats/repo.js index 343b8d9617..ef4e5ff194 100644 --- a/src/cli/commands/stats/repo.js +++ b/src/cli/commands/stats/repo.js @@ -1,6 +1,7 @@ 'use strict' const print = require('../../utils').print +const humanize = require('../../utils').humanize module.exports = { command: 'repo', @@ -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} diff --git a/src/cli/utils.js b/src/cli/utils.js index 88637bf06b..6882c518c0 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -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] + } +}