Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

fix: normalize stats fields #669

Merged
merged 1 commit into from
Jan 18, 2018
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
18 changes: 16 additions & 2 deletions src/stats/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@

const promisify = require('promisify-es6')

const transform = function (res, callback) {
callback(null, {
provideBufLen: res.ProvideBufLen,
wantlist: res.Wantlist,
peers: res.Peers,
blocksReceived: res.BlocksReceived,
dataReceived: res.DataReceived,
blocksSent: res.BlocksSent,
dataSent: res.DataSent,
dupBlksReceived: res.DupBlksReceived,
dupDataReceived: res.DupDataReceived
})
}

module.exports = (send) => {
return promisify((opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}

send({
send.andTransform({
path: 'stats/bitswap',
qs: opts
}, callback)
}, transform, callback)
})
}
25 changes: 16 additions & 9 deletions src/stats/bw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
const promisify = require('promisify-es6')
const streamToValue = require('../utils/stream-to-value')

const transform = function (res, callback) {
streamToValue(res, (err, data) => {
if (err) {
return callback(err)
}

callback(null, {
totalIn: data[0].TotalIn,
totalOut: data[0].TotalOut,
rateIn: data[0].RateIn,
rateOut: data[0].RateOut
})
})
}

module.exports = (send) => {
return promisify((opts, callback) => {
if (typeof (opts) === 'function') {
Expand All @@ -13,14 +28,6 @@ module.exports = (send) => {
send.andTransform({
path: 'stats/bw',
qs: opts
}, streamToValue, (err, stats) => {
if (err) {
return callback(err)
}

// streamToValue returns an array and we're only
// interested in returning the object itself.
callback(err, stats[0])
})
}, transform, callback)
})
}
14 changes: 12 additions & 2 deletions src/stats/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@

const promisify = require('promisify-es6')

const transform = function (res, callback) {
callback(null, {
numObjects: res.NumObjects,
repoSize: res.RepoSize,
repoPath: res.RepoPath,
version: res.Version,
storageMax: res.StorageMax
})
}

module.exports = (send) => {
return promisify((opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}

send({
send.andTransform({
path: 'stats/repo',
qs: opts
}, callback)
}, transform, callback)
})
}
1 change: 0 additions & 1 deletion test/fixtures/test-folder/hello-link

This file was deleted.

1 change: 1 addition & 0 deletions test/fixtures/test-folder/hello-link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
files/hello.txt
72 changes: 36 additions & 36 deletions test/stats.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ describe('stats', function () {
ipfs.stats.bitswap((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.a.property('ProvideBufLen')
expect(res).to.have.a.property('Wantlist')
expect(res).to.have.a.property('Peers')
expect(res).to.have.a.property('BlocksReceived')
expect(res).to.have.a.property('DataReceived')
expect(res).to.have.a.property('BlocksSent')
expect(res).to.have.a.property('DataSent')
expect(res).to.have.a.property('DupBlksReceived')
expect(res).to.have.a.property('DupDataReceived')
expect(res).to.have.a.property('provideBufLen')
expect(res).to.have.a.property('wantlist')
expect(res).to.have.a.property('peers')
expect(res).to.have.a.property('blocksReceived')
expect(res).to.have.a.property('dataReceived')
expect(res).to.have.a.property('blocksSent')
expect(res).to.have.a.property('dataSent')
expect(res).to.have.a.property('dupBlksReceived')
expect(res).to.have.a.property('dupDataReceived')
done()
})
})
Expand All @@ -48,10 +48,10 @@ describe('stats', function () {
ipfs.stats.bw((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.a.property('TotalIn')
expect(res).to.have.a.property('TotalOut')
expect(res).to.have.a.property('RateIn')
expect(res).to.have.a.property('RateOut')
expect(res).to.have.a.property('totalIn')
expect(res).to.have.a.property('totalOut')
expect(res).to.have.a.property('rateIn')
expect(res).to.have.a.property('rateOut')
done()
})
})
Expand All @@ -60,11 +60,11 @@ describe('stats', function () {
ipfs.stats.repo((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.a.property('NumObjects')
expect(res).to.have.a.property('RepoSize')
expect(res).to.have.a.property('RepoPath')
expect(res).to.have.a.property('Version')
expect(res).to.have.a.property('StorageMax')
expect(res).to.have.a.property('numObjects')
expect(res).to.have.a.property('repoSize')
expect(res).to.have.a.property('repoPath')
expect(res).to.have.a.property('version')
expect(res).to.have.a.property('storageMax')
done()
})
})
Expand All @@ -75,38 +75,38 @@ describe('stats', function () {
return ipfs.stats.bw()
.then((res) => {
expect(res).to.exist()
expect(res).to.have.a.property('TotalIn')
expect(res).to.have.a.property('TotalOut')
expect(res).to.have.a.property('RateIn')
expect(res).to.have.a.property('RateOut')
expect(res).to.have.a.property('totalIn')
expect(res).to.have.a.property('totalOut')
expect(res).to.have.a.property('rateIn')
expect(res).to.have.a.property('rateOut')
})
})

it('.stats.repo', () => {
return ipfs.stats.repo()
.then((res) => {
expect(res).to.exist()
expect(res).to.have.a.property('NumObjects')
expect(res).to.have.a.property('RepoSize')
expect(res).to.have.a.property('RepoPath')
expect(res).to.have.a.property('Version')
expect(res).to.have.a.property('StorageMax')
expect(res).to.have.a.property('numObjects')
expect(res).to.have.a.property('repoSize')
expect(res).to.have.a.property('repoPath')
expect(res).to.have.a.property('version')
expect(res).to.have.a.property('storageMax')
})
})

it('.stats.bitswap', () => {
return ipfs.stats.bitswap()
.then((res) => {
expect(res).to.exist()
expect(res).to.have.a.property('ProvideBufLen')
expect(res).to.have.a.property('Wantlist')
expect(res).to.have.a.property('Peers')
expect(res).to.have.a.property('BlocksReceived')
expect(res).to.have.a.property('DataReceived')
expect(res).to.have.a.property('BlocksSent')
expect(res).to.have.a.property('DataSent')
expect(res).to.have.a.property('DupBlksReceived')
expect(res).to.have.a.property('DupDataReceived')
expect(res).to.have.a.property('provideBufLen')
expect(res).to.have.a.property('wantlist')
expect(res).to.have.a.property('peers')
expect(res).to.have.a.property('blocksReceived')
expect(res).to.have.a.property('dataReceived')
expect(res).to.have.a.property('blocksSent')
expect(res).to.have.a.property('dataSent')
expect(res).to.have.a.property('dupBlksReceived')
expect(res).to.have.a.property('dupDataReceived')
})
})
})
Expand Down