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

Commit 3489ded

Browse files
committed
feat: .stats.bw*
1 parent f7cf10b commit 3489ded

File tree

7 files changed

+180
-4
lines changed

7 files changed

+180
-4
lines changed

bw

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"TotalIn":"0","TotalOut":"0","RateIn":"0","RateOut":"0"}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"form-data": "^2.3.2",
7575
"go-ipfs-dep": "^0.4.13",
7676
"hat": "0.0.3",
77-
"interface-ipfs-core": "~0.52.0",
77+
"interface-ipfs-core": "ipfs/interface-ipfs-core#792464e",
7878
"ipfsd-ctl": "~0.29.1",
7979
"left-pad": "^1.2.0",
8080
"lodash": "^4.17.5",
@@ -122,7 +122,7 @@
122122
"joi": "^13.1.2",
123123
"joi-browser": "^13.0.1",
124124
"joi-multiaddr": "^1.0.1",
125-
"libp2p": "~0.18.0",
125+
"libp2p": "~0.19.0",
126126
"libp2p-circuit": "~0.1.4",
127127
"libp2p-floodsub": "~0.14.1",
128128
"libp2p-kad-dht": "~0.8.0",

src/cli/commands/stats/bw.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
const pull = require('pull-stream')
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.bwPullStream({
31+
peer: argv.peer,
32+
proto: argv.proto,
33+
poll: argv.poll,
34+
interval: argv.interval
35+
})
36+
37+
pull(
38+
stream,
39+
pull.drain((chunk) => {
40+
console.log(`bandwidth status
41+
total in: ${chunk.totalIn}B
42+
total out: ${chunk.totalOut}B
43+
rate in: ${chunk.rateIn}B/s
44+
rate out: ${chunk.rateOut}B/s`)
45+
})
46+
)
47+
}
48+
}

src/core/components/stats.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,88 @@
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+
return new Promise((resolve, reject) => {
11+
let stats
12+
13+
if (opts.peer) {
14+
stats = self._libp2pNode.stats.forPeer(opts.peer)
15+
} else if (opts.proto) {
16+
stats = self._libp2pNode.stats.forProtocol(opts.proto)
17+
} else {
18+
stats = self._libp2pNode.stats.global
19+
}
20+
21+
if (!stats) {
22+
resolve({
23+
totalIn: new Big(0),
24+
totalOut: new Big(0),
25+
rateIn: new Big(0),
26+
rateOut: new Big(0)
27+
})
28+
return
29+
}
30+
31+
resolve({
32+
totalIn: stats.snapshot.dataReceived,
33+
totalOut: stats.snapshot.dataSent,
34+
rateIn: new Big(stats.movingAverages.dataReceived['60000'].movingAverage() / 60),
35+
rateOut: new Big(stats.movingAverages.dataSent['60000'].movingAverage() / 60)
36+
})
37+
})
38+
}
39+
340
module.exports = function stats (self) {
41+
const _bwPullStream = (opts) => {
42+
opts = opts || {}
43+
let interval = null
44+
let stream = Pushable(true, () => {
45+
if (interval) {
46+
clearInterval(interval)
47+
}
48+
})
49+
50+
if (opts.poll) {
51+
human(opts.interval || '1s', (err, value) => {
52+
if (err) throw err
53+
54+
interval = setInterval(() => {
55+
bandwidthStats(self, opts)
56+
.then((stats) => stream.push(stats))
57+
.catch((err) => stream.end(err))
58+
}, value)
59+
})
60+
} else {
61+
bandwidthStats(self, opts)
62+
.then((stats) => {
63+
stream.push(stats)
64+
stream.end()
65+
})
66+
.catch((err) => stream.end(err))
67+
}
68+
69+
return stream.source
70+
}
71+
472
return {
573
bitswap: require('./bitswap')(self).stat,
6-
repo: require('./repo')(self).stat
74+
repo: require('./repo')(self).stat,
75+
bw: promisify((opts, callback) => {
76+
if (typeof opts === 'function') {
77+
callback = opts
78+
opts = {}
79+
}
80+
81+
bandwidthStats(self, opts)
82+
.then((stats) => callback(null, stats))
83+
.catch((err) => callback(err))
84+
}),
85+
bwReadableStream: (opts) => toStream.source(_bwPullStream(opts)),
86+
bwPullStream: _bwPullStream
787
}
888
}

src/http/api/resources/stats.js

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

3+
const { Transform } = 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+
writableObjectMode: 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(output)
44+
.header('content-type', 'application/json')
45+
.header('x-chunked-output', '1')
46+
}

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
}

test/cli/commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const expect = require('chai').expect
55
const runOnAndOff = require('../utils/on-and-off')
66

7-
const commandCount = 72
7+
const commandCount = 73
88
describe('commands', () => runOnAndOff((thing) => {
99
let ipfs
1010

0 commit comments

Comments
 (0)