This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert stats API to async/await
- Loading branch information
Alan Shaw
committed
Dec 16, 2019
1 parent
21e62cb
commit 8d2c57c
Showing
5 changed files
with
79 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters