Skip to content

fix: human readable option #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 13 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 6 additions & 2 deletions test/repo-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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 () => {
Expand Down
7 changes: 3 additions & 4 deletions test/stat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down