Skip to content

Commit f3ee5cc

Browse files
committed
🐛 Keep the custom label when transitioning to/from Formula
1 parent 0dcd3d3 commit f3ee5cc

File tree

2 files changed

+142
-7
lines changed

2 files changed

+142
-7
lines changed

x-pack/plugins/lens/public/indexpattern_datasource/operations/layer_helpers.test.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,122 @@ describe('state_helpers', () => {
11391139
}).columns.col1
11401140
).toEqual(expect.objectContaining({ label: 'Average of bytes' }));
11411141
});
1142+
1143+
it('should carry over a custom label when transitioning to a managed reference', () => {
1144+
expect(
1145+
replaceColumn({
1146+
layer: {
1147+
indexPatternId: '1',
1148+
columnOrder: ['col1', 'col2'],
1149+
columns: {
1150+
col1: {
1151+
label: 'MY CUSTOM LABEL',
1152+
customLabel: true,
1153+
dataType: 'string',
1154+
isBucketed: true,
1155+
operationType: 'terms',
1156+
sourceField: 'source',
1157+
params: {
1158+
orderBy: { type: 'alphabetical' },
1159+
orderDirection: 'asc',
1160+
size: 5,
1161+
},
1162+
},
1163+
},
1164+
},
1165+
indexPattern,
1166+
columnId: 'col1',
1167+
op: 'formula',
1168+
field: indexPattern.fields[2], // bytes field
1169+
visualizationGroups: [],
1170+
shouldResetLabel: undefined,
1171+
}).columns.col1
1172+
).toEqual(expect.objectContaining({ label: 'MY CUSTOM LABEL' }));
1173+
});
1174+
1175+
it('should overwrite the current label when transitioning to a managed reference operation when not custom', () => {
1176+
expect(
1177+
replaceColumn({
1178+
layer: {
1179+
indexPatternId: '1',
1180+
columnOrder: ['col1', 'col2'],
1181+
columns: {
1182+
col1: {
1183+
label: 'Average of bytes',
1184+
dataType: 'number',
1185+
isBucketed: false,
1186+
operationType: 'average',
1187+
sourceField: 'bytes',
1188+
},
1189+
},
1190+
},
1191+
indexPattern,
1192+
columnId: 'col1',
1193+
op: 'formula',
1194+
field: indexPattern.fields[2], // bytes field
1195+
visualizationGroups: [],
1196+
shouldResetLabel: undefined,
1197+
}).columns.col1
1198+
).toEqual(expect.objectContaining({ label: 'average(bytes)' }));
1199+
});
1200+
1201+
it('should carry over a custom label when transitioning from a managed reference', () => {
1202+
expect(
1203+
replaceColumn({
1204+
layer: {
1205+
indexPatternId: '1',
1206+
columnOrder: ['col1', 'col2'],
1207+
columns: {
1208+
col1: {
1209+
label: 'MY CUSTOM LABEL',
1210+
customLabel: true,
1211+
dataType: 'number',
1212+
operationType: 'formula',
1213+
isBucketed: false,
1214+
scale: 'ratio',
1215+
params: { isFormulaBroken: false, formula: 'average(bytes)' },
1216+
references: [],
1217+
},
1218+
},
1219+
},
1220+
indexPattern,
1221+
columnId: 'col1',
1222+
op: 'average',
1223+
field: indexPattern.fields[2], // bytes field
1224+
visualizationGroups: [],
1225+
shouldResetLabel: undefined,
1226+
}).columns.col1
1227+
).toEqual(expect.objectContaining({ label: 'MY CUSTOM LABEL' }));
1228+
});
1229+
1230+
it('should not carry over the managed reference default label to the new operation', () => {
1231+
expect(
1232+
replaceColumn({
1233+
layer: {
1234+
indexPatternId: '1',
1235+
columnOrder: ['col1', 'col2'],
1236+
columns: {
1237+
col1: {
1238+
label: 'average(bytes)',
1239+
customLabel: true,
1240+
dataType: 'number',
1241+
operationType: 'formula',
1242+
isBucketed: false,
1243+
scale: 'ratio',
1244+
params: { isFormulaBroken: false, formula: 'average(bytes)' },
1245+
references: [],
1246+
},
1247+
},
1248+
},
1249+
indexPattern,
1250+
columnId: 'col1',
1251+
op: 'average',
1252+
field: indexPattern.fields[2], // bytes field
1253+
visualizationGroups: [],
1254+
shouldResetLabel: undefined,
1255+
}).columns.col1
1256+
).toEqual(expect.objectContaining({ label: 'Average of bytes' }));
1257+
});
11421258
});
11431259

11441260
it('should execute adjustments for other columns', () => {

x-pack/plugins/lens/public/indexpattern_datasource/operations/layer_helpers.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,17 @@ export function replaceColumn({
415415
field,
416416
visualizationGroups,
417417
});
418+
419+
// if the formula label is not the default one, propagate it to the new operation
420+
if (
421+
!shouldResetLabel &&
422+
previousColumn.customLabel &&
423+
previousColumn.label !==
424+
previousDefinition.getDefaultLabel(previousColumn, indexPattern, tempLayer.columns)
425+
) {
426+
hypotheticalLayer.columns[columnId].customLabel = true;
427+
hypotheticalLayer.columns[columnId].label = previousColumn.label;
428+
}
418429
if (hypotheticalLayer.incompleteColumns && hypotheticalLayer.incompleteColumns[columnId]) {
419430
return {
420431
...layer,
@@ -498,13 +509,10 @@ export function replaceColumn({
498509
// TODO: Refactor all this to be more generic and know less about Formula
499510
// if managed it has to look at the full picture to have a seamless transition
500511
if (operationDefinition.input === 'managedReference') {
501-
const newColumn = copyCustomLabel(
502-
operationDefinition.buildColumn(
503-
{ ...baseOptions, layer: tempLayer },
504-
previousColumn.params,
505-
operationDefinitionMap
506-
),
507-
previousColumn
512+
const newColumn = operationDefinition.buildColumn(
513+
{ ...baseOptions, layer: tempLayer },
514+
previousColumn.params,
515+
operationDefinitionMap
508516
) as FormulaIndexPatternColumn;
509517

510518
// now remove the previous references
@@ -533,6 +541,17 @@ export function replaceColumn({
533541
newLayer = basicLayer;
534542
}
535543

544+
// when coming to Formula keep the custom label
545+
const regeneratedColumn = newLayer.columns[columnId];
546+
if (
547+
!shouldResetLabel &&
548+
regeneratedColumn.operationType !== previousColumn.operationType &&
549+
previousColumn.customLabel
550+
) {
551+
regeneratedColumn.customLabel = true;
552+
regeneratedColumn.label = previousColumn.label;
553+
}
554+
536555
return updateDefaultLabels(
537556
{
538557
...tempLayer,

0 commit comments

Comments
 (0)