Start breaking up the sadness in applyFuncsToFields
parent
2c77ea9b52
commit
73f35be7e9
|
@ -42,26 +42,14 @@ export const fill = (queryId, value) => ({
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
/*
|
export const removeFuncs = (queryID, fields, groupBy) => ({
|
||||||
// all fields implicitly have a function applied to them by default, unless
|
type: 'DE_REMOVE_FUNCS',
|
||||||
// it was explicitly removed previously, so set the auto group by time except
|
payload: {
|
||||||
// under that removal condition
|
queryID,
|
||||||
export const toggleFieldWithGroupByInterval = (queryID, fieldFunc) => (
|
fields,
|
||||||
dispatch,
|
groupBy,
|
||||||
getState
|
},
|
||||||
) => {
|
})
|
||||||
dispatch(toggleField(queryID, fieldFunc))
|
|
||||||
// toggleField determines whether to add a func, so now check state for funcs
|
|
||||||
// presence, and if present then apply default group by time
|
|
||||||
const updatedFieldFunc = getState().dataExplorerQueryConfigs[
|
|
||||||
queryID
|
|
||||||
].fields.find(({field}) => field === fieldFunc.field)
|
|
||||||
// updatedFieldFunc could be undefined if it was toggled for removal
|
|
||||||
if (updatedFieldFunc && updatedFieldFunc.funcs.length) {
|
|
||||||
dispatch(groupByTime(queryID, DEFAULT_DATA_EXPLORER_GROUP_BY_INTERVAL))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
export const applyFuncsToField = (queryId, fieldFunc) => ({
|
export const applyFuncsToField = (queryId, fieldFunc) => ({
|
||||||
type: 'DE_APPLY_FUNCS_TO_FIELD',
|
type: 'DE_APPLY_FUNCS_TO_FIELD',
|
||||||
|
|
|
@ -82,4 +82,4 @@ export const QUERY_TEMPLATES = [
|
||||||
{text: 'Show Diagnostics', query: 'SHOW DIAGNOSTICS'},
|
{text: 'Show Diagnostics', query: 'SHOW DIAGNOSTICS'},
|
||||||
]
|
]
|
||||||
|
|
||||||
export const DEFAULT_DATA_EXPLORER_GROUP_BY_INTERVAL = '10s'
|
export const INITIAL_GROUP_BY_TIME = '10s'
|
||||||
|
|
|
@ -2,18 +2,20 @@ import _ from 'lodash'
|
||||||
|
|
||||||
import defaultQueryConfig from 'src/utils/defaultQueryConfig'
|
import defaultQueryConfig from 'src/utils/defaultQueryConfig'
|
||||||
import {
|
import {
|
||||||
editRawText,
|
fill,
|
||||||
applyFuncsToField,
|
|
||||||
chooseMeasurement,
|
|
||||||
chooseNamespace,
|
|
||||||
chooseTag,
|
chooseTag,
|
||||||
groupByTag,
|
groupByTag,
|
||||||
|
removeFuncs,
|
||||||
groupByTime,
|
groupByTime,
|
||||||
toggleField,
|
toggleField,
|
||||||
toggleTagAcceptance,
|
editRawText,
|
||||||
fill,
|
|
||||||
updateRawQuery,
|
updateRawQuery,
|
||||||
|
chooseNamespace,
|
||||||
|
chooseMeasurement,
|
||||||
|
applyFuncsToField,
|
||||||
|
toggleTagAcceptance,
|
||||||
} from 'src/utils/queryTransitions'
|
} from 'src/utils/queryTransitions'
|
||||||
|
import {INITIAL_GROUP_BY_TIME} from 'src/data_explorer/constants'
|
||||||
|
|
||||||
const queryConfigs = (state = {}, action) => {
|
const queryConfigs = (state = {}, action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
@ -99,9 +101,11 @@ const queryConfigs = (state = {}, action) => {
|
||||||
|
|
||||||
case 'DE_APPLY_FUNCS_TO_FIELD': {
|
case 'DE_APPLY_FUNCS_TO_FIELD': {
|
||||||
const {queryId, fieldFunc} = action.payload
|
const {queryId, fieldFunc} = action.payload
|
||||||
const nextQueryConfig = applyFuncsToField(state[queryId], fieldFunc, {
|
const nextQueryConfig = applyFuncsToField(
|
||||||
preventAutoGroupBy: true,
|
state[queryId],
|
||||||
})
|
fieldFunc,
|
||||||
|
INITIAL_GROUP_BY_TIME
|
||||||
|
)
|
||||||
|
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
[queryId]: nextQueryConfig,
|
[queryId]: nextQueryConfig,
|
||||||
|
@ -151,6 +155,21 @@ const queryConfigs = (state = {}, action) => {
|
||||||
|
|
||||||
return {...state, ...nextState}
|
return {...state, ...nextState}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 'DE_REMOVE_FUNCS': {
|
||||||
|
const {queryID, fields, groupBy} = action.payload
|
||||||
|
|
||||||
|
// fields with no functions cannot have a group by time
|
||||||
|
const nextState = {
|
||||||
|
[queryID]: {
|
||||||
|
...state[queryID],
|
||||||
|
fields: removeFuncs(fields),
|
||||||
|
groupBy: {...groupBy, time: null},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return {...state, ...nextState}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,6 +64,19 @@ class FieldList extends Component {
|
||||||
this.props.onFill(fill)
|
this.props.onFill(fill)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleApplyFuncs = fieldFunc => {
|
||||||
|
const {removeFuncs, query, applyFuncsToField} = this.props
|
||||||
|
const {id, fields, groupBy} = query
|
||||||
|
const {field, funcs} = fieldFunc
|
||||||
|
|
||||||
|
// If one field has no funcs, all fields must have no funcs
|
||||||
|
if (!_.size(funcs)) {
|
||||||
|
return removeFuncs(fields, groupBy)
|
||||||
|
}
|
||||||
|
|
||||||
|
applyFuncsToField(fieldFunc)
|
||||||
|
}
|
||||||
|
|
||||||
_getFields = () => {
|
_getFields = () => {
|
||||||
const {database, measurement, retentionPolicy} = this.props.query
|
const {database, measurement, retentionPolicy} = this.props.query
|
||||||
const {source} = this.context
|
const {source} = this.context
|
||||||
|
@ -90,7 +103,6 @@ class FieldList extends Component {
|
||||||
isKapacitorRule,
|
isKapacitorRule,
|
||||||
isInDataExplorer,
|
isInDataExplorer,
|
||||||
onToggleField,
|
onToggleField,
|
||||||
applyFuncsToField,
|
|
||||||
} = this.props
|
} = this.props
|
||||||
|
|
||||||
const hasAggregates = numFunctions(fields) > 0
|
const hasAggregates = numFunctions(fields) > 0
|
||||||
|
@ -136,7 +148,7 @@ class FieldList extends Component {
|
||||||
<FieldListItem
|
<FieldListItem
|
||||||
key={i}
|
key={i}
|
||||||
onToggleField={onToggleField}
|
onToggleField={onToggleField}
|
||||||
onApplyFuncsToField={applyFuncsToField}
|
onApplyFuncsToField={this.handleApplyFuncs}
|
||||||
isSelected={!!selectedFields.length}
|
isSelected={!!selectedFields.length}
|
||||||
fieldFuncs={
|
fieldFuncs={
|
||||||
selectedFields.length ? selectedFields : [fieldFunc]
|
selectedFields.length ? selectedFields : [fieldFunc]
|
||||||
|
@ -184,6 +196,7 @@ FieldList.propTypes = {
|
||||||
proxy: string.isRequired,
|
proxy: string.isRequired,
|
||||||
}).isRequired,
|
}).isRequired,
|
||||||
}),
|
}),
|
||||||
|
removeFuncs: func.isRequired,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default FieldList
|
export default FieldList
|
||||||
|
|
|
@ -4,7 +4,8 @@ import DatabaseList from 'src/shared/components/DatabaseList'
|
||||||
import MeasurementList from 'src/shared/components/MeasurementList'
|
import MeasurementList from 'src/shared/components/MeasurementList'
|
||||||
import FieldList from 'src/shared/components/FieldList'
|
import FieldList from 'src/shared/components/FieldList'
|
||||||
|
|
||||||
const actionBinder = (id, action) => item => action(id, item)
|
const actionBinder = (id, action) => (item, ...args) =>
|
||||||
|
action(id, item, ...args)
|
||||||
|
|
||||||
const SchemaExplorer = ({
|
const SchemaExplorer = ({
|
||||||
query,
|
query,
|
||||||
|
@ -19,6 +20,7 @@ const SchemaExplorer = ({
|
||||||
chooseNamespace,
|
chooseNamespace,
|
||||||
chooseMeasurement,
|
chooseMeasurement,
|
||||||
applyFuncsToField,
|
applyFuncsToField,
|
||||||
|
removeFuncs,
|
||||||
toggleTagAcceptance,
|
toggleTagAcceptance,
|
||||||
},
|
},
|
||||||
}) =>
|
}) =>
|
||||||
|
@ -45,6 +47,7 @@ const SchemaExplorer = ({
|
||||||
onFill={actionBinder(id, fill)}
|
onFill={actionBinder(id, fill)}
|
||||||
onGroupByTime={actionBinder(id, groupByTime)}
|
onGroupByTime={actionBinder(id, groupByTime)}
|
||||||
applyFuncsToField={actionBinder(id, applyFuncsToField)}
|
applyFuncsToField={actionBinder(id, applyFuncsToField)}
|
||||||
|
removeFuncs={actionBinder(id, removeFuncs)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
import defaultQueryConfig from 'utils/defaultQueryConfig'
|
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 {
|
import {
|
||||||
hasField,
|
hasField,
|
||||||
removeField,
|
removeField,
|
||||||
|
getFieldsDeep,
|
||||||
getFuncsByFieldName,
|
getFuncsByFieldName,
|
||||||
} from 'shared/reducers/helpers/fields'
|
} from 'shared/reducers/helpers/fields'
|
||||||
import _ from 'lodash'
|
import _ from 'lodash'
|
||||||
|
@ -136,21 +135,10 @@ export function toggleTagAcceptance(query) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function applyFuncsToField(
|
export const removeFuncs = fields => getFieldsDeep(fields)
|
||||||
query,
|
|
||||||
{field, funcs = []},
|
|
||||||
{preventAutoGroupBy = false} = {}
|
|
||||||
) {
|
|
||||||
const shouldRemoveFuncs = funcs.length === 0
|
|
||||||
const nextFields = query.fields.reduce((acc, f) => {
|
|
||||||
// If one field has no funcs, all fields must have no funcs
|
|
||||||
if (shouldRemoveFuncs) {
|
|
||||||
return _.uniq(
|
|
||||||
[...acc, ...f.args.filter(a => a.type === 'field')],
|
|
||||||
fld => fld.name
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
export const applyFuncsToField = (query, {field, funcs = []}, time) => {
|
||||||
|
const nextFields = query.fields.reduce((acc, f) => {
|
||||||
// If there is a func applied to only one field, add it to the other fields
|
// If there is a func applied to only one field, add it to the other fields
|
||||||
if (f.type === 'field') {
|
if (f.type === 'field') {
|
||||||
return [
|
return [
|
||||||
|
@ -194,15 +182,11 @@ export function applyFuncsToField(
|
||||||
return [...acc, f]
|
return [...acc, f]
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const defaultGroupBy = preventAutoGroupBy
|
return {
|
||||||
? DEFAULT_DATA_EXPLORER_GROUP_BY_INTERVAL
|
...query,
|
||||||
: DEFAULT_DASHBOARD_GROUP_BY_INTERVAL
|
fields: _.flatten(nextFields),
|
||||||
|
groupBy: {...query.groupBy, time},
|
||||||
// If there are no functions, then there should be no GROUP BY time
|
}
|
||||||
const nextTime = shouldRemoveFuncs ? null : defaultGroupBy
|
|
||||||
const nextGroupBy = {...query.groupBy, time: nextTime}
|
|
||||||
|
|
||||||
return {...query, fields: _.flatten(nextFields), groupBy: nextGroupBy}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateRawQuery(query, rawText) {
|
export function updateRawQuery(query, rawText) {
|
||||||
|
|
Loading…
Reference in New Issue