[Lens] update defaults for metric vis (#129968)

This commit is contained in:
Andrew Tate 2022-04-13 07:10:47 -05:00 committed by GitHub
parent dbb6b1a75b
commit 88336719a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 118 additions and 20 deletions

View file

@ -14,6 +14,7 @@ import {
import { DOC_TYPE } from '../../common';
import {
commonEnhanceTableRowHeight,
commonLockOldMetricVisSettings,
commonMakeReversePaletteAsCustom,
commonRemoveTimezoneDateHistogramParam,
commonRenameFilterReferences,
@ -103,6 +104,14 @@ export const makeLensEmbeddableFactory =
attributes: migratedLensState,
} as unknown as SerializableRecord;
},
'8.3.0': (state) => {
const lensState = state as unknown as { attributes: LensDocShape810<VisState810> };
const migratedLensState = commonLockOldMetricVisSettings(lensState.attributes);
return {
...lensState,
attributes: migratedLensState,
} as unknown as SerializableRecord;
},
}),
getLensCustomVisualizationMigrations(customVisualizationMigrations)
),

View file

@ -28,7 +28,7 @@ import {
CustomVisualizationMigrations,
LensDocShape810,
} from './types';
import { DOCUMENT_FIELD_NAME, layerTypes } from '../../common';
import { DOCUMENT_FIELD_NAME, layerTypes, MetricState } from '../../common';
import { LensDocShape } from './saved_object_migrations';
export const commonRenameOperationsForFormula = (
@ -239,6 +239,21 @@ export const commonSetIncludeEmptyRowsDateHistogram = (
return newAttributes;
};
export const commonLockOldMetricVisSettings = (
attributes: LensDocShape810
): LensDocShape810<VisState820> => {
const newAttributes = cloneDeep(attributes);
if (newAttributes.visualizationType !== 'lnsMetric') {
return newAttributes;
}
const visState = newAttributes.state.visualization as MetricState;
visState.textAlign = visState.textAlign ?? 'center';
visState.titlePosition = visState.titlePosition ?? 'bottom';
visState.size = visState.size ?? 'xl';
return newAttributes;
};
const getApplyCustomVisualizationMigrationToLens = (id: string, migration: MigrateFunction) => {
return (savedObject: { attributes: LensDocShape }) => {
if (savedObject.attributes.visualizationType !== id) return savedObject;

View file

@ -22,7 +22,7 @@ import {
VisState810,
VisState820,
} from './types';
import { layerTypes } from '../../common';
import { layerTypes, MetricState } from '../../common';
import { Filter } from '@kbn/es-query';
describe('Lens migrations', () => {
@ -2064,4 +2064,53 @@ describe('Lens migrations', () => {
expect(layer2Columns['4'].params).toHaveProperty('includeEmptyRows', true);
});
});
describe('8.3.0 old metric visualization defaults', () => {
const context = { log: { warning: () => {} } } as unknown as SavedObjectMigrationContext;
const example = {
type: 'lens',
id: 'mocked-saved-object-id',
attributes: {
savedObjectId: '1',
title: 'MyRenamedOps',
description: '',
visualizationType: 'lnsMetric',
state: {
visualization: {},
},
},
} as unknown as SavedObjectUnsanitizedDoc<LensDocShape810>;
it('preserves current config for existing visualizations that are using the DEFAULTS', () => {
const result = migrations['8.3.0'](example, context) as ReturnType<
SavedObjectMigrationFn<LensDocShape, LensDocShape>
>;
const visState = result.attributes.state.visualization as MetricState;
expect(visState.textAlign).toBe('center');
expect(visState.titlePosition).toBe('bottom');
expect(visState.size).toBe('xl');
});
it('preserves current config for existing visualizations that are using CUSTOM settings', () => {
const result = migrations['8.3.0'](
{
...example,
attributes: {
...example.attributes,
state: {
visualization: {
textAlign: 'right',
titlePosition: 'top',
size: 's',
},
},
},
},
context
) as ReturnType<SavedObjectMigrationFn<LensDocShape, LensDocShape>>;
const visState = result.attributes.state.visualization as MetricState;
expect(visState.textAlign).toBe('right');
expect(visState.titlePosition).toBe('top');
expect(visState.size).toBe('s');
});
});
});

View file

@ -44,6 +44,7 @@ import {
commonSetLastValueShowArrayValues,
commonEnhanceTableRowHeight,
commonSetIncludeEmptyRowsDateHistogram,
commonLockOldMetricVisSettings,
} from './common_migrations';
interface LensDocShapePre710<VisualizationState = unknown> {
@ -485,6 +486,10 @@ const setIncludeEmptyRowsDateHistogram: SavedObjectMigrationFn<LensDocShape810,
return { ...doc, attributes: commonSetIncludeEmptyRowsDateHistogram(doc.attributes) };
};
const lockOldMetricVisSettings: SavedObjectMigrationFn<LensDocShape810, LensDocShape810> = (
doc
) => ({ ...doc, attributes: commonLockOldMetricVisSettings(doc.attributes) });
const lensMigrations: SavedObjectMigrationMap = {
'7.7.0': removeInvalidAccessors,
// The order of these migrations matter, since the timefield migration relies on the aggConfigs
@ -504,6 +509,7 @@ const lensMigrations: SavedObjectMigrationMap = {
setIncludeEmptyRowsDateHistogram,
enhanceTableRowHeight
),
'8.3.0': lockOldMetricVisSettings,
};
export const getAllMigrations = (