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

Commit 8d2c57c

Browse files
author
Alan Shaw
committed
refactor: convert stats API to async/await
Depends on: * [ ] libp2p 0.27 * [ ] #2659 * [ ] #2674
1 parent 21e62cb commit 8d2c57c

File tree

5 files changed

+79
-84
lines changed

5 files changed

+79
-84
lines changed

src/core/components/init.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,12 @@ function createApi ({
301301
add,
302302
config: Commands.config({ repo }),
303303
init: () => { throw new AlreadyInitializedError() },
304-
start
304+
start,
305+
stats: {
306+
bitswap: () => { throw new NotStartedError() },
307+
bw: () => { throw new NotStartedError() },
308+
repo: Commands.repo.stat({ repo })
309+
}
305310
}
306311

307312
return api

src/core/components/start.js

+5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ function createApi ({
134134
config: Commands.config({ repo }),
135135
init: () => { throw new AlreadyInitializedError() },
136136
start: () => apiManager.api,
137+
stats: {
138+
bitswap: Commands.bitswap.stat({ bitswap }),
139+
bw: Commands.stats.bw({ libp2p }),
140+
repo: Commands.repo.stat({ repo })
141+
},
137142
stop
138143
}
139144

src/core/components/stats.js

-83
This file was deleted.

src/core/components/stats/bw.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
const Big = require('bignumber.js')
4+
const human = require('human-to-milliseconds')
5+
const errCode = require('err-code')
6+
7+
function getBandwidthStats (libp2p, opts) {
8+
let stats
9+
10+
if (opts.peer) {
11+
stats = libp2p.metrics.forPeer(opts.peer)
12+
} else if (opts.proto) {
13+
stats = libp2p.metrics.forProtocol(opts.proto)
14+
} else {
15+
stats = libp2p.metrics.global
16+
}
17+
18+
if (!stats) {
19+
return {
20+
totalIn: new Big(0),
21+
totalOut: new Big(0),
22+
rateIn: new Big(0),
23+
rateOut: new Big(0)
24+
}
25+
}
26+
27+
const { movingAverages, snapshot } = stats
28+
29+
return {
30+
totalIn: snapshot.dataReceived,
31+
totalOut: snapshot.dataSent,
32+
rateIn: new Big(movingAverages.dataReceived[60000].movingAverage() / 60),
33+
rateOut: new Big(movingAverages.dataSent[60000].movingAverage() / 60)
34+
}
35+
}
36+
37+
module.exports = ({ libp2p }) => {
38+
return async function * (options) {
39+
options = options || {}
40+
41+
if (!options.poll) {
42+
yield getBandwidthStats(libp2p, options)
43+
return
44+
}
45+
46+
let interval
47+
try {
48+
interval = human(options.interval || '1s')
49+
} catch (err) {
50+
throw errCode(err, 'ERR_INVALID_POLL_INTERVAL')
51+
}
52+
53+
let timeoutId
54+
try {
55+
while (true) {
56+
yield getBandwidthStats(libp2p, options)
57+
await new Promise(resolve => { timeoutId = setTimeout(resolve, interval) })
58+
}
59+
} finally {
60+
clearTimeout(timeoutId)
61+
}
62+
}
63+
}

src/core/components/stop.js

+5
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ function createApi ({
101101
config: Commands.config({ repo }),
102102
init: () => { throw new AlreadyInitializedError() },
103103
start,
104+
stats: {
105+
bitswap: () => { throw new NotStartedError() },
106+
bw: () => { throw new NotStartedError() },
107+
repo: Commands.repo.stat({ repo })
108+
},
104109
stop: () => apiManager.api
105110
}
106111

0 commit comments

Comments
 (0)