Skip to content
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

fix: human readable option #213

Merged
merged 3 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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,6 +57,7 @@
"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",
Expand All @@ -70,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"
},
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 prettyBytes = require('pretty-bytes')
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
? prettyBytes(storageMax.toNumber()).toUpperCase()
: storageMax,
version: version,
numObjects: blocks.count,
repoSize: size
numObjects: options.human
? blocks.count.toNumber()
: blocks.count,
repoSize: options.human
? prettyBytes(size.toNumber()).toUpperCase()
: 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))
PedroMiguelSS marked this conversation as resolved.
Show resolved Hide resolved
} 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
28 changes: 17 additions & 11 deletions test/stat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -33,18 +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')
expect(stats).to.have.property('repoPath')
expect(stats).to.have.property('repoSize')
expect(stats).to.have.property('storageMax')
const { repoSize, storageMax } = await repo.stat()

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)
const humanizedRepoSize = prettyBytes(repoSize.toNumber()).toUpperCase()
const humanizedStorageMax = prettyBytes(storageMax.toNumber()).toUpperCase()

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)
})
})
}