Skip to content

Commit

Permalink
[Lens] Fix error in query from generated suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
wylieconlon committed Apr 8, 2020
1 parent 184f594 commit 752472b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ describe('IndexPattern Data Source suggestions', () => {
columnOrder: ['cola', 'id1'],
columns: {
cola: initialState.layers.currentLayer.columns.cola,
id1: expect.objectContaining({
colb: expect.objectContaining({
operationType: 'avg',
sourceField: 'memory',
}),
Expand All @@ -839,6 +839,16 @@ describe('IndexPattern Data Source suggestions', () => {
);
});

it('updates references when replacing one metric with another', () => {
const initialState = stateWithNonEmptyTables();
const suggestions = getDatasourceSuggestionsForField(initialState, '1', {
name: 'memory',
type: 'number',
aggregatable: true,
searchable: true,
});
});

it('adds a metric column on a number field if no other metrics set', () => {
const initialState = stateWithNonEmptyTables();
const modifiedState: IndexPatternPrivateState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
operationDefinitionMap,
IndexPatternColumn,
} from './operations';
import { hasField } from './utils';
import { operationDefinitions } from './operations/definitions';
import { hasField } from './utils';
import {
IndexPattern,
IndexPatternPrivateState,
Expand Down Expand Up @@ -196,30 +196,29 @@ function addFieldAsMetricOperation(
suggestedPriority: undefined,
field,
});
const newColumnId = generateId();
const addedColumnId = generateId();

const [, metrics] = separateBucketColumns(layer);
const [buckets, metrics] = separateBucketColumns(layer);

// Add metrics if there are 0 or > 1 metric
if (metrics.length !== 1) {
return {
indexPatternId: indexPattern.id,
columns: {
...layer.columns,
[newColumnId]: newColumn,
[addedColumnId]: newColumn,
},
columnOrder: [...layer.columnOrder, newColumnId],
columnOrder: [...layer.columnOrder, addedColumnId],
};
}

// If only one metric, replace instead of add
const newColumns = { ...layer.columns, [newColumnId]: newColumn };
delete newColumns[metrics[0]];
// Replacing old column with new column, keeping the old ID
const newColumns = { ...layer.columns, [metrics[0]]: newColumn };

return {
indexPatternId: indexPattern.id,
columns: newColumns,
columnOrder: [...layer.columnOrder.filter(c => c !== metrics[0]), newColumnId],
columnOrder: layer.columnOrder, // Order is kept by replacing
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ describe('terms', () => {
expect(updatedColumn).toBe(initialColumn);
});

it('should switch to alphabetical ordering if the order column is removed', () => {
it('should switch to alphabetical ordering if there are no columns to order by', () => {
const termsColumn = termsOperation.onOtherColumnChanged!(
{
label: 'Top value of category',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const termsOperation: OperationDefinition<TermsIndexPatternColumn> = {
}
return currentColumn;
},
paramEditor: ({ state, setState, currentColumn, columnId: currentColumnId, layerId }) => {
paramEditor: ({ state, setState, currentColumn, layerId }) => {
const SEPARATOR = '$$$';
function toValue(orderBy: TermsIndexPatternColumn['params']['orderBy']) {
if (orderBy.type === 'alphabetical') {
Expand Down

0 comments on commit 752472b

Please sign in to comment.