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

Commit 2f3df7a

Browse files
authoredAug 10, 2021
fix: return rate in/out as number (#3798)
The return type for `rateIn`/`rateOut` should be a number and not an integer. go-IPFS returns a `float64` which isn't representable in js, but we are not interested in that level of accuracy for this value so just return it as a regular `number` which can be a `float`. Fixes #3782 BREAKING CHANGE: rateIn/rateOut are returned as numbers
1 parent 4f532a5 commit 2f3df7a

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed
 

‎docs/core-api/STATS.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Each yielded object contains the following keys:
5151

5252
- `totalIn` - is a [BigInt][bigNumber], in bytes.
5353
- `totalOut` - is a [BigInt][bigNumber], in bytes.
54-
- `rateIn` - is a [BigInt][bigNumber], in bytes.
55-
- `rateOut` - is a [BigInt][bigNumber], in bytes.
54+
- `rateIn` - is a `float`, in bytes.
55+
- `rateOut` - is a `float`, in bytes.
5656

5757
### Example
5858

@@ -62,8 +62,8 @@ for await (const stats of ipfs.stats.bw()) {
6262
}
6363
// { totalIn: BigInt {...},
6464
// totalOut: BigInt {...},
65-
// rateIn: BigInt {...},
66-
// rateOut: BigInt {...} }
65+
// rateIn: number {...},
66+
// rateOut: number {...} }
6767
```
6868

6969
A great source of [examples][] can be found in the tests for this API.

‎packages/interface-ipfs-core/src/stats/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ exports.expectIsBandwidth = (err, stats) => {
5050
expect(stats).to.have.a.property('rateOut')
5151
expect(isBigInt(stats.totalIn)).to.eql(true)
5252
expect(isBigInt(stats.totalOut)).to.eql(true)
53-
expect(isBigInt(stats.rateIn)).to.eql(true)
54-
expect(isBigInt(stats.rateOut)).to.eql(true)
53+
expect(stats.rateIn).to.be.a('number')
54+
expect(stats.rateOut).to.be.a('number')
5555
}
5656

5757
/**

‎packages/ipfs-core-types/src/stats/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ export interface BWOptions extends AbortOptions {
2222
export interface BWResult {
2323
totalIn: bigint
2424
totalOut: bigint
25-
rateIn: bigint
26-
rateOut: bigint
25+
rateIn: number
26+
rateOut: number
2727
}

‎packages/ipfs-core/src/components/stats/bw.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')
1414
* @typedef {Object} BandwidthInfo
1515
* @property {bigint} totalIn
1616
* @property {bigint} totalOut
17-
* @property {bigint} rateIn
18-
* @property {bigint} rateOut
17+
* @property {number} rateIn
18+
* @property {number} rateOut
1919
*
2020
* @typedef {import('libp2p')} libp2p
2121
* @typedef {import('peer-id')} PeerId
@@ -45,8 +45,8 @@ function getBandwidthStats (libp2p, opts) {
4545
return {
4646
totalIn: BigInt(0),
4747
totalOut: BigInt(0),
48-
rateIn: BigInt(0),
49-
rateOut: BigInt(0)
48+
rateIn: 0.0,
49+
rateOut: 0.0
5050
}
5151
}
5252

@@ -55,8 +55,8 @@ function getBandwidthStats (libp2p, opts) {
5555
return {
5656
totalIn: BigInt(snapshot.dataReceived.integerValue().toString()),
5757
totalOut: BigInt(snapshot.dataSent.integerValue().toString()),
58-
rateIn: BigInt(Math.round(movingAverages.dataReceived[60000].movingAverage() / 60)),
59-
rateOut: BigInt(Math.round(movingAverages.dataSent[60000].movingAverage() / 60))
58+
rateIn: movingAverages.dataReceived[60000].movingAverage() / 60,
59+
rateOut: movingAverages.dataSent[60000].movingAverage() / 60
6060
}
6161
}
6262

‎packages/ipfs-http-client/src/stats/bw.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ module.exports = configure(api => {
2121
transform: (stats) => ({
2222
totalIn: BigInt(stats.TotalIn),
2323
totalOut: BigInt(stats.TotalOut),
24-
rateIn: BigInt(stats.RateIn),
25-
rateOut: BigInt(stats.RateOut)
24+
rateIn: parseFloat(stats.RateIn),
25+
rateOut: parseFloat(stats.RateOut)
2626
})
2727
})
2828

‎packages/ipfs-http-server/src/api/resources/stats.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ exports.bw = {
6161
yield * map(source, stat => ({
6262
TotalIn: stat.totalIn.toString(),
6363
TotalOut: stat.totalOut.toString(),
64-
RateIn: stat.rateIn.toString(),
65-
RateOut: stat.rateOut.toString()
64+
RateIn: stat.rateIn,
65+
RateOut: stat.rateOut
6666
}))
6767
}
6868
))

0 commit comments

Comments
 (0)
This repository has been archived.