diff --git a/ui/src/utils/timeSeriesToDygraph.js b/ui/src/utils/timeSeriesToDygraph.js index 4ca6fe4000..2e71482236 100644 --- a/ui/src/utils/timeSeriesToDygraph.js +++ b/ui/src/utils/timeSeriesToDygraph.js @@ -8,257 +8,101 @@ import {STROKE_WIDTH} from 'src/shared/constants'; // activeQueryIndex is an optional argument that indicated which query's series we want highlighted. export default function timeSeriesToDygraph(raw = [], activeQueryIndex, isInDataExplorer) { // collect results from each influx response - const results = raw.reduce((acc, response, responseIndex) => { - const responses = _.get(response, 'response.results', []) - const indexedResponses = responses.map((response) => ({...response, responseIndex})) - return [...acc, ...indexedResponses] - }, []) + const results = raw.reduce((acc, rawResponse, responseIndex) => { + const responses = _.get(rawResponse, 'response.results', []); + const indexedResponses = responses.map((response) => ({...response, responseIndex})); + return [...acc, ...indexedResponses]; + }, []); // collect each series const serieses = results.reduce((acc, {series, responseIndex}, index) => { return [...acc, ...series.map((item) => ({...item, responseIndex, index}))]; - }, []) + }, []); // convert series into cells with rows and columns const cells = serieses.reduce((acc, {name, columns, values, index, responseIndex}) => { - const rows = values.map((values) => ({ + const rows = values.map((vals) => ({ name, columns, - values, + vals, index, - })) + })); - rows.forEach(({values: vals, columns: cols, name: n, index: seriesIndex}) => { - const [time, ...rowValues] = vals + rows.forEach(({vals, columns: cols, name: n, index: seriesIndex}) => { + const [time, ...rowValues] = vals; rowValues.forEach((value, i) => { - const column = cols[i + 1] + const column = cols[i + 1]; acc.push({ label: `${n}.${column}`, value, time, seriesIndex, responseIndex, - }) - }) - }) + }); + }); + }); - return acc - }, []) + return acc; + }, []); const labels = cells.reduce((acc, {label, seriesIndex, responseIndex}) => { const existingLabel = acc.find(({ label: findLabel, seriesIndex: findSeriesIndex, - }) => findLabel === label && findSeriesIndex === seriesIndex) + }) => findLabel === label && findSeriesIndex === seriesIndex); if (!existingLabel) { acc.push({ label, seriesIndex, responseIndex, - }) + }); } - return acc - }, []) + return acc; + }, []); - const sortedLabels = _.sortBy(labels, 'label') + const sortedLabels = _.sortBy(labels, 'label'); const timeSeries = cells.reduce((acc, cell) => { - let existingRowIndex = acc.findIndex(({time}) => cell.time === time) + let existingRowIndex = acc.findIndex(({time}) => cell.time === time); if (existingRowIndex === -1) { acc.push({ time: cell.time, values: Array(sortedLabels.length).fill(null), - }) + }); - existingRowIndex = acc.length - 1 + existingRowIndex = acc.length - 1; } - const values = acc[existingRowIndex].values + const values = acc[existingRowIndex].values; const labelIndex = sortedLabels.findIndex(({label, seriesIndex}) => label === cell.label && cell.seriesIndex === seriesIndex); - values[labelIndex] = cell.value - acc[existingRowIndex].values = values + values[labelIndex] = cell.value; + acc[existingRowIndex].values = values; - return acc - }, []) + return acc; + }, []); - const sortedTimeSeries = _.sortBy(timeSeries, 'time') + const sortedTimeSeries = _.sortBy(timeSeries, 'time'); const {light, heavy} = STROKE_WIDTH; const dygraphSeries = sortedLabels.reduce((acc, {label, responseIndex}) => { acc[label] = { strokeWidth: responseIndex === activeQueryIndex ? heavy : light, - } + }; if (!isInDataExplorer) { - acc[label].axis = responseIndex === 0 ? 'y' : 'y2' + acc[label].axis = responseIndex === 0 ? 'y' : 'y2'; } - return acc - }, {}) + return acc; + }, {}); - const timeSeriesToDygraph = { + return { timeSeries: sortedTimeSeries.map(({time, values}) => ([new Date(time), ...values])), labels: ["time", ...sortedLabels.map(({label}) => label)], dygraphSeries, - } - - return timeSeriesToDygraph; - // timeSeriesToDygraph , {labels: [], timeSeries: []} - - // raw.forEach(({response}, queryIndex) => { - // // If a response is an empty result set or a query returned an error - // // from InfluxDB, don't try and parse. - // if (response.results.length) { - // if (isEmpty(response) || hasError(response)) { - // return; - // } - // } - // - // /** - // * response looks like: - // * { - // * results: [ - // * { series: [...] }, - // * { series: [...] }, - // * ] - // * } - // */ - // response.results.forEach(parseResult); - // - // function parseResult(s) { - // /* - // * s looks like: - // * { - // * series: [ - // * { - // * name: "", - // * columns: ["time", "", "", ...], - // * values: [