Skip to content

Commit

Permalink
[Lens] Fixes broken charts with terms agg and no dataview (elastic#16…
Browse files Browse the repository at this point in the history
…9503)

## Summary

In Lens when a dataview is missing, the editing should be allowed. There
are cases which it doesn't. The easiest to reproduce is to create a
visualization with terms and remove the dataview. Then you will see an
error (cannot getFieldByName of undefined) and editing and changing the
dataview is not allowed.

This PR fixes this and all other occurences.

### Checklist
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
stratoula authored Oct 25, 2023
1 parent 6f22eef commit 33c409b
Show file tree
Hide file tree
Showing 26 changed files with 73 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1087,8 +1087,8 @@ export function DimensionEditor(props: DimensionEditorProps) {
selectedColumn &&
operationDefinitionMap[selectedColumn.operationType].getDefaultLabel(
selectedColumn,
props.indexPatterns[state.layers[layerId].indexPatternId],
state.layers[layerId].columns
state.layers[layerId].columns,
props.indexPatterns[state.layers[layerId].indexPatternId]
)
),
[layerId, selectedColumn, props.indexPatterns, state.layers]
Expand Down Expand Up @@ -1263,8 +1263,8 @@ export function DimensionEditor(props: DimensionEditorProps) {
customLabel:
operationDefinitionMap[selectedColumn.operationType].getDefaultLabel(
selectedColumn,
props.indexPatterns[state.layers[layerId].indexPatternId],
state.layers[layerId].columns
state.layers[layerId].columns,
props.indexPatterns[state.layers[layerId].indexPatternId]
) !== value,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ export function getFormBasedDatasource({
? column.label
: operationDefinitionMap[column.operationType].getDefaultLabel(
column,
indexPatternsMap[layer.indexPatternId],
layer.columns
layer.columns,
indexPatternsMap[layer.indexPatternId]
)
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ export const counterRateOperation: OperationDefinition<
};
}
},
getDefaultLabel: (column, indexPattern, columns) => {
getDefaultLabel: (column, columns, indexPattern) => {
const ref = columns[column.references[0]];
return ofName(
ref && 'sourceField' in ref
? indexPattern.getFieldByName(ref.sourceField)?.displayName
? indexPattern?.getFieldByName(ref.sourceField)?.displayName
: undefined,
column.timeScale,
column.timeShift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ export const cumulativeSumOperation: OperationDefinition<
};
}
},
getDefaultLabel: (column, indexPattern, columns) => {
getDefaultLabel: (column, columns, indexPattern) => {
const ref = columns[column.references[0]];
return ofName(
ref && 'sourceField' in ref
? indexPattern.getFieldByName(ref.sourceField)?.displayName
? indexPattern?.getFieldByName(ref.sourceField)?.displayName
: undefined,
undefined,
column.timeShift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const derivativeOperation: OperationDefinition<
};
}
},
getDefaultLabel: (column, indexPattern, columns) => {
getDefaultLabel: (column, columns, indexPattern) => {
return ofName(columns[column.references[0]]?.label, column.timeScale, column.timeShift);
},
toExpression: (layer, columnId) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const movingAverageOperation: OperationDefinition<
};
}
},
getDefaultLabel: (column, indexPattern, columns) => {
getDefaultLabel: (column, columns, indexPattern) => {
return ofName(columns[column.references[0]]?.label, column.timeScale, column.timeShift);
},
toExpression: (layer, columnId) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ function buildOverallMetricOperation<T extends OverallMetricIndexPatternColumn<s
scale: 'ratio',
};
},
getDefaultLabel: (column, indexPattern, columns) => {
getDefaultLabel: (column, columns, indexPattern) => {
const ref = columns[column.references[0]];
return ofName(
ref && 'sourceField' in ref
? indexPattern.getFieldByName(ref.sourceField)?.displayName
? indexPattern?.getFieldByName(ref.sourceField)?.displayName
: undefined
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const timeScaleOperation: OperationDefinition<TimeScaleIndexPatternColumn
scale: 'ratio',
};
},
getDefaultLabel: (column, indexPattern, columns) => {
getDefaultLabel: (column, columns, indexPattern) => {
return 'normalize_by_unit';
},
toExpression: (layer, columnId) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const cardinalityOperation: OperationDefinition<
filterable: true,
shiftable: true,
canReduceTimeRange: true,
getDefaultLabel: (column, indexPattern) =>
getDefaultLabel: (column, columns, indexPattern) =>
ofName(
getSafeName(column.sourceField, indexPattern),
column.timeShift,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const countOperation: OperationDefinition<CountIndexPatternColumn, 'field
return { dataType: 'number', isBucketed: IS_BUCKETED, scale: SCALE };
}
},
getDefaultLabel: (column, indexPattern) => {
getDefaultLabel: (column, columns, indexPattern) => {
const field = indexPattern?.getFieldByName(column.sourceField);
return ofName(field, column.timeShift, column.timeScale, column.reducedTimeRange);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,6 @@ describe('date_histogram', () => {
sourceField: 'missing',
params: { interval: 'auto' },
},
indexPattern1,
{
col1: {
label: '',
Expand All @@ -786,7 +785,8 @@ describe('date_histogram', () => {
sourceField: 'missing',
params: { interval: 'auto' },
} as DateHistogramIndexPatternColumn,
}
},
indexPattern1
)
).toEqual('Missing field');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const dateHistogramOperation: OperationDefinition<
};
}
},
getDefaultLabel: (column, indexPattern) => getSafeName(column.sourceField, indexPattern),
getDefaultLabel: (column, columns, indexPattern) => getSafeName(column.sourceField, indexPattern),
buildColumn({ field }, columnParams) {
return {
label: field.displayName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const formulaOperation: OperationDefinition<FormulaIndexPatternColumn, 'm
{
type: 'formula',
displayName: defaultLabel,
getDefaultLabel: (column, indexPattern) => column.params.formula ?? defaultLabel,
getDefaultLabel: (column) => column.params.formula ?? defaultLabel,
input: 'managedReference',
hidden: true,
filterable: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const mathOperation: OperationDefinition<MathIndexPatternColumn, 'managed
type: 'math',
displayName: 'Math',
hidden: true,
getDefaultLabel: (column, indexPattern) => 'Math',
getDefaultLabel: (column, columns, indexPattern) => 'Math',
input: 'managedReference',
getDisabledStatus(indexPattern: IndexPattern) {
return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ interface BaseOperationDefinitionProps<
*/
getDefaultLabel: (
column: C,
indexPattern: IndexPattern,
columns: Record<string, GenericIndexPatternColumn>
columns: Record<string, GenericIndexPatternColumn>,
indexPattern?: IndexPattern
) => string;
/**
* This function is called if another column in the same layer changed or got added/removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export const lastValueOperation: OperationDefinition<
displayName: i18n.translate('xpack.lens.indexPattern.lastValue', {
defaultMessage: 'Last value',
}),
getDefaultLabel: (column, indexPattern) =>
getDefaultLabel: (column, columns, indexPattern) =>
ofName(
getSafeName(column.sourceField, indexPattern),
column.timeShift,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function buildMetricOperation<T extends MetricColumn<string>>({
(!newField.aggregationRestrictions || newField.aggregationRestrictions![type])
);
},
getDefaultLabel: (column, indexPattern, columns) =>
getDefaultLabel: (column, columns, indexPattern) =>
labelLookup(getSafeName(column.sourceField, indexPattern), column),
buildColumn: ({ field, previousColumn }, columnParams) => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const percentileOperation: OperationDefinition<
(!newField.aggregationRestrictions || !newField.aggregationRestrictions.percentiles)
);
},
getDefaultLabel: (column, indexPattern, columns) =>
getDefaultLabel: (column, columns, indexPattern) =>
ofName(
getSafeName(column.sourceField, indexPattern),
column.params.percentile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const percentileRanksOperation: OperationDefinition<
(!newField.aggregationRestrictions || !newField.aggregationRestrictions.percentile_ranks)
);
},
getDefaultLabel: (column, indexPattern, columns) =>
getDefaultLabel: (column, columns, indexPattern) =>
ofName(
getSafeName(column.sourceField, indexPattern),
column.params.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ export const rangeOperation: OperationDefinition<
};
}
},
getDefaultLabel: (column, indexPattern) =>
indexPattern.getFieldByName(column.sourceField)?.displayName ??
getDefaultLabel: (column, columns, indexPattern) =>
indexPattern?.getFieldByName(column.sourceField)?.displayName ??
i18n.translate('xpack.lens.indexPattern.missingFieldLabel', {
defaultMessage: 'Missing field',
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ describe('static_value', () => {
value: '23',
},
},
createMockedIndexPattern(),
layer.columns
layer.columns,
createMockedIndexPattern()
)
).toBe('Static value: 23');
});
Expand All @@ -142,8 +142,8 @@ describe('static_value', () => {
value: '',
},
},
createMockedIndexPattern(),
layer.columns
layer.columns,
createMockedIndexPattern()
)
).toBe('Static value');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,9 @@ export const termsOperation: OperationDefinition<
}),
}).toAst();
},
getDefaultLabel: (column, indexPattern) =>
getDefaultLabel: (column, columns, indexPattern) =>
ofName(
indexPattern.getFieldByName(column.sourceField)?.displayName,
indexPattern?.getFieldByName(column.sourceField)?.displayName,
column.params.secondaryFields?.length,
column.params.orderBy.type === 'rare',
column.params.orderBy.type === 'significant',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1177,12 +1177,34 @@ describe('terms', () => {
},
sourceField: 'source',
} as TermsIndexPatternColumn,
createMockedIndexPattern(),
{}
{},
createMockedIndexPattern()
)
).toBe('Top 3 values of source');
});

it('should not fail if dataview is not given', () => {
expect(
termsOperation.getDefaultLabel(
{
dataType: 'string',
isBucketed: true,

// Private
operationType: 'terms',
params: {
orderBy: { type: 'alphabetical', fallback: true },
size: 3,
orderDirection: 'asc',
},
sourceField: 'source',
} as TermsIndexPatternColumn,
{},
undefined
)
).toBe('Top 3 values of Missing field');
});

it('should return main value with single counter for two fields', () => {
expect(
termsOperation.getDefaultLabel(
Expand All @@ -1200,8 +1222,8 @@ describe('terms', () => {
},
sourceField: 'source',
} as TermsIndexPatternColumn,
createMockedIndexPattern(),
{}
{},
createMockedIndexPattern()
)
).toBe('Top values of source + 1 other');
});
Expand All @@ -1223,8 +1245,8 @@ describe('terms', () => {
},
sourceField: 'source',
} as TermsIndexPatternColumn,
createMockedIndexPattern(),
{}
{},
createMockedIndexPattern()
)
).toBe('Top values of source + 2 others');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2577,7 +2577,6 @@ describe('state_helpers', () => {
operationType: 'testReference',
references: ['col1'],
},
indexPattern,
{
col2: {
label: 'Default label',
Expand All @@ -2586,7 +2585,8 @@ describe('state_helpers', () => {
operationType: 'testReference',
references: ['col1'],
},
}
},
indexPattern
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ export function replaceColumn({
previousColumn.customLabel &&
hypotheticalLayer.columns[columnId] &&
previousColumn.label !==
previousDefinition.getDefaultLabel(previousColumn, indexPattern, tempLayer.columns)
previousDefinition.getDefaultLabel(previousColumn, tempLayer.columns, indexPattern)
) {
hypotheticalLayer.columns[columnId].customLabel = true;
hypotheticalLayer.columns[columnId].label = previousColumn.label;
Expand Down Expand Up @@ -1723,8 +1723,8 @@ export function updateDefaultLabels(
...col,
label: operationDefinitionMap[col.operationType].getDefaultLabel(
col,
indexPattern,
copiedColumns
copiedColumns,
indexPattern
),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ function getExpressionForLayer(
? col.label
: operationDefinitionMap[col.operationType].getDefaultLabel(
col,
indexPattern,
layer.columns
layer.columns,
indexPattern
),
},
];
Expand Down Expand Up @@ -398,8 +398,8 @@ function getExpressionForLayer(
? col.label
: operationDefinitionMap[col.operationType].getDefaultLabel(
col,
indexPattern,
layer.columns
layer.columns,
indexPattern
),
],
targetUnit: [col.timeScale!],
Expand Down

0 comments on commit 33c409b

Please sign in to comment.