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

feat(breaking change): use stream on stats.bw #686

Merged
merged 5 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"async": "^2.6.0",
"big.js": "^5.0.3",
"bs58": "^4.0.1",
"cids": "~0.5.2",
"concat-stream": "^1.6.0",
Expand Down Expand Up @@ -68,8 +69,8 @@
"eslint-plugin-react": "^7.5.1",
"go-ipfs-dep": "^0.4.13",
"gulp": "^3.9.1",
"interface-ipfs-core": "~0.43.0",
"hapi": "^16.6.2",
"interface-ipfs-core": "~0.43.0",
"ipfsd-ctl": "~0.27.0",
"pre-commit": "^1.2.2",
"socket.io": "^2.0.4",
Expand Down
17 changes: 9 additions & 8 deletions src/bitswap/stat.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
'use strict'

const promisify = require('promisify-es6')
const Big = require('big.js')

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

Expand Down
7 changes: 4 additions & 3 deletions src/repo/stat.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict'

const promisify = require('promisify-es6')
const Big = require('big.js')

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

Expand Down
17 changes: 9 additions & 8 deletions src/stats/bitswap.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
'use strict'

const promisify = require('promisify-es6')
const Big = require('big.js')

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

Expand Down
36 changes: 36 additions & 0 deletions src/stats/bw-pull-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const toPull = require('stream-to-pull-stream')
const pull = require('pull-stream')
const transformChunk = require('./bw-util')
const deferred = require('pull-defer')

const transform = () => (read) => (abort, cb) => {
read(abort, (err, data) => {
console.log(data)
if (err) return cb(err)
cb(null, transformChunk(data))
})
}

module.exports = (send) => {
return (hash, opts) => {
opts = opts || {}

const p = deferred.through()

send({
path: 'stats/bw',
qs: opts
}, (err, stream) => {
if (err) {
return p.end(err)
}

pull(toPull(stream), p)
Copy link
Contributor

@dignifiedquire dignifiedquire Feb 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be simply

pull(pull.map(transformChunk), p)

if I understand it correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :D

p.resolve(transform)
})

return p
}
}
31 changes: 31 additions & 0 deletions src/stats/bw-readable-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const Stream = require('readable-stream')
const pump = require('pump')
const transformChunk = require('./bw-util')

module.exports = (send) => {
return (hash, opts) => {
opts = opts || {}

const pt = new Stream.Transform({
objectMode: true,
transform (chunk, encoding, cb) {
cb(null, transformChunk(chunk))
}
})

send({
path: 'stats/bw',
qs: opts
}, (err, stream) => {
if (err) {
return pt.destroy(err)
}

pump(stream, pt)
})

return pt
}
}
12 changes: 12 additions & 0 deletions src/stats/bw-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const Big = require('big.js')

module.exports = (chunk) => {
return {
totalIn: new Big(chunk.TotalIn),
totalOut: new Big(chunk.TotalOut),
rateIn: new Big(chunk.RateIn),
rateOut: new Big(chunk.RateOut)
}
}
12 changes: 4 additions & 8 deletions src/stats/bw.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

const promisify = require('promisify-es6')
const streamToValue = require('../utils/stream-to-value')
const transformChunk = require('./bw-util')

const transform = function (res, callback) {
streamToValue(res, (err, data) => {
const transform = (res, callback) => {
return 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
})
callback(null, transformChunk(data[0]))
})
}

Expand Down
2 changes: 2 additions & 0 deletions src/stats/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module.exports = (arg) => {
return {
bitswap: require('./bitswap')(send),
bw: require('./bw')(send),
bwReadableStream: require('./bw-readable-stream')(send),
bwPullStream: require('./bw-pull-stream')(send),
repo: require('./repo')(send)
}
}
7 changes: 4 additions & 3 deletions src/stats/repo.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict'

const promisify = require('promisify-es6')
const Big = require('big.js')

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

Expand Down