From 73d6d624a3d564d851f55d4640aa87511948cd30 Mon Sep 17 00:00:00 2001 From: Andrew Watkins Date: Mon, 23 Jan 2017 21:14:39 -0800 Subject: [PATCH] WIP all the refactors and in teh darkness bind them --- ui/spec/utils/timeSeriesToDygraphSpec.js | 51 ++-- ui/src/utils/timeSeriesToDygraph.js | 358 ++++++++++++++--------- 2 files changed, 252 insertions(+), 157 deletions(-) diff --git a/ui/spec/utils/timeSeriesToDygraphSpec.js b/ui/spec/utils/timeSeriesToDygraphSpec.js index 0b5396be1..7221ae800 100644 --- a/ui/spec/utils/timeSeriesToDygraphSpec.js +++ b/ui/spec/utils/timeSeriesToDygraphSpec.js @@ -322,12 +322,35 @@ describe('timeSeriesToDygraph', () => { expect(dygraphSeries["m2.f2"].strokeWidth).to.be.above(dygraphSeries["m1.f1"].strokeWidth); }); - it('parses labels alphabetically with the correct field values for multiple series', () => { + it.only('parses labels alphabetically with the correct field values for multiple series', () => { const influxResponse = [ { "response": { "results": [ + { + "series": [ + { + "name":"mb", + "columns": ["time","fa"], + "values": [ + [1000, 200], + [2000, 300], + [4000, 400], + ], + }, + { + "name":"mc", + "columns": ["time","fa"], + "values": [ + [1000, 400], + [2000, 600], + [3000, 800], + [5000, 1000], + ], + }, + ] + }, { "series": [ { @@ -341,19 +364,6 @@ describe('timeSeriesToDygraph', () => { }, ] }, - { - "series": [ - { - "name":"mb", - "columns": ["time","fa","fc","fb"], - "values": [ - [1000, 200, 100, 100], - [2000, 300, 150, 90], - [3000, 400, 200, 80], - ], - }, - ] - }, ], }, } @@ -368,17 +378,18 @@ describe('timeSeriesToDygraph', () => { `ma.fb`, `ma.fc`, `mb.fa`, - `mb.fb`, - `mb.fc`, + `mc.fa`, ], timeSeries: [ - [new Date(1000), 20, 10, 10], - [new Date(2000), 30, 9, 15], - [new Date(3000), 40, 8, 20], + [new Date(1000), 20, 10, 10, 200, 400], + [new Date(2000), 30, 9, 15, 300, 600], + [new Date(3000), 40, 8, 20, null, 800], + [new Date(4000), null, null, null, 400, null], + [new Date(5000), null, null, null, null, 1000], ], }; - console.log(actual.timeSeries); + // console.log(actual.timeSeries); expect(actual.labels).to.deep.equal(expected.labels); expect(actual.timeSeries).to.deep.equal(expected.timeSeries); }); diff --git a/ui/src/utils/timeSeriesToDygraph.js b/ui/src/utils/timeSeriesToDygraph.js index 19bb31404..206ae2c39 100644 --- a/ui/src/utils/timeSeriesToDygraph.js +++ b/ui/src/utils/timeSeriesToDygraph.js @@ -1,3 +1,4 @@ +import _ from 'lodash'; import {STROKE_WIDTH} from 'src/shared/constants'; /** * Accepts an array of raw influxdb responses and returns a format @@ -7,7 +8,7 @@ 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) { - const labels = []; // all of the effective field names (i.e. .) + // const labels = []; // all of the effective field names (i.e. .) const fieldToIndex = {}; // see parseSeries const dates = {}; // map of date as string to date value to minimize string coercion const dygraphSeries = {}; // dygraphSeries is a graph legend label and its corresponding y-axis e.g. {legendLabel1: 'y', legendLabel2: 'y2'}; @@ -30,152 +31,235 @@ export default function timeSeriesToDygraph(raw = [], activeQueryIndex, isInData */ const dateToFieldValue = {}; - 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; - } + const results = raw.reduce((acc, response) => { + return [...acc, ..._.get(response, 'response.results', [])] + }, []) + + const serieses = results.reduce((acc, result, index) => { + return [...acc, ...result.series.map((item) => ({...item, index}))]; + }, []) + + const cells = serieses.reduce((acc, {name, columns, values, index}) => { + const rows = values.map((values) => ({ + name, + columns, + values, + index, + })) + console.log("HI IM Rerws: ", rows) + + rows.forEach(({values: vals, columns: cols, name: n, index: seriesIndex}) => { + const [time, ...rowValues] = vals + rowValues.forEach((value, i) => { + const column = cols[i + 1] + acc.push({ + label: `${n}.${column}`, + value, + time, + seriesIndex, + }) + }) + }) + + return acc + }, []) + + console.log("HI IM CELLS: ", cells) + + const labels = cells.reduce((acc, cell) => { + const existingLabel = acc.find(({label, seriesIndex}) => cell.label === label && cell.seriesIndex === seriesIndex) + + if (!existingLabel) { + acc.push({ + label: cell.label, + seriesIndex: cell.seriesIndex, + }) } - /** - * response looks like: - * { - * results: [ - * { series: [...] }, - * { series: [...] }, - * ] - * } - */ - response.results.forEach(parseResult); + return acc + }, []) - function parseResult(s) { - /* - * s looks like: - * { - * series: [ - * { - * name: "", - * columns: ["time", "", "", ...], - * values: [