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

Commit ecb04c0

Browse files
committed
feat: .stats.bw*
1 parent 7f69628 commit ecb04c0

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

src/cli/commands/stats/bw.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
const print = require('../../utils').print
4+
5+
module.exports = {
6+
command: 'bw',
7+
8+
describe: 'Get bandwidth information.',
9+
10+
builder: {
11+
peer: {
12+
type: 'string',
13+
default: ''
14+
},
15+
proto: {
16+
type: 'string',
17+
default: ''
18+
},
19+
poll: {
20+
type: 'boolean',
21+
default: false
22+
},
23+
interval: {
24+
type: 'string',
25+
default: '1s'
26+
}
27+
},
28+
29+
handler (argv) {
30+
const stream = argv.ipfs.stats.bwReadableStream({
31+
peer: argv.peer,
32+
proto: argv.proto,
33+
poll: argv.poll,
34+
interval: argv.interval
35+
})
36+
37+
stream.once('data', function (stats) {
38+
print(`bandwidth status
39+
total in: ${stats.totalIn}B
40+
total out: ${stats.totalOut}B
41+
rate in: ${stats.rateIn}B/s
42+
rate out: ${stats.rateOut}B/s`)
43+
})
44+
}
45+
}

src/core/components/stats.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,63 @@
11
'use strict'
22

3+
const promisify = require('promisify-es6')
4+
const Big = require('big.js')
5+
const Pushable = require('pull-pushable')
6+
const human = require('human-to-milliseconds')
7+
const toStream = require('pull-stream-to-stream')
8+
9+
function bandwidthStats (self, opts) {
10+
// TODO: get the true stats :)
11+
return new Promise((resolve, reject) => {
12+
resolve({
13+
totalIn: new Big(0),
14+
totalOut: new Big(0),
15+
rateIn: new Big(0),
16+
rateOut: new Big(0)
17+
})
18+
})
19+
}
20+
321
module.exports = function stats (self) {
22+
const _bwPullStream = (opts) => {
23+
let interval = null
24+
let stream = Pushable(true, () => {
25+
if (interval) {
26+
clearInterval(interval)
27+
}
28+
})
29+
30+
if (opts.poll) {
31+
human(opts.interval || '1s', (err, value) => {
32+
if (err) throw err
33+
34+
interval = setInterval(() => {
35+
bandwidthStats(self, opts)
36+
.then((stats) => stream.push(stats))
37+
.catch((err) => stream.end(err))
38+
}, value)
39+
})
40+
} else {
41+
bandwidthStats(self, opts)
42+
.then((stats) => {
43+
stream.push(stats)
44+
stream.end()
45+
})
46+
.catch((err) => stream.end(err))
47+
}
48+
49+
return stream.source
50+
}
51+
452
return {
553
bitswap: require('./bitswap')(self).stat,
6-
repo: require('./repo')(self).stat
54+
repo: require('./repo')(self).stat,
55+
bw: promisify((opts, callback) => {
56+
bandwidthStats(self, opts)
57+
.then((stats) => callback(null, stats))
58+
.catch((err) => callback(err))
59+
}),
60+
bwReadableStream: (opts) => toStream.source(_bwPullStream(opts)),
61+
bwPullStream: _bwPullStream
762
}
863
}

src/http/api/resources/stats.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
11
'use strict'
22

3+
const { Transform, Readable } = require('readable-stream')
4+
5+
const transformBandwidth = (stat) => {
6+
return {
7+
TotalIn: stat.totalIn,
8+
TotalOut: stat.totalOut,
9+
RateIn: stat.rateIn,
10+
RateOut: stat.rateOut
11+
}
12+
}
13+
314
exports = module.exports
415

516
exports.bitswap = require('./bitswap').stat
617

718
exports.repo = require('./repo').stat
19+
20+
exports.bw = (request, reply) => {
21+
const ipfs = request.server.app.ipfs
22+
const options = {
23+
peer: request.query.peer,
24+
proto: request.query.proto,
25+
poll: request.query.poll === 'true',
26+
interval: request.query.interval || '1s'
27+
}
28+
29+
const res = ipfs.stats.bwReadableStream(options)
30+
const output = new Transform({
31+
objectMode: true,
32+
transform (chunk, encoding, cb) {
33+
this.push(JSON.stringify(transformBandwidth(chunk)) + '\n')
34+
cb()
35+
}
36+
})
37+
38+
request.on('disconnect', () => {
39+
res.destroy()
40+
})
41+
42+
res.pipe(output)
43+
reply(new Readable().wrap(output))
44+
.header('x-chunked-output', '1')
45+
}

src/http/api/routes/stats.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ module.exports = (server) => {
2020
handler: resources.stats.repo
2121
}
2222
})
23+
24+
api.route({
25+
method: '*',
26+
path: '/api/v0/stats/bw',
27+
config: {
28+
handler: resources.stats.bw
29+
}
30+
})
2331
}

0 commit comments

Comments
 (0)