Skip to content

Commit

Permalink
🐛 Keep the custom label when transitioning to/from Formula
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed Oct 7, 2021
1 parent 0dcd3d3 commit f3ee5cc
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,122 @@ describe('state_helpers', () => {
}).columns.col1
).toEqual(expect.objectContaining({ label: 'Average of bytes' }));
});

it('should carry over a custom label when transitioning to a managed reference', () => {
expect(
replaceColumn({
layer: {
indexPatternId: '1',
columnOrder: ['col1', 'col2'],
columns: {
col1: {
label: 'MY CUSTOM LABEL',
customLabel: true,
dataType: 'string',
isBucketed: true,
operationType: 'terms',
sourceField: 'source',
params: {
orderBy: { type: 'alphabetical' },
orderDirection: 'asc',
size: 5,
},
},
},
},
indexPattern,
columnId: 'col1',
op: 'formula',
field: indexPattern.fields[2], // bytes field
visualizationGroups: [],
shouldResetLabel: undefined,
}).columns.col1
).toEqual(expect.objectContaining({ label: 'MY CUSTOM LABEL' }));
});

it('should overwrite the current label when transitioning to a managed reference operation when not custom', () => {
expect(
replaceColumn({
layer: {
indexPatternId: '1',
columnOrder: ['col1', 'col2'],
columns: {
col1: {
label: 'Average of bytes',
dataType: 'number',
isBucketed: false,
operationType: 'average',
sourceField: 'bytes',
},
},
},
indexPattern,
columnId: 'col1',
op: 'formula',
field: indexPattern.fields[2], // bytes field
visualizationGroups: [],
shouldResetLabel: undefined,
}).columns.col1
).toEqual(expect.objectContaining({ label: 'average(bytes)' }));
});

it('should carry over a custom label when transitioning from a managed reference', () => {
expect(
replaceColumn({
layer: {
indexPatternId: '1',
columnOrder: ['col1', 'col2'],
columns: {
col1: {
label: 'MY CUSTOM LABEL',
customLabel: true,
dataType: 'number',
operationType: 'formula',
isBucketed: false,
scale: 'ratio',
params: { isFormulaBroken: false, formula: 'average(bytes)' },
references: [],
},
},
},
indexPattern,
columnId: 'col1',
op: 'average',
field: indexPattern.fields[2], // bytes field
visualizationGroups: [],
shouldResetLabel: undefined,
}).columns.col1
).toEqual(expect.objectContaining({ label: 'MY CUSTOM LABEL' }));
});

it('should not carry over the managed reference default label to the new operation', () => {
expect(
replaceColumn({
layer: {
indexPatternId: '1',
columnOrder: ['col1', 'col2'],
columns: {
col1: {
label: 'average(bytes)',
customLabel: true,
dataType: 'number',
operationType: 'formula',
isBucketed: false,
scale: 'ratio',
params: { isFormulaBroken: false, formula: 'average(bytes)' },
references: [],
},
},
},
indexPattern,
columnId: 'col1',
op: 'average',
field: indexPattern.fields[2], // bytes field
visualizationGroups: [],
shouldResetLabel: undefined,
}).columns.col1
).toEqual(expect.objectContaining({ label: 'Average of bytes' }));
});
});

it('should execute adjustments for other columns', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,17 @@ export function replaceColumn({
field,
visualizationGroups,
});

// if the formula label is not the default one, propagate it to the new operation
if (
!shouldResetLabel &&
previousColumn.customLabel &&
previousColumn.label !==
previousDefinition.getDefaultLabel(previousColumn, indexPattern, tempLayer.columns)
) {
hypotheticalLayer.columns[columnId].customLabel = true;
hypotheticalLayer.columns[columnId].label = previousColumn.label;
}
if (hypotheticalLayer.incompleteColumns && hypotheticalLayer.incompleteColumns[columnId]) {
return {
...layer,
Expand Down Expand Up @@ -498,13 +509,10 @@ export function replaceColumn({
// TODO: Refactor all this to be more generic and know less about Formula
// if managed it has to look at the full picture to have a seamless transition
if (operationDefinition.input === 'managedReference') {
const newColumn = copyCustomLabel(
operationDefinition.buildColumn(
{ ...baseOptions, layer: tempLayer },
previousColumn.params,
operationDefinitionMap
),
previousColumn
const newColumn = operationDefinition.buildColumn(
{ ...baseOptions, layer: tempLayer },
previousColumn.params,
operationDefinitionMap
) as FormulaIndexPatternColumn;

// now remove the previous references
Expand Down Expand Up @@ -533,6 +541,17 @@ export function replaceColumn({
newLayer = basicLayer;
}

// when coming to Formula keep the custom label
const regeneratedColumn = newLayer.columns[columnId];
if (
!shouldResetLabel &&
regeneratedColumn.operationType !== previousColumn.operationType &&
previousColumn.customLabel
) {
regeneratedColumn.customLabel = true;
regeneratedColumn.label = previousColumn.label;
}

return updateDefaultLabels(
{
...tempLayer,
Expand Down

0 comments on commit f3ee5cc

Please sign in to comment.