Update queryTransitions to use field helpers

pull/2128/head
Chris Goller 2017-10-11 15:38:20 -05:00 committed by Andrew Watkins
parent 14f7b16f50
commit ecc3f2ccb7
2 changed files with 53 additions and 2 deletions

View File

@ -28,3 +28,32 @@ export const fieldNamesDeep = fields =>
// firstFieldName returns the name of the first of type field
export const firstFieldName = fields => _.head(fieldNamesDeep(fields))
export const hasField = (fieldName, fields) =>
fieldNamesDeep(fields).some(f => f === fieldName)
// everyOfType returns true if all top-level field types are type
export const everyOfType = (fields, type) =>
fields.every(f => _.get(f, 'type') === type)
// everyField returns if all top-level field types are field
export const everyField = fields => everyOfType(fields, 'field')
// everyFunction returns if all top-level field types are functions
export const everyFunction = fields => everyOfType(fields, 'func')
// removeField will remove the field or function from the field list with the
// given fieldName
export const removeField = (fieldName, fields) => {
if (everyField(fields)) {
return fields.filter(f => f.name !== fieldName)
}
return fields.reduce((acc, f) => {
const has = fieldNamesDeep(f.args).some(n => n.name === fieldName)
if (has) {
return [...acc, f]
}
return acc
}, [])
}

View File

@ -1,7 +1,7 @@
import defaultQueryConfig from 'utils/defaultQueryConfig'
import {DEFAULT_DASHBOARD_GROUP_BY_INTERVAL} from 'shared/constants'
import {DEFAULT_DATA_EXPLORER_GROUP_BY_INTERVAL} from 'src/data_explorer/constants'
import {NULL_STRING} from 'shared/constants/queryFillOptions'
import {hasField, removeField} from 'utils/fields'
import _ from 'lodash'
export function editRawText(query, rawText) {
@ -48,8 +48,30 @@ export const toggleField = (query, {name, type}, isKapacitorRule = false) => {
}
}
const isSelected = hasField(name, fields)
const newFuncs = fields.filter(f => f.type === 'func')
if (isSelected) {
// if list is all fields, remove that field
// if list is all funcs,. remove all funcs that match
const newFields = removeField(name, fields)
if (!newFields.length) {
return {
...query,
groupBy: {
...groupBy,
time: null,
},
fields: [],
}
}
return {
...query,
fields: removeField(name, fields),
}
}
if (!newFuncs) {
return {
...query,
@ -127,7 +149,7 @@ export function toggleTagAcceptance(query) {
export function applyFuncsToField(
query,
{field, funcs = []},
{preventAutoGroupBy = false, isKapacitorRule = false} = {}
{preventAutoGroupBy = false} = {}
) {
const shouldRemoveFuncs = funcs.length === 0
const nextFields = query.fields.reduce((acc, f) => {