From c2850bb670c250b432fab3f5ddd11d2a08a5d9a3 Mon Sep 17 00:00:00 2001 From: Andrew Watkins Date: Thu, 12 Jan 2017 13:44:22 -0800 Subject: [PATCH 1/2] Export getRange to helper funciton --- ui/src/shared/components/Dygraph.js | 58 +-------------------- ui/src/shared/parsing/getRangeForDygraph.js | 56 ++++++++++++++++++++ 2 files changed, 57 insertions(+), 57 deletions(-) create mode 100644 ui/src/shared/parsing/getRangeForDygraph.js diff --git a/ui/src/shared/components/Dygraph.js b/ui/src/shared/components/Dygraph.js index c1873f1c2..12895f862 100644 --- a/ui/src/shared/components/Dygraph.js +++ b/ui/src/shared/components/Dygraph.js @@ -1,6 +1,7 @@ /* eslint-disable no-magic-numbers */ import React, {PropTypes} from 'react'; import Dygraph from '../../external/dygraph'; +import getRange from 'src/shared/parsing/getRangeForDygraph'; import _ from 'lodash'; const { @@ -179,60 +180,3 @@ export default React.createClass({ ); }, }); - -const PADDING_FACTOR = 0.1; - -function getRange(timeSeries, override, value = null, rangeValue = null) { - if (override) { - return override; - } - - const subtractPadding = (val) => +val - val * PADDING_FACTOR; - const addPadding = (val) => +val + val * PADDING_FACTOR; - - const pad = (val, side) => { - if (val === null || val === '') { - return null; - } - - if (val < 0) { - return side === "top" ? subtractPadding(val) : addPadding(val); - } - - return side === "top" ? addPadding(val) : subtractPadding(val); - }; - - const points = [ - ...timeSeries, - [null, pad(value)], - [null, pad(rangeValue, "top")], - ]; - - const range = points.reduce(([min, max], series) => { - for (let i = 1; i < series.length; i++) { - const val = series[i]; - - if (max === null) { - max = val; - } - - if (min === null) { - min = val; - } - - if (typeof val === "number") { - min = Math.min(min, val); - max = Math.max(max, val); - } - - return [min, max]; - } - }, [null, null]); - - // Dygraph will not reliably plot X / Y axis labels if min and max are both 0 - if (range[0] === 0 && range[1] === 0) { - return [null, null]; - } - - return range; -} diff --git a/ui/src/shared/parsing/getRangeForDygraph.js b/ui/src/shared/parsing/getRangeForDygraph.js new file mode 100644 index 000000000..29314e47b --- /dev/null +++ b/ui/src/shared/parsing/getRangeForDygraph.js @@ -0,0 +1,56 @@ +const PADDING_FACTOR = 0.1; + +export default function getRange(timeSeries, override, value = null, rangeValue = null) { + if (override) { + return override; + } + + const subtractPadding = (val) => +val - val * PADDING_FACTOR; + const addPadding = (val) => +val + val * PADDING_FACTOR; + + const pad = (val, side) => { + if (val === null || val === '') { + return null; + } + + if (val < 0) { + return side === "top" ? subtractPadding(val) : addPadding(val); + } + + return side === "top" ? addPadding(val) : subtractPadding(val); + }; + + const points = [ + ...timeSeries, + [null, pad(value)], + [null, pad(rangeValue, "top")], + ]; + + const range = points.reduce(([min, max], series) => { + for (let i = 1; i < series.length; i++) { + const val = series[i]; + + if (max === null) { + max = val; + } + + if (min === null) { + min = val; + } + + if (typeof val === "number") { + min = Math.min(min, val); + max = Math.max(max, val); + } + } + + return [min, max]; + }, [null, null]); + + // If time series is such that min and max are equal use Dygraph defaults + if (range[0] === range[1]) { + return [null, null]; + } + + return range; +} From 996c1d077b80702301a65b3fc1e73f4084c42759 Mon Sep 17 00:00:00 2001 From: Andrew Watkins Date: Thu, 12 Jan 2017 13:44:53 -0800 Subject: [PATCH 2/2] Add tests for range calc --- .../shared/parsing/getRangeForDygraphSpec.js | 43 +++++++++++++++++++ ui/src/shared/parsing/getRangeForDygraph.js | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 ui/spec/shared/parsing/getRangeForDygraphSpec.js diff --git a/ui/spec/shared/parsing/getRangeForDygraphSpec.js b/ui/spec/shared/parsing/getRangeForDygraphSpec.js new file mode 100644 index 000000000..a65fef9f2 --- /dev/null +++ b/ui/spec/shared/parsing/getRangeForDygraphSpec.js @@ -0,0 +1,43 @@ +import getRange from 'shared/parsing/getRangeForDygraph'; + +describe('getRangeForDygraphSpec', () => { + it('gets the range for one timeSeries', () => { + const timeSeries = [[new Date(1000), 1], [new Date(2000), 2], [new Date(3000), 3]]; + + const actual = getRange(timeSeries); + const expected = [1, 3]; + + expect(actual).to.deep.equal(expected); + }); + + it('does not get range when a range is provided', () => { + const timeSeries = [[new Date(1000), 1], [new Date(2000), 2], [new Date(3000), 3]]; + + const providedRange = [0, 4]; + const actual = getRange(timeSeries, providedRange); + + expect(actual).to.deep.equal(providedRange); + }); + + it('gets the range for multiple timeSeries', () => { + const timeSeries = [ + [new Date(1000), null, 1], + [new Date(1000), 100, 1], + [new Date(2000), null, 2], + [new Date(3000), 200, 3], + ]; + + const actual = getRange(timeSeries); + const expected = [1, 200]; + + expect(actual).to.deep.equal(expected); + }); + + it('returns a null array of two elements when min and max are equal', () => { + const timeSeries = [[new Date(1000), 1], [new Date(2000), 1], [new Date(3000), 1]]; + const actual = getRange(timeSeries); + const expected = [null, null]; + + expect(actual).to.deep.equal(expected); + }); +}); diff --git a/ui/src/shared/parsing/getRangeForDygraph.js b/ui/src/shared/parsing/getRangeForDygraph.js index 29314e47b..204cfda90 100644 --- a/ui/src/shared/parsing/getRangeForDygraph.js +++ b/ui/src/shared/parsing/getRangeForDygraph.js @@ -26,7 +26,7 @@ export default function getRange(timeSeries, override, value = null, rangeValue [null, pad(rangeValue, "top")], ]; - const range = points.reduce(([min, max], series) => { + const range = points.reduce(([min, max] = [], series) => { for (let i = 1; i < series.length; i++) { const val = series[i];