From 2b6e433e08b68013b8c60c8698708fd392a1cfbf Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Thu, 7 Nov 2019 15:33:16 +0000 Subject: [PATCH 1/3] fix: human readable option --- package.json | 2 ++ src/index.js | 21 +++++++++++++-------- test/repo-test.js | 8 ++++++-- test/stat-test.js | 7 +++---- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 3ae08c53..e4b0d7b2 100644 --- a/package.json +++ b/package.json @@ -57,12 +57,14 @@ "dependencies": { "base32.js": "~0.1.0", "bignumber.js": "^9.0.0", + "bytes": "^3.1.0", "cids": "~0.7.0", "datastore-core": "~0.7.0", "datastore-fs": "~0.9.0", "datastore-level": "~0.12.0", "debug": "^4.1.0", "err-code": "^1.1.2", + "filesize": "^6.0.0", "interface-datastore": "~0.7.0", "ipfs-block": "~0.8.1", "ipfs-repo-migrations": "~0.1.0", diff --git a/src/index.js b/src/index.js index 83e0c9dd..ac4d5846 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,8 @@ const debug = require('debug') const Big = require('bignumber.js') const errcode = require('err-code') const migrator = require('ipfs-repo-migrations') +const filesize = require('filesize') +const bytes = require('bytes') const constants = require('./constants') const backends = require('./backends') @@ -258,19 +260,22 @@ class IpfsRepo { getSize(this.datastore), getSize(this.keys) ]) - let size = blocks.size + const size = blocks.size .plus(datastore) .plus(keys) - if (options.human) { - size = size.div(1048576) - } return { repoPath: this.path, - storageMax: storageMax, + storageMax: options.human + ? filesize(storageMax, { exponent: 3 }) // exponent 3 specifies the symbol GB for base 2 + : storageMax, version: version, - numObjects: blocks.count, - repoSize: size + numObjects: options.human + ? blocks.count.toNumber() + : blocks.count, + repoSize: options.human + ? filesize(size, { exponent: 2 }) // exponent 2 specifies the symbol MB for base 2 + : size } } @@ -308,7 +313,7 @@ class IpfsRepo { async _storageMaxStat () { try { const max = await this.config.get('Datastore.StorageMax') - return new Big(max) + return new Big(bytes(max)) } catch (err) { return new Big(noLimit) } diff --git a/test/repo-test.js b/test/repo-test.js index d4f62e21..8765c47f 100644 --- a/test/repo-test.js +++ b/test/repo-test.js @@ -8,6 +8,7 @@ const path = require('path') const IPFSRepo = require('../') const Errors = require('../src/errors') const os = require('os') +const bytes = require('bytes') module.exports = (repo) => { describe('IPFS Repo Tests', () => { @@ -247,14 +248,17 @@ module.exports = (repo) => { }) it('should return the max storage stat when set', async () => { + const maxStorage = '1GB' + otherRepo = new IPFSRepo(path.join(os.tmpdir(), 'repo-' + Date.now())) await otherRepo.init({}) await otherRepo.open() - await otherRepo.config.set('Datastore.StorageMax', 100) + await otherRepo.config.set('Datastore.StorageMax', maxStorage) const stat = await otherRepo.stat({}) - expect(stat.storageMax.toNumber()).to.equal(100) + expect(stat).to.have.property('storageMax') + expect(stat.storageMax.toNumber()).to.equal(bytes(maxStorage)) }) it('should throw unexpected errors when closing', async () => { diff --git a/test/stat-test.js b/test/stat-test.js index aceb8938..11e80d5e 100644 --- a/test/stat-test.js +++ b/test/stat-test.js @@ -36,13 +36,12 @@ module.exports = (repo) => { const stats = await repo.stat({ human: true }) expect(stats).to.exist() expect(stats).to.have.property('numObjects') - expect(stats).to.have.property('version') + expect(stats).to.have.property('version').and.be.above(0) expect(stats).to.have.property('repoPath') - expect(stats).to.have.property('repoSize') - expect(stats).to.have.property('storageMax') + expect(stats).to.have.property('repoSize').that.includes('MB') + expect(stats).to.have.property('storageMax').that.includes('GB') expect(stats.numObjects > '0').to.eql(true) - expect(stats.version > '0').to.eql(true) expect(stats.repoSize > '0').to.eql(true) expect(stats.storageMax > '0').to.eql(true) }) From 6d8be73399a1c8936bed6bf0393250311fc79f21 Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Fri, 8 Nov 2019 14:55:17 +0000 Subject: [PATCH 2/3] chore: code review changes --- package.json | 2 +- src/index.js | 6 +++--- test/stat-test.js | 27 +++++++++++++++++---------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index e4b0d7b2..73e17e5f 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,6 @@ "datastore-level": "~0.12.0", "debug": "^4.1.0", "err-code": "^1.1.2", - "filesize": "^6.0.0", "interface-datastore": "~0.7.0", "ipfs-block": "~0.8.1", "ipfs-repo-migrations": "~0.1.0", @@ -72,6 +71,7 @@ "just-safe-set": "^2.1.0", "lodash.has": "^4.5.2", "p-queue": "^6.0.0", + "pretty-bytes": "^5.3.0", "proper-lockfile": "^4.0.0", "sort-keys": "^3.0.0" }, diff --git a/src/index.js b/src/index.js index ac4d5846..b6f0f972 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ const debug = require('debug') const Big = require('bignumber.js') const errcode = require('err-code') const migrator = require('ipfs-repo-migrations') -const filesize = require('filesize') +const prettyBytes = require('pretty-bytes') const bytes = require('bytes') const constants = require('./constants') @@ -267,14 +267,14 @@ class IpfsRepo { return { repoPath: this.path, storageMax: options.human - ? filesize(storageMax, { exponent: 3 }) // exponent 3 specifies the symbol GB for base 2 + ? prettyBytes(storageMax.toNumber()) : storageMax, version: version, numObjects: options.human ? blocks.count.toNumber() : blocks.count, repoSize: options.human - ? filesize(size, { exponent: 2 }) // exponent 2 specifies the symbol MB for base 2 + ? prettyBytes(size.toNumber()) : size } } diff --git a/test/stat-test.js b/test/stat-test.js index 11e80d5e..293ce765 100644 --- a/test/stat-test.js +++ b/test/stat-test.js @@ -6,6 +6,7 @@ chai.use(require('dirty-chai')) const expect = chai.expect const Block = require('ipfs-block') const CID = require('cids') +const prettyBytes = require('pretty-bytes') module.exports = (repo) => { describe('stat', () => { @@ -33,17 +34,23 @@ module.exports = (repo) => { }) it('get human stats', async () => { - const stats = await repo.stat({ human: true }) - expect(stats).to.exist() - expect(stats).to.have.property('numObjects') - expect(stats).to.have.property('version').and.be.above(0) - expect(stats).to.have.property('repoPath') - expect(stats).to.have.property('repoSize').that.includes('MB') - expect(stats).to.have.property('storageMax').that.includes('GB') + const { repoSize, storageMax } = await repo.stat() - expect(stats.numObjects > '0').to.eql(true) - expect(stats.repoSize > '0').to.eql(true) - expect(stats.storageMax > '0').to.eql(true) + const humanizedRepoSize = prettyBytes(repoSize.toNumber()) + const humanizedStorageMax = prettyBytes(storageMax.toNumber()) + + const humanizedStats = await repo.stat({ human: true }) + + expect(humanizedStats).to.exist() + expect(humanizedStats).to.have.property('numObjects') + expect(humanizedStats).to.have.property('version').and.be.above(0) + expect(humanizedStats).to.have.property('repoPath') + expect(humanizedStats).to.have.property('repoSize').that.equals(humanizedRepoSize) + expect(humanizedStats).to.have.property('storageMax').that.equals(humanizedStorageMax) + + expect(humanizedStats.numObjects > '0').to.eql(true) + expect(humanizedStats.repoSize > '0').to.eql(true) + expect(humanizedStats.storageMax > '0').to.eql(true) }) }) } From 054261d0276a0005360fc9434109f95ca9101a2f Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Mon, 11 Nov 2019 08:54:41 +0000 Subject: [PATCH 3/3] chore: make output consistent with go implementation --- src/index.js | 4 ++-- test/stat-test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index b6f0f972..a05b4d4f 100644 --- a/src/index.js +++ b/src/index.js @@ -267,14 +267,14 @@ class IpfsRepo { return { repoPath: this.path, storageMax: options.human - ? prettyBytes(storageMax.toNumber()) + ? prettyBytes(storageMax.toNumber()).toUpperCase() : storageMax, version: version, numObjects: options.human ? blocks.count.toNumber() : blocks.count, repoSize: options.human - ? prettyBytes(size.toNumber()) + ? prettyBytes(size.toNumber()).toUpperCase() : size } } diff --git a/test/stat-test.js b/test/stat-test.js index 293ce765..b88db655 100644 --- a/test/stat-test.js +++ b/test/stat-test.js @@ -36,8 +36,8 @@ module.exports = (repo) => { it('get human stats', async () => { const { repoSize, storageMax } = await repo.stat() - const humanizedRepoSize = prettyBytes(repoSize.toNumber()) - const humanizedStorageMax = prettyBytes(storageMax.toNumber()) + const humanizedRepoSize = prettyBytes(repoSize.toNumber()).toUpperCase() + const humanizedStorageMax = prettyBytes(storageMax.toNumber()).toUpperCase() const humanizedStats = await repo.stat({ human: true })