From 47b5f6cf4dae9a93668b47dd4435a05056516fa5 Mon Sep 17 00:00:00 2001 From: Andrew Watkins Date: Thu, 3 Aug 2017 14:28:58 -0700 Subject: [PATCH] Add logic to handle ranges that are submitted as equal --- ui/spec/shared/parsing/getRangeForDygraphSpec.js | 8 ++++++++ ui/src/shared/parsing/getRangeForDygraph.js | 15 ++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/ui/spec/shared/parsing/getRangeForDygraphSpec.js b/ui/spec/shared/parsing/getRangeForDygraphSpec.js index c44d1a4a9a..1c3c7d80ef 100644 --- a/ui/spec/shared/parsing/getRangeForDygraphSpec.js +++ b/ui/spec/shared/parsing/getRangeForDygraphSpec.js @@ -23,6 +23,14 @@ describe('getRangeForDygraphSpec', () => { expect(actual).to.deep.equal([0, 4]) }) + it('does not use the user submitted range if they are equal', () => { + const timeSeries = [[date, min], [date, max], [date, mid]] + const providedRange = ['0', '0'] + const actual = getRange(timeSeries, providedRange) + + expect(actual).to.deep.equal([min, max]) + }) + it('gets the range for multiple timeSeries', () => { const timeSeries = [[date, null, min], [date, max, mid], [date, null, mid]] const actual = getRange(timeSeries) diff --git a/ui/src/shared/parsing/getRangeForDygraph.js b/ui/src/shared/parsing/getRangeForDygraph.js index bb874b3588..7a19fa439f 100644 --- a/ui/src/shared/parsing/getRangeForDygraph.js +++ b/ui/src/shared/parsing/getRangeForDygraph.js @@ -1,6 +1,6 @@ const PADDING_FACTOR = 0.1 -const considerZero = (userNumber, number) => { +const considerEmpty = (userNumber, number) => { if (userNumber === '') { return null } @@ -20,10 +20,6 @@ const getRange = ( const {value, rangeValue, operator} = ruleValues const [userMin, userMax] = userSelectedRange - if (userMin && userMax) { - return [considerZero(userMin), considerZero(userMax)] - } - const subtractPadding = val => +val - Math.abs(val * PADDING_FACTOR) const addPadding = val => +val + Math.abs(val * PADDING_FACTOR) @@ -65,8 +61,9 @@ const getRange = ( [null, null] ) - // If time series is such that min and max are equal use Dygraph defaults const [min, max] = range + + // If time series is such that min and max are equal use Dygraph defaults if (min === max) { return [null, null] } @@ -75,7 +72,11 @@ const getRange = ( return [min, max] } - return [considerZero(userMin, min), considerZero(userMax, max)] + if (userMin && userMax) { + return [considerEmpty(userMin), considerEmpty(userMax)] + } + + return [considerEmpty(userMin, min), considerEmpty(userMax, max)] } export default getRange