diff --git a/ui/spec/data_explorer/reducers/queryConfigSpec.js b/ui/spec/data_explorer/reducers/queryConfigSpec.js index 85d4874e8e..f0df692ed6 100644 --- a/ui/spec/data_explorer/reducers/queryConfigSpec.js +++ b/ui/spec/data_explorer/reducers/queryConfigSpec.js @@ -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]: { diff --git a/ui/src/utils/queryTransitions.js b/ui/src/utils/queryTransitions.js index b9e93e1a93..80eeefb45c 100644 --- a/ui/src/utils/queryTransitions.js +++ b/ui/src/utils/queryTransitions.js @@ -133,38 +133,53 @@ 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 { - name: func.name, - type: func.type, - args: [{name: f.name, type: 'field'}], - alias: `${name}_${f.name}`, - } - }) + 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) - ) + // 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 + } - const modifiedFields = funcs.map(func => { - return {...func, args: [field], alias: `${func.name}_${field.name}`} - }) + return [ + ...acc2, + { + ...func, + args: [field], + alias: `${func.name}_${field.name}`, + }, + ] + }, []) - nextFields = [...nextFields, ...modifiedFields] - } + // console.log('newFuncs: ', newFuncs) + return [...acc, ...newFuncs] + } + + return [...acc, f] + }, []) const defaultGroupBy = preventAutoGroupBy ? DEFAULT_DATA_EXPLORER_GROUP_BY_INTERVAL