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 float
Browse files Browse the repository at this point in the history
The return type for `rateIn`/`rateOut` should be a float and not an
integer.

go-IPFS returns a `float64` which isn't representible in js, but we
are not interested in that level of accuracy for this value so just
return it as a regular `float`.

Fixes #3782
  • Loading branch information
achingbrain committed Aug 9, 2021
1 parent 1ad6001 commit 610bff7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions docs/core-api/STATS.md
Original file line number Diff line number Diff line change
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: float {...},
// rateOut: float {...} }
```

A great source of [examples][] can be found in the tests for this API.
Expand Down
4 changes: 2 additions & 2 deletions packages/ipfs-core-types/src/stats/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export interface BWOptions extends AbortOptions {
export interface BWResult {
totalIn: bigint
totalOut: bigint
rateIn: bigint
rateOut: bigint
rateIn: float
rateOut: float
}
8 changes: 4 additions & 4 deletions packages/ipfs-core/src/components/stats/bw.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 610bff7

Please sign in to comment.