Skip to content

Commit bf0f170

Browse files
PedroMiguelSSjacobheun
authored andcommitted
fix: human readable option (#213)
* fix: human readable option * chore: code review changes * chore: make output consistent with go implementation
1 parent b4b0970 commit bf0f170

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"dependencies": {
5858
"base32.js": "~0.1.0",
5959
"bignumber.js": "^9.0.0",
60+
"bytes": "^3.1.0",
6061
"cids": "~0.7.0",
6162
"datastore-core": "~0.7.0",
6263
"datastore-fs": "~0.9.0",
@@ -70,6 +71,7 @@
7071
"just-safe-set": "^2.1.0",
7172
"lodash.has": "^4.5.2",
7273
"p-queue": "^6.0.0",
74+
"pretty-bytes": "^5.3.0",
7375
"proper-lockfile": "^4.0.0",
7476
"sort-keys": "^3.0.0"
7577
},

src/index.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const debug = require('debug')
77
const Big = require('bignumber.js')
88
const errcode = require('err-code')
99
const migrator = require('ipfs-repo-migrations')
10+
const prettyBytes = require('pretty-bytes')
11+
const bytes = require('bytes')
1012

1113
const constants = require('./constants')
1214
const backends = require('./backends')
@@ -258,19 +260,22 @@ class IpfsRepo {
258260
getSize(this.datastore),
259261
getSize(this.keys)
260262
])
261-
let size = blocks.size
263+
const size = blocks.size
262264
.plus(datastore)
263265
.plus(keys)
264266

265-
if (options.human) {
266-
size = size.div(1048576)
267-
}
268267
return {
269268
repoPath: this.path,
270-
storageMax: storageMax,
269+
storageMax: options.human
270+
? prettyBytes(storageMax.toNumber()).toUpperCase()
271+
: storageMax,
271272
version: version,
272-
numObjects: blocks.count,
273-
repoSize: size
273+
numObjects: options.human
274+
? blocks.count.toNumber()
275+
: blocks.count,
276+
repoSize: options.human
277+
? prettyBytes(size.toNumber()).toUpperCase()
278+
: size
274279
}
275280
}
276281

@@ -308,7 +313,7 @@ class IpfsRepo {
308313
async _storageMaxStat () {
309314
try {
310315
const max = await this.config.get('Datastore.StorageMax')
311-
return new Big(max)
316+
return new Big(bytes(max))
312317
} catch (err) {
313318
return new Big(noLimit)
314319
}

test/repo-test.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const path = require('path')
88
const IPFSRepo = require('../')
99
const Errors = require('../src/errors')
1010
const os = require('os')
11+
const bytes = require('bytes')
1112

1213
module.exports = (repo) => {
1314
describe('IPFS Repo Tests', () => {
@@ -247,14 +248,17 @@ module.exports = (repo) => {
247248
})
248249

249250
it('should return the max storage stat when set', async () => {
251+
const maxStorage = '1GB'
252+
250253
otherRepo = new IPFSRepo(path.join(os.tmpdir(), 'repo-' + Date.now()))
251254
await otherRepo.init({})
252255
await otherRepo.open()
253-
await otherRepo.config.set('Datastore.StorageMax', 100)
256+
await otherRepo.config.set('Datastore.StorageMax', maxStorage)
254257

255258
const stat = await otherRepo.stat({})
256259

257-
expect(stat.storageMax.toNumber()).to.equal(100)
260+
expect(stat).to.have.property('storageMax')
261+
expect(stat.storageMax.toNumber()).to.equal(bytes(maxStorage))
258262
})
259263

260264
it('should throw unexpected errors when closing', async () => {

test/stat-test.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ chai.use(require('dirty-chai'))
66
const expect = chai.expect
77
const Block = require('ipfs-block')
88
const CID = require('cids')
9+
const prettyBytes = require('pretty-bytes')
910

1011
module.exports = (repo) => {
1112
describe('stat', () => {
@@ -33,18 +34,23 @@ module.exports = (repo) => {
3334
})
3435

3536
it('get human stats', async () => {
36-
const stats = await repo.stat({ human: true })
37-
expect(stats).to.exist()
38-
expect(stats).to.have.property('numObjects')
39-
expect(stats).to.have.property('version')
40-
expect(stats).to.have.property('repoPath')
41-
expect(stats).to.have.property('repoSize')
42-
expect(stats).to.have.property('storageMax')
37+
const { repoSize, storageMax } = await repo.stat()
4338

44-
expect(stats.numObjects > '0').to.eql(true)
45-
expect(stats.version > '0').to.eql(true)
46-
expect(stats.repoSize > '0').to.eql(true)
47-
expect(stats.storageMax > '0').to.eql(true)
39+
const humanizedRepoSize = prettyBytes(repoSize.toNumber()).toUpperCase()
40+
const humanizedStorageMax = prettyBytes(storageMax.toNumber()).toUpperCase()
41+
42+
const humanizedStats = await repo.stat({ human: true })
43+
44+
expect(humanizedStats).to.exist()
45+
expect(humanizedStats).to.have.property('numObjects')
46+
expect(humanizedStats).to.have.property('version').and.be.above(0)
47+
expect(humanizedStats).to.have.property('repoPath')
48+
expect(humanizedStats).to.have.property('repoSize').that.equals(humanizedRepoSize)
49+
expect(humanizedStats).to.have.property('storageMax').that.equals(humanizedStorageMax)
50+
51+
expect(humanizedStats.numObjects > '0').to.eql(true)
52+
expect(humanizedStats.repoSize > '0').to.eql(true)
53+
expect(humanizedStats.storageMax > '0').to.eql(true)
4854
})
4955
})
5056
}

0 commit comments

Comments
 (0)