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

Commit

Permalink
fix: return rate in/out as number (#3798)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain committed Aug 10, 2021
1 parent 4f532a5 commit 2f3df7a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions docs/core-api/STATS.md
Expand Up @@ -51,8 +51,8 @@ Each yielded object contains the following keys:

- `totalIn` - is a [BigInt][bigNumber], in bytes.
- `totalOut` - is a [BigInt][bigNumber], in bytes.
- `rateIn` - is a [BigInt][bigNumber], in bytes.
- `rateOut` - is a [BigInt][bigNumber], in bytes.
- `rateIn` - is a `float`, in bytes.
- `rateOut` - is a `float`, in bytes.

### Example

Expand All @@ -62,8 +62,8 @@ for await (const stats of ipfs.stats.bw()) {
}
// { totalIn: BigInt {...},
// totalOut: BigInt {...},
// rateIn: BigInt {...},
// rateOut: BigInt {...} }
// rateIn: number {...},
// rateOut: number {...} }
```

A great source of [examples][] can be found in the tests for this API.
Expand Down
4 changes: 2 additions & 2 deletions packages/interface-ipfs-core/src/stats/utils.js
Expand Up @@ -50,8 +50,8 @@ exports.expectIsBandwidth = (err, stats) => {
expect(stats).to.have.a.property('rateOut')
expect(isBigInt(stats.totalIn)).to.eql(true)
expect(isBigInt(stats.totalOut)).to.eql(true)
expect(isBigInt(stats.rateIn)).to.eql(true)
expect(isBigInt(stats.rateOut)).to.eql(true)
expect(stats.rateIn).to.be.a('number')
expect(stats.rateOut).to.be.a('number')
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/ipfs-core-types/src/stats/index.d.ts
Expand Up @@ -22,6 +22,6 @@ export interface BWOptions extends AbortOptions {
export interface BWResult {
totalIn: bigint
totalOut: bigint
rateIn: bigint
rateOut: bigint
rateIn: number
rateOut: number
}
12 changes: 6 additions & 6 deletions packages/ipfs-core/src/components/stats/bw.js
Expand Up @@ -14,8 +14,8 @@ const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')
* @typedef {Object} BandwidthInfo
* @property {bigint} totalIn
* @property {bigint} totalOut
* @property {bigint} rateIn
* @property {bigint} rateOut
* @property {number} rateIn
* @property {number} rateOut
*
* @typedef {import('libp2p')} libp2p
* @typedef {import('peer-id')} PeerId
Expand Down Expand Up @@ -45,8 +45,8 @@ function getBandwidthStats (libp2p, opts) {
return {
totalIn: BigInt(0),
totalOut: BigInt(0),
rateIn: BigInt(0),
rateOut: BigInt(0)
rateIn: 0.0,
rateOut: 0.0
}
}

Expand All @@ -55,8 +55,8 @@ function getBandwidthStats (libp2p, opts) {
return {
totalIn: BigInt(snapshot.dataReceived.integerValue().toString()),
totalOut: BigInt(snapshot.dataSent.integerValue().toString()),
rateIn: BigInt(Math.round(movingAverages.dataReceived[60000].movingAverage() / 60)),
rateOut: BigInt(Math.round(movingAverages.dataSent[60000].movingAverage() / 60))
rateIn: movingAverages.dataReceived[60000].movingAverage() / 60,
rateOut: movingAverages.dataSent[60000].movingAverage() / 60
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ipfs-http-client/src/stats/bw.js
Expand Up @@ -21,8 +21,8 @@ module.exports = configure(api => {
transform: (stats) => ({
totalIn: BigInt(stats.TotalIn),
totalOut: BigInt(stats.TotalOut),
rateIn: BigInt(stats.RateIn),
rateOut: BigInt(stats.RateOut)
rateIn: parseFloat(stats.RateIn),
rateOut: parseFloat(stats.RateOut)
})
})

Expand Down
4 changes: 2 additions & 2 deletions packages/ipfs-http-server/src/api/resources/stats.js
Expand Up @@ -61,8 +61,8 @@ exports.bw = {
yield * map(source, stat => ({
TotalIn: stat.totalIn.toString(),
TotalOut: stat.totalOut.toString(),
RateIn: stat.rateIn.toString(),
RateOut: stat.rateOut.toString()
RateIn: stat.rateIn,
RateOut: stat.rateOut
}))
}
))
Expand Down

0 comments on commit 2f3df7a

Please sign in to comment.