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

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jan 30, 2018
1 parent 73abc21 commit 9665936
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 3 deletions.
47 changes: 47 additions & 0 deletions src/cli/commands/stats/bw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'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: ''
}
},

handler (argv) {
argv.ipfs.stats.bw({
peer: argv.peer,
proto: argv.proto,
poll: argv.poll,
interval: argv.interval
}, (err, stats) => {
if (err) {
throw err
}

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`)
})
}
}
33 changes: 32 additions & 1 deletion src/core/components/stats.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
'use strict'

const promisify = require('promisify-es6')

module.exports = function stats (self) {
return {
bitswap: require('./bitswap')(self).stat,
repo: require('./repo')(self).stat

repo: require('./repo')(self).stat,

bw: promisify((options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

let stats

if (options.peer) {
// TODO: stats for a specific peer
} else if (options.proto) {
// TODO: stats for a specific proto
} else {
const stat = self._bitswap.stat()
const snapshot = stat.snapshot
const movingAverages = stat.movingAverages

stats = {
totalIn: snapshot.dataReceived,
totalOut: snapshot.dataSent,
rateIn: movingAverages.dataReceived[60000].movingAverage() / 60, // Bytes per second
rateOut: movingAverages.dataSent[60000].movingAverage() / 60 // Bytes per second
}
}

callback(null, stats)
})
}
}
32 changes: 32 additions & 0 deletions src/http/api/resources/stats.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
'use strict'

const Readable = require('readable-stream').Readable

exports = module.exports

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

exports.repo = require('./repo').stat

exports.bw = (request, reply) => {
const ipfs = request.server.app.ipfs
const human = request.query.human === 'true'

const options = {
peer: request.query.peer,
proto: request.query.proto,
poll: request.query.poll === 'true',
interval: request.query.interval
}

ipfs.stats.bw(options, (err, stat) => {
if (err) {
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

reply({
TotalIn: stat.totalIn,
TotalOut: stat.totalOut,
RateIn: stat.rateIn,
RateOut: stat.rateOut,
})
})
}

8 changes: 6 additions & 2 deletions src/http/api/routes/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ module.exports = (server) => {
}
})

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

0 comments on commit 9665936

Please sign in to comment.