Skip to content

Commit

Permalink
[Lens] fix pie suggestions (#159902)
Browse files Browse the repository at this point in the history
## Summary

Fixes #159711

When dragging a numeric field to a pie with a metric and a group already
defined, we add another metric even if `allowMultipleMetric` setting is
not set.
I fixed it by making it work the same way as a heatmap when the setting
is off - replacing metric instead. Not sure if that's a correct approach
though (should we block the suggestion whatsover?) but definitely an
improvement.

---------

Co-authored-by: Stratoula Kalafateli <efstratia.kalafateli@elastic.co>
  • Loading branch information
mbondyra and stratoula authored Jun 20, 2023
1 parent 9e0c9a7 commit 4a099e1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -768,51 +768,63 @@ describe('suggestions', () => {
).toHaveLength(0);
});

it('should accept multiple metrics if active visualization', () => {
it('should accept multiple metrics if active visualization and allows multiple metrics', () => {
const props = {
table: {
layerId: 'first',
isMultiRow: true,
columns: [
{
columnId: 'a',
operation: { label: 'Top 5', dataType: 'string' as DataType, isBucketed: true },
},
{
columnId: 'b',
operation: { label: 'Top 5', dataType: 'string' as DataType, isBucketed: true },
},
{
columnId: 'c',
operation: { label: 'Top 5', dataType: 'string' as DataType, isBucketed: true },
},
{
columnId: 'd',
operation: { label: 'Avg', dataType: 'number' as DataType, isBucketed: false },
},
{
columnId: 'e',
operation: { label: 'Count', dataType: 'number' as DataType, isBucketed: false },
},
],
changeType: 'initial',
},
state: {
shape: PieChartTypes.TREEMAP,
layers: [
{
layerId: 'first',
layerType: layerTypes.DATA,
primaryGroups: ['a', 'b'],
metrics: ['e'],
numberDisplay: NumberDisplay.PERCENT,
categoryDisplay: CategoryDisplay.DEFAULT,
legendDisplay: LegendDisplay.DEFAULT,
},
],
},
keptLayerIds: ['first'],
} as SuggestionRequest<PieVisualizationState>;

// no suggestions if multiple metrics are not allowed
expect(suggestions(props)).toHaveLength(0);

// accepts suggestions if multiple metrics are allowed
expect(
suggestions({
table: {
layerId: 'first',
isMultiRow: true,
columns: [
{
columnId: 'a',
operation: { label: 'Top 5', dataType: 'string' as DataType, isBucketed: true },
},
{
columnId: 'b',
operation: { label: 'Top 5', dataType: 'string' as DataType, isBucketed: true },
},
{
columnId: 'c',
operation: { label: 'Top 5', dataType: 'string' as DataType, isBucketed: true },
},
{
columnId: 'd',
operation: { label: 'Avg', dataType: 'number' as DataType, isBucketed: false },
},
{
columnId: 'e',
operation: { label: 'Count', dataType: 'number' as DataType, isBucketed: false },
},
],
changeType: 'initial',
},
...props,
state: {
shape: PieChartTypes.TREEMAP,
layers: [
{
layerId: 'first',
layerType: layerTypes.DATA,
primaryGroups: ['a', 'b'],
metrics: ['e'],
numberDisplay: NumberDisplay.PERCENT,
categoryDisplay: CategoryDisplay.DEFAULT,
legendDisplay: LegendDisplay.DEFAULT,
},
],
},
keptLayerIds: ['first'],
...props.state,
layers: [{ ...props.state!.layers[0], allowMultipleMetrics: true }],
} as PieVisualizationState,
})
).toHaveLength(2);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export function suggestions({
return [];
}

if (metrics.length > 1 && !state?.layers[0].allowMultipleMetrics) {
return [];
}

const incompleteConfiguration = metrics.length === 0 || groups.length === 0;

if (incompleteConfiguration && state && !subVisualizationId) {
Expand Down

0 comments on commit 4a099e1

Please sign in to comment.