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

WIP feat: human option bitswap stat #1162

Closed
wants to merge 4 commits into from
Closed
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"browser-process-platform": "~0.1.1",
"cross-env": "^6.0.0",
"go-ipfs-dep": "^0.4.22",
"interface-ipfs-core": "^0.119.0",
"interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#test/add-human-option-test-bitswap-stat",
"ipfsd-ctl": "^0.47.1",
"nock": "^11.4.0",
"stream-equal": "^1.1.1"
Expand Down
37 changes: 27 additions & 10 deletions src/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,44 @@ module.exports = configure(({ ky }) => {
return async (options) => {
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
if (options.human) searchParams.set('human', options.human)

const res = await ky.get('bitswap/stat', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams: options.searchParams
searchParams
}).json()

return toCoreInterface(res)
return toCoreInterface(res, options)
}
})

function toCoreInterface (res) {
function toCoreInterface (res, { human }) {
return {
provideBufLen: res.ProvideBufLen,
provideBufLen: human
? res.ProvideBufLen
: new Big(res.ProvideBufLen),
wantlist: res.Wantlist || [],
peers: res.Peers || [],
blocksReceived: new Big(res.BlocksReceived),
dataReceived: new Big(res.DataReceived),
blocksSent: new Big(res.BlocksSent),
dataSent: new Big(res.DataSent),
dupBlksReceived: new Big(res.DupBlksReceived),
dupDataReceived: new Big(res.DupDataReceived)
blocksReceived: human
? res.BlocksReceived
: new Big(res.BlocksReceived),
dataReceived: human
? res.DataReceived
: new Big(res.DataReceived),
blocksSent: human
? res.BlocksSent
: new Big(res.BlocksSent),
dataSent: human
? res.DataSent
: new Big(res.DataSent),
dupBlksReceived: human
? res.DupBlksReceived
: new Big(res.DupBlksReceived),
dupDataReceived: human
? res.DupDataReceived
: new Big(res.DupDataReceived)
}
}
32 changes: 23 additions & 9 deletions src/stats/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@
const promisify = require('promisify-es6')
const Big = require('bignumber.js')

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

Expand All @@ -27,6 +41,6 @@ module.exports = (send) => {
send.andTransform({
path: 'stats/bitswap',
qs: opts
}, transform, callback)
}, transform(opts), callback)
})
}
13 changes: 12 additions & 1 deletion test/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ describe('interface-ipfs-core tests', () => {
name: 'should not get bitswap stats when offline',
reason: 'FIXME go-ipfs returns an error https://github.com/ipfs/go-ipfs/issues/4078'
},
{
name: 'should get human readable bitswap stats',
reason: 'FIXME go-ipfs only supports human option for the cli'
},
// bitswap.wantlist
{
name: 'should not get the wantlist when offline',
Expand Down Expand Up @@ -249,7 +253,14 @@ describe('interface-ipfs-core tests', () => {

tests.repo(defaultCommonFactory)

tests.stats(defaultCommonFactory)
tests.stats(defaultCommonFactory, {
skip: [
{
name: 'should get human readable bitswap stats',
reason: 'FIXME go-ipfs only supports human option for the cli'
}
]
})

tests.swarm(CommonFactory.createAsync())
})