Skip to content

Commit

Permalink
fix: show queries built in advanced mode when switching tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
hoorayimhelping committed Jan 7, 2020
1 parent e45de53 commit 56f3548
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ui/src/telegrafs/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ export const telegrafsReducer = (
return
}
}
})
})
111 changes: 111 additions & 0 deletions ui/src/timeMachine/reducers/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {createStore} from 'redux'

import {
buildActiveQuery,
initialState,
initialStateHelper,
timeMachinesReducer,
Expand Down Expand Up @@ -39,4 +40,114 @@ describe('the Time Machine reducer', () => {
)
})
})

describe('buildActiveQuery', () => {
let draftState

beforeEach(() => {
draftState = initialStateHelper()
})

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
)
})
})
})
4 changes: 2 additions & 2 deletions ui/src/timeMachine/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
}
}
Expand Down

0 comments on commit 56f3548

Please sign in to comment.