From 92f8d4cefd88c1dda4f5d389df0a015780e7d46e Mon Sep 17 00:00:00 2001 From: Chirag Shinde Date: Thu, 23 May 2019 21:38:25 +0530 Subject: [PATCH 1/2] cli: add --human flag to repo stat & stats repo - Added a `humanize` object to /src/cli/utils with `Bytes` method that takes bytes and returns Human readable size. - Used `humanize.Bytes()` method in /commands/stats/repo and /commands/repo/stat if `--human` flag is passed. License: MIT Signed-off-by: Chirag Shinde --- src/cli/commands/repo/stat.js | 7 ++++++- src/cli/commands/stats/repo.js | 7 ++++++- src/cli/utils.js | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) 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] + } +} From 69523e875e500696f5bc3b939d18a753cecefad6 Mon Sep 17 00:00:00 2001 From: Chirag Shinde Date: Sat, 1 Jun 2019 16:33:48 +0530 Subject: [PATCH 2/2] fix(cli): add human flag to repo stat & stats repo - Use filesize to return human readable sizes. - handler now returns stats instead of printing them. License: MIT Signed-off-by: Chirag Shinde --- package.json | 3 ++- src/cli/commands/repo/stat.js | 11 +++++------ src/cli/commands/stats/repo.js | 11 +++++------ src/cli/utils.js | 15 --------------- 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index ac8b1167cb..2a5205ffad 100644 --- a/package.json +++ b/package.json @@ -109,7 +109,8 @@ "debug": "^4.1.0", "dlv": "^1.1.3", "err-code": "^1.1.2", - "file-type": "^11.1.0", + "file-type": "^11.0.0", + "filesize": "^4.1.2", "fnv1a": "^1.0.1", "fsm-event": "^2.1.0", "get-folder-size": "^2.0.0", diff --git a/src/cli/commands/repo/stat.js b/src/cli/commands/repo/stat.js index 179adc5051..a7bab3cf1f 100644 --- a/src/cli/commands/repo/stat.js +++ b/src/cli/commands/repo/stat.js @@ -1,7 +1,6 @@ 'use strict' -const print = require('../../utils').print -const humanize = require('../../utils').humanize +const filesize = require('filesize') module.exports = { command: 'stat', @@ -20,15 +19,15 @@ module.exports = { const ipfs = await argv.getIpfs() const stats = await ipfs.repo.stat() if (argv.human) { - stats.repoSize = humanize.Bytes(stats.repoSize) - stats.storageMax = humanize.Bytes(stats.storageMax) + stats.repoSize = filesize(stats.repoSize) + stats.storageMax = filesize(stats.storageMax) } - print(`repo status + return `repo status number of objects: ${stats.numObjects} repo size: ${stats.repoSize} repo path: ${stats.repoPath} version: ${stats.version} - maximum storage: ${stats.storageMax}`) + maximum storage: ${stats.storageMax}` })()) } } diff --git a/src/cli/commands/stats/repo.js b/src/cli/commands/stats/repo.js index ef4e5ff194..4abf5f45cd 100644 --- a/src/cli/commands/stats/repo.js +++ b/src/cli/commands/stats/repo.js @@ -1,7 +1,6 @@ 'use strict' -const print = require('../../utils').print -const humanize = require('../../utils').humanize +const filesize = require('filesize') module.exports = { command: 'repo', @@ -20,15 +19,15 @@ module.exports = { const ipfs = await argv.getIpfs() const stats = await ipfs.stats.repo() if (argv.human) { - stats.repoSize = humanize.Bytes(stats.repoSize) - stats.storageMax = humanize.Bytes(stats.storageMax) + stats.repoSize = filesize(stats.repoSize) + stats.storageMax = filesize(stats.storageMax) } - print(`repo status + return `repo status number of objects: ${stats.numObjects} repo size: ${stats.repoSize} repo path: ${stats.repoPath} version: ${stats.version} - maximum storage: ${stats.storageMax}`) + maximum storage: ${stats.storageMax}` })()) } } diff --git a/src/cli/utils.js b/src/cli/utils.js index 6882c518c0..88637bf06b 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -135,18 +135,3 @@ 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] - } -}