From ef8531ced1220f0fbb8b01be452de815ed2dad6a Mon Sep 17 00:00:00 2001 From: Bucky Schwarz Date: Mon, 6 Jan 2020 17:14:16 -0800 Subject: [PATCH] fix: show queries built in advanced mode when switching tabs --- ui/src/telegrafs/reducers/index.ts | 2 +- ui/src/timeMachine/reducers/index.test.ts | 113 ++++++++++++++++++++++ ui/src/timeMachine/reducers/index.ts | 4 +- 3 files changed, 116 insertions(+), 3 deletions(-) diff --git a/ui/src/telegrafs/reducers/index.ts b/ui/src/telegrafs/reducers/index.ts index 75049f06ecc..48c7939b284 100644 --- a/ui/src/telegrafs/reducers/index.ts +++ b/ui/src/telegrafs/reducers/index.ts @@ -74,4 +74,4 @@ export const telegrafsReducer = ( return } } - }) \ No newline at end of file + }) diff --git a/ui/src/timeMachine/reducers/index.test.ts b/ui/src/timeMachine/reducers/index.test.ts index f4a682b751f..608347e8e8f 100644 --- a/ui/src/timeMachine/reducers/index.test.ts +++ b/ui/src/timeMachine/reducers/index.test.ts @@ -1,11 +1,14 @@ import {createStore} from 'redux' import { + buildActiveQuery, initialState, initialStateHelper, timeMachinesReducer, } from 'src/timeMachine/reducers' +import {buildQuery} from 'src/timeMachine/utils/queryBuilder' + describe('the Time Machine reducer', () => { describe('setting the default aggregateFunctionType', () => { const store = createStore(timeMachinesReducer, initialState()) @@ -39,4 +42,114 @@ describe('the Time Machine reducer', () => { ) }) }) + + describe('buildActiveQuery', () => { + let draftState + + beforeEach(() => { + draftState = initialStateHelper() + }) + + it('it builds the query if the query builder config is valid', () => { + const expectedText = + 'from(bucket: "metrics")\n |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n |> filter(fn: (r) => r._measurement == "mem")\n |> filter(fn: (r) => r._field == "active")\n |> aggregateWindow(every: v.windowPeriod, fn: mean)\n |> yield(name: "mean")' + const validDraftQuery = { + text: '', + editMode: 'advanced', + name: '', + builderConfig: { + buckets: ['metrics'], + tags: [ + { + key: '_measurement', + values: ['mem'], + }, + { + key: '_field', + values: ['active'], + }, + { + key: 'host', + values: [], + }, + ], + functions: [ + { + name: 'mean', + }, + ], + aggregateWindow: { + period: 'auto', + }, + }, + hidden: false, + } + + draftState.draftQueries[draftState.activeQueryIndex] = validDraftQuery + buildActiveQuery(draftState) + expect(draftState.draftQueries[draftState.activeQueryIndex].text).toEqual( + expectedText + ) + }) + + it("sets the text of invalid queries to empty string if they don't have any query text", () => { + const invalidDraftQueryWithoutText = { + editMode: 'advanced', + name: '', + builderConfig: { + buckets: [], + tags: [ + { + key: '_measurement', + values: [], + }, + ], + functions: [], + aggregateWindow: { + period: '', + }, + }, + hidden: false, + } + + draftState.draftQueries[ + draftState.activeQueryIndex + ] = invalidDraftQueryWithoutText + buildActiveQuery(draftState) + expect(draftState.draftQueries[draftState.activeQueryIndex].text).toEqual( + '' + ) + }) + + it("retains text of valid queries that weren't built with the query builder", () => { + const invalidDraftQueryWithText = { + text: + 'from(bucket: v.bucket)\n |> range(start: v.timeRangeStart)\n |> filter(fn: (r) => r._measurement == "system")\n |> filter(fn: (r) => r._field == "load1" or r._field == "load5" or r._field == "load15")\n |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)\n |> yield(name: "mean")', + editMode: 'advanced', + name: '', + builderConfig: { + buckets: [], + tags: [ + { + key: '_measurement', + values: [], + }, + ], + functions: [], + aggregateWindow: { + period: '', + }, + }, + hidden: false, + } + + draftState.draftQueries[ + draftState.activeQueryIndex + ] = invalidDraftQueryWithText + buildActiveQuery(draftState) + expect(draftState.draftQueries[draftState.activeQueryIndex].text).toEqual( + draftState.draftQueries[draftState.activeQueryIndex].text + ) + }) + }) }) diff --git a/ui/src/timeMachine/reducers/index.ts b/ui/src/timeMachine/reducers/index.ts index e42531c6be5..c4fbd37e9be 100644 --- a/ui/src/timeMachine/reducers/index.ts +++ b/ui/src/timeMachine/reducers/index.ts @@ -1025,12 +1025,12 @@ const initialQueryResultsState = (): QueryResultsState => ({ statuses: null, }) -const buildActiveQuery = (draftState: TimeMachineState) => { +export const buildActiveQuery = (draftState: TimeMachineState) => { const draftQuery = draftState.draftQueries[draftState.activeQueryIndex] if (isConfigValid(draftQuery.builderConfig)) { draftQuery.text = buildQuery(draftQuery.builderConfig) - } else { + } else if (!draftQuery.text) { draftQuery.text = '' } }