From 610bff75968f523dee63f0ceed7d7a5be8e2165c Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 9 Aug 2021 14:27:15 +0100 Subject: [PATCH 1/3] fix: return rate in/out as float 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 --- docs/core-api/STATS.md | 8 ++++---- packages/ipfs-core-types/src/stats/index.d.ts | 4 ++-- packages/ipfs-core/src/components/stats/bw.js | 8 ++++---- packages/ipfs-http-client/src/stats/bw.js | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/core-api/STATS.md b/docs/core-api/STATS.md index bc661c069b..683d5ba0d3 100644 --- a/docs/core-api/STATS.md +++ b/docs/core-api/STATS.md @@ -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 @@ -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. diff --git a/packages/ipfs-core-types/src/stats/index.d.ts b/packages/ipfs-core-types/src/stats/index.d.ts index a3628ab131..d0a7ed953c 100644 --- a/packages/ipfs-core-types/src/stats/index.d.ts +++ b/packages/ipfs-core-types/src/stats/index.d.ts @@ -22,6 +22,6 @@ export interface BWOptions extends AbortOptions { export interface BWResult { totalIn: bigint totalOut: bigint - rateIn: bigint - rateOut: bigint + rateIn: float + rateOut: float } diff --git a/packages/ipfs-core/src/components/stats/bw.js b/packages/ipfs-core/src/components/stats/bw.js index 3981059848..e2ab5f20e1 100644 --- a/packages/ipfs-core/src/components/stats/bw.js +++ b/packages/ipfs-core/src/components/stats/bw.js @@ -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 } } @@ -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 } } diff --git a/packages/ipfs-http-client/src/stats/bw.js b/packages/ipfs-http-client/src/stats/bw.js index bcea618085..ef77cd4c4d 100644 --- a/packages/ipfs-http-client/src/stats/bw.js +++ b/packages/ipfs-http-client/src/stats/bw.js @@ -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) }) }) From 21bd3eab1fd8ae414e246b1664db841be1c983e6 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 9 Aug 2021 14:52:36 +0100 Subject: [PATCH 2/3] chore: update type --- docs/core-api/STATS.md | 4 ++-- packages/ipfs-core-types/src/stats/index.d.ts | 4 ++-- packages/ipfs-core/src/components/stats/bw.js | 4 ++-- packages/ipfs-http-server/src/api/resources/stats.js | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/core-api/STATS.md b/docs/core-api/STATS.md index 683d5ba0d3..22296b1cec 100644 --- a/docs/core-api/STATS.md +++ b/docs/core-api/STATS.md @@ -62,8 +62,8 @@ for await (const stats of ipfs.stats.bw()) { } // { totalIn: BigInt {...}, // totalOut: BigInt {...}, -// rateIn: float {...}, -// rateOut: float {...} } +// rateIn: number {...}, +// rateOut: number {...} } ``` A great source of [examples][] can be found in the tests for this API. diff --git a/packages/ipfs-core-types/src/stats/index.d.ts b/packages/ipfs-core-types/src/stats/index.d.ts index d0a7ed953c..3f8e66d942 100644 --- a/packages/ipfs-core-types/src/stats/index.d.ts +++ b/packages/ipfs-core-types/src/stats/index.d.ts @@ -22,6 +22,6 @@ export interface BWOptions extends AbortOptions { export interface BWResult { totalIn: bigint totalOut: bigint - rateIn: float - rateOut: float + rateIn: number + rateOut: number } diff --git a/packages/ipfs-core/src/components/stats/bw.js b/packages/ipfs-core/src/components/stats/bw.js index e2ab5f20e1..af450cc313 100644 --- a/packages/ipfs-core/src/components/stats/bw.js +++ b/packages/ipfs-core/src/components/stats/bw.js @@ -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 diff --git a/packages/ipfs-http-server/src/api/resources/stats.js b/packages/ipfs-http-server/src/api/resources/stats.js index 923ce969ac..491d447d7b 100644 --- a/packages/ipfs-http-server/src/api/resources/stats.js +++ b/packages/ipfs-http-server/src/api/resources/stats.js @@ -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 })) } )) From 26bd4d6921b81a29318fdfbb1009e756e648b806 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 9 Aug 2021 17:49:55 +0100 Subject: [PATCH 3/3] chore: fix test --- packages/interface-ipfs-core/src/stats/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/interface-ipfs-core/src/stats/utils.js b/packages/interface-ipfs-core/src/stats/utils.js index afdcd215f4..5714675d8b 100644 --- a/packages/interface-ipfs-core/src/stats/utils.js +++ b/packages/interface-ipfs-core/src/stats/utils.js @@ -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') } /**