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

Commit 9665936

Browse files
committed
update
1 parent 73abc21 commit 9665936

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

src/cli/commands/stats/bw.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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: ''
26+
}
27+
},
28+
29+
handler (argv) {
30+
argv.ipfs.stats.bw({
31+
peer: argv.peer,
32+
proto: argv.proto,
33+
poll: argv.poll,
34+
interval: argv.interval
35+
}, (err, stats) => {
36+
if (err) {
37+
throw err
38+
}
39+
40+
print(`bandwidth status
41+
total in: ${stats.totalIn}B
42+
total out: ${stats.totalOut}B
43+
rate in: ${stats.rateIn}B/s
44+
rate out: ${stats.rateOut}B/s`)
45+
})
46+
}
47+
}

src/core/components/stats.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
'use strict'
22

3+
const promisify = require('promisify-es6')
4+
35
module.exports = function stats (self) {
46
return {
57
bitswap: require('./bitswap')(self).stat,
6-
repo: require('./repo')(self).stat
8+
9+
repo: require('./repo')(self).stat,
10+
11+
bw: promisify((options, callback) => {
12+
if (typeof options === 'function') {
13+
callback = options
14+
options = {}
15+
}
16+
17+
let stats
18+
19+
if (options.peer) {
20+
// TODO: stats for a specific peer
21+
} else if (options.proto) {
22+
// TODO: stats for a specific proto
23+
} else {
24+
const stat = self._bitswap.stat()
25+
const snapshot = stat.snapshot
26+
const movingAverages = stat.movingAverages
27+
28+
stats = {
29+
totalIn: snapshot.dataReceived,
30+
totalOut: snapshot.dataSent,
31+
rateIn: movingAverages.dataReceived[60000].movingAverage() / 60, // Bytes per second
32+
rateOut: movingAverages.dataSent[60000].movingAverage() / 60 // Bytes per second
33+
}
34+
}
35+
36+
callback(null, stats)
37+
})
738
}
839
}

src/http/api/resources/stats.js

+32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
'use strict'
22

3+
const Readable = require('readable-stream').Readable
4+
35
exports = module.exports
46

57
exports.bitswap = require('./bitswap').stat
8+
69
exports.repo = require('./repo').stat
10+
11+
exports.bw = (request, reply) => {
12+
const ipfs = request.server.app.ipfs
13+
const human = request.query.human === 'true'
14+
15+
const options = {
16+
peer: request.query.peer,
17+
proto: request.query.proto,
18+
poll: request.query.poll === 'true',
19+
interval: request.query.interval
20+
}
21+
22+
ipfs.stats.bw(options, (err, stat) => {
23+
if (err) {
24+
return reply({
25+
Message: err.toString(),
26+
Code: 0
27+
}).code(500)
28+
}
29+
30+
reply({
31+
TotalIn: stat.totalIn,
32+
TotalOut: stat.totalOut,
33+
RateIn: stat.rateIn,
34+
RateOut: stat.rateOut,
35+
})
36+
})
37+
}
38+

src/http/api/routes/stats.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ module.exports = (server) => {
2121
}
2222
})
2323

24-
/* api.route({
24+
api.route({
2525
method: '*',
2626
path: '/api/v0/stats/bw',
2727
config: {
28+
payload: {
29+
output: 'stream',
30+
parse: false
31+
},
2832
handler: resources.stats.bw
2933
}
30-
}) */
34+
})
3135
}

0 commit comments

Comments
 (0)