[Lens] clear metric visualization state (#139154)

* clean up state when dimensions removed and layer cleared

* validate max accessor

* simplify some logic
This commit is contained in:
Andrew Tate 2022-08-19 13:32:01 -05:00 committed by GitHub
parent 2b15e2a4f3
commit 0488b46aa3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 27 deletions

View file

@ -102,6 +102,7 @@ export const metricVisFunction = (): MetricVisExpressionFunctionDefinition => ({
fn(input, args, handlers) {
validateAccessor(args.metric, input.columns);
validateAccessor(args.secondaryMetric, input.columns);
validateAccessor(args.max, input.columns);
validateAccessor(args.breakdownBy, input.columns);
if (handlers?.inspectorAdapters?.tables) {

View file

@ -160,16 +160,11 @@ const getColor = (
data: Datatable,
rowNumber: number
) => {
let minBound = paletteParams.rangeMin;
let maxBound = paletteParams.rangeMax;
const { min, max } = getDataBoundsForPalette(accessors, data, rowNumber);
minBound = min;
maxBound = max;
return getPaletteService().get(CUSTOM_PALETTE)?.getColorForValue?.(value, paletteParams, {
min: minBound,
max: maxBound,
min,
max,
});
};

View file

@ -517,12 +517,8 @@ describe('metric visualization', () => {
it('clears a layer', () => {
expect(visualization.clearLayer(fullState, 'some-id')).toMatchInlineSnapshot(`
Object {
"color": "static-color",
"layerId": "first",
"layerType": "data",
"maxCols": 5,
"progressDirection": "vertical",
"subtitle": "subtitle",
}
`);
});
@ -634,6 +630,7 @@ describe('metric visualization', () => {
expect(removed).not.toHaveProperty('metricAccessor');
expect(removed).not.toHaveProperty('palette');
expect(removed).not.toHaveProperty('color');
});
it('removes secondary metric dimension', () => {
const removed = visualization.removeDimension({
@ -651,6 +648,7 @@ describe('metric visualization', () => {
});
expect(removed).not.toHaveProperty('maxAccessor');
expect(removed).not.toHaveProperty('progressDirection');
});
it('removes breakdown-by dimension', () => {
const removed = visualization.removeDimension({
@ -660,6 +658,7 @@ describe('metric visualization', () => {
expect(removed).not.toHaveProperty('breakdownByAccessor');
expect(removed).not.toHaveProperty('collapseFn');
expect(removed).not.toHaveProperty('maxCols');
});
});

View file

@ -155,6 +155,28 @@ const metricGroupLabel = i18n.translate('xpack.lens.metric.groupLabel', {
defaultMessage: 'Goal and single value',
});
const removeMetricDimension = (state: MetricVisualizationState) => {
delete state.metricAccessor;
delete state.palette;
delete state.color;
};
const removeSecondaryMetricDimension = (state: MetricVisualizationState) => {
delete state.secondaryMetricAccessor;
delete state.secondaryPrefix;
};
const removeMaxDimension = (state: MetricVisualizationState) => {
delete state.maxAccessor;
delete state.progressDirection;
};
const removeBreakdownByDimension = (state: MetricVisualizationState) => {
delete state.breakdownByAccessor;
delete state.collapseFn;
delete state.maxCols;
};
export const getMetricVisualization = ({
paletteService,
theme,
@ -181,14 +203,13 @@ export const getMetricVisualization = ({
clearLayer(state) {
const newState = { ...state };
delete newState.metricAccessor;
delete newState.secondaryMetricAccessor;
delete newState.secondaryPrefix;
delete newState.breakdownByAccessor;
delete newState.collapseFn;
delete newState.maxAccessor;
delete newState.palette;
// TODO - clear more?
delete newState.subtitle;
removeMetricDimension(newState);
removeSecondaryMetricDimension(newState);
removeMaxDimension(newState);
removeBreakdownByDimension(newState);
return newState;
},
@ -405,20 +426,16 @@ export const getMetricVisualization = ({
const updated = { ...prevState };
if (prevState.metricAccessor === columnId) {
delete updated.metricAccessor;
delete updated.palette;
delete updated.color;
removeMetricDimension(updated);
}
if (prevState.secondaryMetricAccessor === columnId) {
delete updated.secondaryMetricAccessor;
delete updated.secondaryPrefix;
removeSecondaryMetricDimension(updated);
}
if (prevState.maxAccessor === columnId) {
delete updated.maxAccessor;
removeMaxDimension(updated);
}
if (prevState.breakdownByAccessor === columnId) {
delete updated.breakdownByAccessor;
delete updated.collapseFn;
removeBreakdownByDimension(updated);
}
return updated;