From 78cab61c9c20045fd94a463b5084a9775c1188f8 Mon Sep 17 00:00:00 2001 From: Andrew Watkins Date: Mon, 28 Aug 2017 11:58:04 -0700 Subject: [PATCH] Fix js rounding errors --- ui/package.json | 1 + ui/src/shared/parsing/getRangeForDygraph.js | 26 +++++++++++---------- ui/yarn.lock | 4 ++++ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/ui/package.json b/ui/package.json index bb9909e6e1..8db744ea4a 100644 --- a/ui/package.json +++ b/ui/package.json @@ -96,6 +96,7 @@ }, "dependencies": { "axios": "^0.13.1", + "bignumber.js": "^4.0.2", "bootstrap": "^3.3.7", "calculate-size": "^1.1.1", "classnames": "^2.2.3", diff --git a/ui/src/shared/parsing/getRangeForDygraph.js b/ui/src/shared/parsing/getRangeForDygraph.js index d012b64f61..06364a9267 100644 --- a/ui/src/shared/parsing/getRangeForDygraph.js +++ b/ui/src/shared/parsing/getRangeForDygraph.js @@ -1,4 +1,7 @@ -const PADDING_FACTOR = 0.1 +import BigNumber from 'bignumber.js' + +const ADD_FACTOR = 1.1 +const SUB_FACTOR = 0.9 const considerEmpty = (userNumber, number) => { if (userNumber) { @@ -16,19 +19,21 @@ const getRange = ( const {value, rangeValue, operator} = ruleValues const [userMin, userMax] = userSelectedRange - const subtractPadding = val => +val - Math.abs(val * PADDING_FACTOR) - const addPadding = val => +val + Math.abs(val * PADDING_FACTOR) + const addPad = bigNum => bigNum.times(ADD_FACTOR).toNumber() + const subPad = bigNum => bigNum.times(SUB_FACTOR).toNumber() - const pad = val => { - if (val === null || val === '') { + const pad = v => { + if (v === null || v === '') { return null } + const val = new BigNumber(v) + if (operator === 'less than') { - return val < 0 ? addPadding(val) : subtractPadding(val) + return val.lessThan(0) ? addPad(val) : subPad(val) } - return val < 0 ? subtractPadding(val) : addPadding(val) + return val.lessThan(0) ? subPad(val) : addPad(val) } const points = [...timeSeries, [null, pad(value)], [null, pad(rangeValue)]] @@ -58,11 +63,8 @@ const getRange = ( ) const [calcMin, calcMax] = range - - const [min, max] = [ - considerEmpty(userMin, calcMin), - considerEmpty(userMax, calcMax), - ] + const min = considerEmpty(userMin, calcMin) + const max = considerEmpty(userMax, calcMax) if (min === max) { if (min > 0) { diff --git a/ui/yarn.lock b/ui/yarn.lock index 1b6ce80e12..0c55a38651 100644 --- a/ui/yarn.lock +++ b/ui/yarn.lock @@ -1446,6 +1446,10 @@ big.js@^3.1.3: version "3.1.3" resolved "https://registry.yarnpkg.com/big.js/-/big.js-3.1.3.tgz#4cada2193652eb3ca9ec8e55c9015669c9806978" +bignumber.js@^4.0.2: + version "4.0.2" + resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.0.2.tgz#2d1dc37ee5968867ecea90b6da4d16e68608d21d" + binary-extensions@^1.0.0: version "1.8.0" resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.8.0.tgz#48ec8d16df4377eae5fa5884682480af4d95c774"