Put add funcs to fields in one reducer function
parent
0c72084fd8
commit
55d29f89e3
|
@ -216,14 +216,14 @@ describe('Chronograf.Reducers.DataExplorer.queryConfigs', () => {
|
|||
|
||||
const nextState = reducer(initialState, action)
|
||||
|
||||
console.log('foo: ', JSON.stringify(nextState[queryId].fields, null, 2))
|
||||
expect(nextState[queryId].fields).to.deep.equal([
|
||||
{name: 'fn1', type: 'func', args: [f2], alias: `fn1_${f2.name}`},
|
||||
{name: 'fn3', type: 'func', args: [f1], alias: `fn3_${f1.name}`},
|
||||
{name: 'fn4', type: 'func', args: [f1], alias: `fn4_${f1.name}`},
|
||||
{name: 'fn1', type: 'func', args: [f2], alias: `fn1_${f2.name}`},
|
||||
])
|
||||
})
|
||||
|
||||
// TODO: start here 10/10/2017
|
||||
it('removes all functions and group by time when one field has no funcs applied', () => {
|
||||
const initialState = {
|
||||
[queryId]: {
|
||||
|
|
|
@ -133,39 +133,54 @@ export function applyFuncsToField(
|
|||
{preventAutoGroupBy = false, isKapacitorRule = false} = {}
|
||||
) {
|
||||
const shouldRemoveFuncs = funcs && funcs.length === 0
|
||||
let nextFields = query.fields.map(f => {
|
||||
const nextFields = query.fields.reduce((acc, f) => {
|
||||
// If one field has no funcs, all fields must have no funcs
|
||||
if (shouldRemoveFuncs) {
|
||||
return f.args.filter(a => a.type === 'field')
|
||||
return [...acc, f.args.filter(a => a.type === 'field')]
|
||||
}
|
||||
|
||||
// If there is a func applied to only one field, add it to the other fields
|
||||
if (f.type === 'field') {
|
||||
return funcs.map(func => {
|
||||
return [
|
||||
...acc,
|
||||
funcs.map(func => {
|
||||
return {
|
||||
name: func.name,
|
||||
type: func.type,
|
||||
args: [{name: f.name, type: 'field'}],
|
||||
alias: `${name}_${f.name}`,
|
||||
}
|
||||
})
|
||||
}),
|
||||
]
|
||||
}
|
||||
|
||||
return f
|
||||
})
|
||||
const isFieldToChange = f.args.find(a => a.name === field.name)
|
||||
|
||||
if (!shouldRemoveFuncs) {
|
||||
nextFields = query.fields.filter(f =>
|
||||
f.args.find(a => a.name !== field.name)
|
||||
)
|
||||
|
||||
const modifiedFields = funcs.map(func => {
|
||||
return {...func, args: [field], alias: `${func.name}_${field.name}`}
|
||||
})
|
||||
|
||||
nextFields = [...nextFields, ...modifiedFields]
|
||||
// Apply new funcs to field
|
||||
if (isFieldToChange) {
|
||||
const newFuncs = funcs.reduce((acc2, func) => {
|
||||
const isDup = acc.find(a => a.name === func.name)
|
||||
if (isDup) {
|
||||
return acc2
|
||||
}
|
||||
|
||||
return [
|
||||
...acc2,
|
||||
{
|
||||
...func,
|
||||
args: [field],
|
||||
alias: `${func.name}_${field.name}`,
|
||||
},
|
||||
]
|
||||
}, [])
|
||||
|
||||
// console.log('newFuncs: ', newFuncs)
|
||||
return [...acc, ...newFuncs]
|
||||
}
|
||||
|
||||
return [...acc, f]
|
||||
}, [])
|
||||
|
||||
const defaultGroupBy = preventAutoGroupBy
|
||||
? DEFAULT_DATA_EXPLORER_GROUP_BY_INTERVAL
|
||||
: DEFAULT_DASHBOARD_GROUP_BY_INTERVAL
|
||||
|
|
Loading…
Reference in New Issue