This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: .stats.bw* - Bandwidth Stats #1230
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"TotalIn":"0","TotalOut":"0","RateIn":"0","RateOut":"0"} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict' | ||
|
||
const pull = require('pull-stream') | ||
|
||
module.exports = { | ||
command: 'bw', | ||
|
||
describe: 'Get bandwidth information.', | ||
|
||
builder: { | ||
peer: { | ||
type: 'string', | ||
default: '' | ||
}, | ||
proto: { | ||
type: 'string', | ||
default: '' | ||
}, | ||
poll: { | ||
type: 'boolean', | ||
default: false | ||
}, | ||
interval: { | ||
type: 'string', | ||
default: '1s' | ||
} | ||
}, | ||
|
||
handler (argv) { | ||
const stream = argv.ipfs.stats.bwPullStream({ | ||
peer: argv.peer, | ||
proto: argv.proto, | ||
poll: argv.poll, | ||
interval: argv.interval | ||
}) | ||
|
||
pull( | ||
stream, | ||
pull.drain((chunk) => { | ||
console.log(`bandwidth status | ||
total in: ${chunk.totalIn}B | ||
total out: ${chunk.totalOut}B | ||
rate in: ${chunk.rateIn}B/s | ||
rate out: ${chunk.rateOut}B/s`) | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,88 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const Big = require('big.js') | ||
const Pushable = require('pull-pushable') | ||
const human = require('human-to-milliseconds') | ||
const toStream = require('pull-stream-to-stream') | ||
|
||
function bandwidthStats (self, opts) { | ||
return new Promise((resolve, reject) => { | ||
let stats | ||
|
||
if (opts.peer) { | ||
stats = self._libp2pNode.stats.forPeer(opts.peer) | ||
} else if (opts.proto) { | ||
stats = self._libp2pNode.stats.forProtocol(opts.proto) | ||
} else { | ||
stats = self._libp2pNode.stats.global | ||
} | ||
|
||
if (!stats) { | ||
resolve({ | ||
totalIn: new Big(0), | ||
totalOut: new Big(0), | ||
rateIn: new Big(0), | ||
rateOut: new Big(0) | ||
}) | ||
return | ||
} | ||
|
||
resolve({ | ||
totalIn: stats.snapshot.dataReceived, | ||
totalOut: stats.snapshot.dataSent, | ||
rateIn: new Big(stats.movingAverages.dataReceived['60000'].movingAverage() / 60), | ||
rateOut: new Big(stats.movingAverages.dataSent['60000'].movingAverage() / 60) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = function stats (self) { | ||
const _bwPullStream = (opts) => { | ||
opts = opts || {} | ||
let interval = null | ||
let stream = Pushable(true, () => { | ||
if (interval) { | ||
clearInterval(interval) | ||
} | ||
}) | ||
|
||
if (opts.poll) { | ||
human(opts.interval || '1s', (err, value) => { | ||
if (err) throw err | ||
|
||
interval = setInterval(() => { | ||
bandwidthStats(self, opts) | ||
.then((stats) => stream.push(stats)) | ||
.catch((err) => stream.end(err)) | ||
}, value) | ||
}) | ||
} else { | ||
bandwidthStats(self, opts) | ||
.then((stats) => { | ||
stream.push(stats) | ||
stream.end() | ||
}) | ||
.catch((err) => stream.end(err)) | ||
} | ||
|
||
return stream.source | ||
} | ||
|
||
return { | ||
bitswap: require('./bitswap')(self).stat, | ||
repo: require('./repo')(self).stat | ||
repo: require('./repo')(self).stat, | ||
bw: promisify((opts, callback) => { | ||
if (typeof opts === 'function') { | ||
callback = opts | ||
opts = {} | ||
} | ||
|
||
bandwidthStats(self, opts) | ||
.then((stats) => callback(null, stats)) | ||
.catch((err) => callback(err)) | ||
}), | ||
bwReadableStream: (opts) => toStream.source(_bwPullStream(opts)), | ||
bwPullStream: _bwPullStream | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,46 @@ | ||
'use strict' | ||
|
||
const { Transform } = require('readable-stream') | ||
|
||
const transformBandwidth = (stat) => { | ||
return { | ||
TotalIn: stat.totalIn, | ||
TotalOut: stat.totalOut, | ||
RateIn: stat.rateIn, | ||
RateOut: stat.rateOut | ||
} | ||
} | ||
|
||
exports = module.exports | ||
|
||
exports.bitswap = require('./bitswap').stat | ||
|
||
exports.repo = require('./repo').stat | ||
|
||
exports.bw = (request, reply) => { | ||
const ipfs = request.server.app.ipfs | ||
const options = { | ||
peer: request.query.peer, | ||
proto: request.query.proto, | ||
poll: request.query.poll === 'true', | ||
interval: request.query.interval || '1s' | ||
} | ||
|
||
const res = ipfs.stats.bwReadableStream(options) | ||
const output = new Transform({ | ||
writableObjectMode: true, | ||
transform (chunk, encoding, cb) { | ||
this.push(JSON.stringify(transformBandwidth(chunk)) + '\n') | ||
cb() | ||
} | ||
}) | ||
|
||
request.on('disconnect', () => { | ||
res.destroy() | ||
}) | ||
|
||
res.pipe(output) | ||
reply(output) | ||
.header('content-type', 'application/json') | ||
.header('x-chunked-output', '1') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hacdias where does this file come from?