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

Commit

Permalink
refactor: convert stats API to async/await
Browse files Browse the repository at this point in the history
Depends on:

* [ ] libp2p 0.27
* [ ] #2659
* [ ] #2674
  • Loading branch information
Alan Shaw committed Dec 16, 2019
1 parent 21e62cb commit 8d2c57c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 84 deletions.
7 changes: 6 additions & 1 deletion src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,12 @@ function createApi ({
add,
config: Commands.config({ repo }),
init: () => { throw new AlreadyInitializedError() },
start
start,
stats: {
bitswap: () => { throw new NotStartedError() },
bw: () => { throw new NotStartedError() },
repo: Commands.repo.stat({ repo })
}
}

return api
Expand Down
5 changes: 5 additions & 0 deletions src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ function createApi ({
config: Commands.config({ repo }),
init: () => { throw new AlreadyInitializedError() },
start: () => apiManager.api,
stats: {
bitswap: Commands.bitswap.stat({ bitswap }),
bw: Commands.stats.bw({ libp2p }),
repo: Commands.repo.stat({ repo })
},
stop
}

Expand Down
83 changes: 0 additions & 83 deletions src/core/components/stats.js

This file was deleted.

63 changes: 63 additions & 0 deletions src/core/components/stats/bw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict'

const Big = require('bignumber.js')
const human = require('human-to-milliseconds')
const errCode = require('err-code')

function getBandwidthStats (libp2p, opts) {
let stats

if (opts.peer) {
stats = libp2p.metrics.forPeer(opts.peer)
} else if (opts.proto) {
stats = libp2p.metrics.forProtocol(opts.proto)
} else {
stats = libp2p.metrics.global
}

if (!stats) {
return {
totalIn: new Big(0),
totalOut: new Big(0),
rateIn: new Big(0),
rateOut: new Big(0)
}
}

const { movingAverages, snapshot } = stats

return {
totalIn: snapshot.dataReceived,
totalOut: snapshot.dataSent,
rateIn: new Big(movingAverages.dataReceived[60000].movingAverage() / 60),
rateOut: new Big(movingAverages.dataSent[60000].movingAverage() / 60)
}
}

module.exports = ({ libp2p }) => {
return async function * (options) {
options = options || {}

if (!options.poll) {
yield getBandwidthStats(libp2p, options)
return
}

let interval
try {
interval = human(options.interval || '1s')
} catch (err) {
throw errCode(err, 'ERR_INVALID_POLL_INTERVAL')
}

let timeoutId
try {
while (true) {
yield getBandwidthStats(libp2p, options)
await new Promise(resolve => { timeoutId = setTimeout(resolve, interval) })
}
} finally {
clearTimeout(timeoutId)
}
}
}
5 changes: 5 additions & 0 deletions src/core/components/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ function createApi ({
config: Commands.config({ repo }),
init: () => { throw new AlreadyInitializedError() },
start,
stats: {
bitswap: () => { throw new NotStartedError() },
bw: () => { throw new NotStartedError() },
repo: Commands.repo.stat({ repo })
},
stop: () => apiManager.api
}

Expand Down

0 comments on commit 8d2c57c

Please sign in to comment.