Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: .stats.bw*
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 26, 2018
1 parent e4d2a15 commit 509d6ee
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -106,9 +106,9 @@
"hapi": "^16.6.2",
"hapi-set-header": "^1.0.2",
"hoek": "^5.0.3",
"human-to-milliseconds": "^1.0.0",
"ipfs-api": "^18.0.0",
"ipfs-bitswap": "~0.19.0",
"human-to-milliseconds": "^1.0.0",
"ipfs-block": "~0.6.1",
"ipfs-block-service": "~0.13.0",
"ipfs-multipart": "~0.1.0",
Expand Down Expand Up @@ -153,7 +153,7 @@
"pull-file": "^1.1.0",
"pull-ndjson": "^0.1.1",
"pull-paramap": "^1.2.2",
"pull-pushable": "^2.1.2",
"pull-pushable": "^2.2.0",
"pull-sort": "^1.0.1",
"pull-stream": "^3.6.1",
"pull-stream-to-stream": "^1.3.4",
Expand Down
45 changes: 45 additions & 0 deletions src/cli/commands/stats/bw.js
@@ -0,0 +1,45 @@
'use strict'

const print = require('../../utils').print

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.bwReadableStream({
peer: argv.peer,
proto: argv.proto,
poll: argv.poll,
interval: argv.interval
})

stream.once('data', function (stats) {
print(`bandwidth status
total in: ${stats.totalIn}B
total out: ${stats.totalOut}B
rate in: ${stats.rateIn}B/s
rate out: ${stats.rateOut}B/s`)
})
}
}
57 changes: 56 additions & 1 deletion src/core/components/stats.js
@@ -1,8 +1,63 @@
'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) {
// TODO: get the true stats :)
return new Promise((resolve, reject) => {
resolve({
totalIn: new Big(0),
totalOut: new Big(0),
rateIn: new Big(0),
rateOut: new Big(0)
})
})
}

module.exports = function stats (self) {
const _bwPullStream = (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) => {
bandwidthStats(self, opts)
.then((stats) => callback(null, stats))
.catch((err) => callback(err))
}),
bwReadableStream: (opts) => toStream.source(_bwPullStream(opts)),
bwPullStream: _bwPullStream
}
}
38 changes: 38 additions & 0 deletions src/http/api/resources/stats.js
@@ -1,7 +1,45 @@
'use strict'

const { Transform, Readable } = 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({
objectMode: true,
transform (chunk, encoding, cb) {
this.push(JSON.stringify(transformBandwidth(chunk)) + '\n')
cb()
}
})

request.on('disconnect', () => {
res.destroy()
})

res.pipe(output)
reply(new Readable().wrap(output))
.header('x-chunked-output', '1')
}
8 changes: 8 additions & 0 deletions src/http/api/routes/stats.js
Expand Up @@ -20,4 +20,12 @@ module.exports = (server) => {
handler: resources.stats.repo
}
})

api.route({
method: '*',
path: '/api/v0/stats/bw',
config: {
handler: resources.stats.bw
}
})
}

0 comments on commit 509d6ee

Please sign in to comment.