Skip to content

Commit b826e5b

Browse files
committed
add stats
1 parent 604a13f commit b826e5b

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"dependencies": {
5757
"async": "^2.6.0",
5858
"base32.js": "~0.1.0",
59+
"big.js": "^5.0.3",
5960
"cids": "~0.5.2",
6061
"datastore-core": "~0.4.0",
6162
"datastore-fs": "~0.4.2",

src/index.js

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const each = require('async/each')
77
const assert = require('assert')
88
const path = require('path')
99
const debug = require('debug')
10+
const Big = require('big.js')
11+
const pull = require('pull-stream')
1012

1113
const backends = require('./backends')
1214
const version = require('./version')
@@ -17,6 +19,8 @@ const defaultOptions = require('./default-options')
1719

1820
const log = debug('repo')
1921

22+
const noLimit = Number.MAX_SAFE_INTEGER
23+
2024
const lockers = {
2125
memory: require('./lock-memory'),
2226
fs: require('./lock')
@@ -201,32 +205,73 @@ class IpfsRepo {
201205
this.version.exists(callback)
202206
}
203207

208+
/**
209+
* Get repo status.
210+
*
211+
* @param {function(Error, Object)} callback
212+
* @return {void}
213+
*/
204214
stat (callback) {
205215
waterfall([
206-
(cb) => cb(null, {repoPath: this.path}),
207-
(stats, cb) => {
208-
this.version.get((err, version) => {
209-
stats.version = version
210-
cb(err, stats)
216+
(cb) => cb(null, {
217+
repoPath: this.path,
218+
repoSize: new Big(0),
219+
storageMax: new Big(noLimit)
220+
}),
221+
(stats, cb) => this.version.get((err, version) => {
222+
stats.version = version
223+
cb(err, stats)
224+
}),
225+
(stats, cb) => this.blocks.query({}, (err, list) => {
226+
list = list || []
227+
stats.numObjects = new Big(list.length)
228+
229+
list.forEach(block => {
230+
stats.repoSize = stats.repoSize
231+
.plus(block.value.byteLength)
232+
.plus(block.key._buf.byteLength)
211233
})
212-
},
213-
(stats, cb) => {
214-
this.blocks.query({ keysOnly: true }, (err, list) => {
215-
list = list || []
216-
stats.numObjects = list.length
217-
cb(err, stats)
218-
})
219-
},
220-
(stats, cb) => {
221-
// TODO:
222-
stats.repoSize = null
223-
stats.storageMax = null
234+
235+
cb(err, stats)
236+
}),
237+
(stats, cb) => getSize(this.datastore, (err, size) => {
238+
stats.repoSize = stats.repoSize.plus(size)
239+
cb(err, stats)
240+
}),
241+
(stats, cb) => getSize(this.keys, (err, size) => {
242+
stats.repoSize = stats.repoSize.plus(size)
243+
cb(err, stats)
244+
}),
245+
(stats, cb) => this.config.get('Datastore.StorageMax', (err, max) => {
246+
if (!err) {
247+
stats.storageMax = new Big(max)
248+
}
249+
224250
cb(null, stats)
225-
}
251+
})
226252
], callback)
227253
}
228254
}
229255

256+
function getSize (queryFn, callback) {
257+
pull(
258+
queryFn.query({}),
259+
pull.collect((err, list) => {
260+
if (err) return callback(err, 0)
261+
262+
let val = new Big(0)
263+
264+
list.forEach(block => {
265+
val = val
266+
.plus(block.value.byteLength)
267+
.plus(block.key._buf.byteLength)
268+
})
269+
270+
callback(null, val)
271+
})
272+
)
273+
}
274+
230275
module.exports = IpfsRepo
231276
module.exports.repoVersion = repoVersion
232277

0 commit comments

Comments
 (0)