mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[Lens] Fix records field name and migrate (#123894)
This commit is contained in:
parent
89fc72f955
commit
fabaa88791
23 changed files with 207 additions and 99 deletions
|
@ -17,6 +17,7 @@ import {
|
|||
commonRemoveTimezoneDateHistogramParam,
|
||||
commonRenameFilterReferences,
|
||||
commonRenameOperationsForFormula,
|
||||
commonRenameRecordsField,
|
||||
commonUpdateVisLayerType,
|
||||
getLensFilterMigrations,
|
||||
} from '../migrations/common_migrations';
|
||||
|
@ -68,8 +69,10 @@ export const makeLensEmbeddableFactory =
|
|||
} as unknown as SerializableRecord;
|
||||
},
|
||||
'8.1.0': (state) => {
|
||||
const lensState = state as unknown as { attributes: LensDocShape715<VisState716> };
|
||||
const migratedLensState = commonRenameFilterReferences(lensState.attributes);
|
||||
const lensState = state as unknown as { attributes: LensDocShape715 };
|
||||
const migratedLensState = commonRenameRecordsField(
|
||||
commonRenameFilterReferences(lensState.attributes)
|
||||
);
|
||||
return {
|
||||
...lensState,
|
||||
attributes: migratedLensState,
|
||||
|
|
|
@ -22,8 +22,9 @@ import {
|
|||
VisStatePost715,
|
||||
VisStatePre715,
|
||||
VisState716,
|
||||
LensDocShape810,
|
||||
} from './types';
|
||||
import { CustomPaletteParams, layerTypes } from '../../common';
|
||||
import { CustomPaletteParams, DOCUMENT_FIELD_NAME, layerTypes } from '../../common';
|
||||
import { LensDocShape } from './saved_object_migrations';
|
||||
|
||||
export const commonRenameOperationsForFormula = (
|
||||
|
@ -162,13 +163,31 @@ export const commonMakeReversePaletteAsCustom = (
|
|||
return newAttributes;
|
||||
};
|
||||
|
||||
export const commonRenameFilterReferences = (attributes: LensDocShape715<VisState716>) => {
|
||||
export const commonRenameRecordsField = (attributes: LensDocShape810) => {
|
||||
const newAttributes = cloneDeep(attributes);
|
||||
Object.keys(newAttributes.state?.datasourceStates?.indexpattern?.layers || {}).forEach(
|
||||
(layerId) => {
|
||||
newAttributes.state.datasourceStates.indexpattern.layers[layerId].columnOrder.forEach(
|
||||
(columnId) => {
|
||||
const column =
|
||||
newAttributes.state.datasourceStates.indexpattern.layers[layerId].columns[columnId];
|
||||
if (column && column.operationType === 'count') {
|
||||
column.sourceField = DOCUMENT_FIELD_NAME;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
return newAttributes;
|
||||
};
|
||||
|
||||
export const commonRenameFilterReferences = (attributes: LensDocShape715): LensDocShape810 => {
|
||||
const newAttributes = cloneDeep(attributes);
|
||||
for (const filter of newAttributes.state.filters) {
|
||||
filter.meta.index = filter.meta.indexRefName;
|
||||
delete filter.meta.indexRefName;
|
||||
}
|
||||
return newAttributes;
|
||||
return newAttributes as LensDocShape810;
|
||||
};
|
||||
|
||||
const getApplyFilterMigrationToLens = (filterMigration: MigrateFunction<Filter[]>) => {
|
||||
|
|
|
@ -12,7 +12,13 @@ import {
|
|||
SavedObjectMigrationFn,
|
||||
SavedObjectUnsanitizedDoc,
|
||||
} from 'src/core/server';
|
||||
import { LensDocShape715, VisState716, VisStatePost715, VisStatePre715 } from './types';
|
||||
import {
|
||||
LensDocShape715,
|
||||
LensDocShape810,
|
||||
VisState716,
|
||||
VisStatePost715,
|
||||
VisStatePre715,
|
||||
} from './types';
|
||||
import { CustomPaletteParams, layerTypes } from '../../common';
|
||||
import { PaletteOutput } from 'src/plugins/charts/common';
|
||||
import { Filter } from '@kbn/es-query';
|
||||
|
@ -1513,6 +1519,78 @@ describe('Lens migrations', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('8.1.0 rename records field', () => {
|
||||
const context = { log: { warning: () => {} } } as unknown as SavedObjectMigrationContext;
|
||||
const example = {
|
||||
type: 'lens',
|
||||
id: 'mocked-saved-object-id',
|
||||
attributes: {
|
||||
savedObjectId: '1',
|
||||
title: 'MyRenamedOps',
|
||||
description: '',
|
||||
visualizationType: null,
|
||||
state: {
|
||||
datasourceMetaData: {
|
||||
filterableIndexPatterns: [],
|
||||
},
|
||||
datasourceStates: {
|
||||
indexpattern: {
|
||||
currentIndexPatternId: 'logstash-*',
|
||||
layers: {
|
||||
'2': {
|
||||
columns: {
|
||||
'3': {
|
||||
label: '@timestamp',
|
||||
dataType: 'date',
|
||||
operationType: 'date_histogram',
|
||||
sourceField: '@timestamp',
|
||||
isBucketed: true,
|
||||
scale: 'interval',
|
||||
params: { interval: 'auto', timeZone: 'Europe/Berlin' },
|
||||
},
|
||||
'4': {
|
||||
label: 'Anzahl der Aufnahmen',
|
||||
dataType: 'number',
|
||||
operationType: 'count',
|
||||
sourceField: 'Aufnahmen',
|
||||
isBucketed: false,
|
||||
scale: 'ratio',
|
||||
},
|
||||
'5': {
|
||||
label: 'Sum of bytes',
|
||||
dataType: 'numver',
|
||||
operationType: 'sum',
|
||||
sourceField: 'bytes',
|
||||
isBucketed: false,
|
||||
scale: 'ratio',
|
||||
},
|
||||
},
|
||||
columnOrder: ['3', '4', '5'],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
visualization: {},
|
||||
query: { query: '', language: 'kuery' },
|
||||
filters: [],
|
||||
},
|
||||
},
|
||||
} as unknown as SavedObjectUnsanitizedDoc<LensDocShape810>;
|
||||
|
||||
it('should change field for count operations but not for others, not changing the vis', () => {
|
||||
const result = migrations['8.1.0'](example, context) as ReturnType<
|
||||
SavedObjectMigrationFn<LensDocShape, LensDocShape>
|
||||
>;
|
||||
|
||||
expect(
|
||||
Object.values(
|
||||
result.attributes.state.datasourceStates.indexpattern.layers['2'].columns
|
||||
).map((column) => column.sourceField)
|
||||
).toEqual(['@timestamp', '___records___', 'bytes']);
|
||||
expect(example.attributes.state.visualization).toEqual(result.attributes.state.visualization);
|
||||
});
|
||||
});
|
||||
|
||||
test('should properly apply a filter migration within a lens visualization', () => {
|
||||
const migrationVersion = 'some-version';
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { cloneDeep, mergeWith } from 'lodash';
|
||||
import { cloneDeep, flow, mergeWith } from 'lodash';
|
||||
import { fromExpression, toExpression, Ast, AstFunction } from '@kbn/interpreter';
|
||||
import {
|
||||
SavedObjectMigrationMap,
|
||||
|
@ -27,6 +27,7 @@ import {
|
|||
VisStatePost715,
|
||||
VisStatePre715,
|
||||
VisState716,
|
||||
LensDocShape810,
|
||||
} from './types';
|
||||
import {
|
||||
commonRenameOperationsForFormula,
|
||||
|
@ -35,6 +36,7 @@ import {
|
|||
commonMakeReversePaletteAsCustom,
|
||||
commonRenameFilterReferences,
|
||||
getLensFilterMigrations,
|
||||
commonRenameRecordsField,
|
||||
} from './common_migrations';
|
||||
|
||||
interface LensDocShapePre710<VisualizationState = unknown> {
|
||||
|
@ -444,14 +446,16 @@ const moveDefaultReversedPaletteToCustom: SavedObjectMigrationFn<
|
|||
return { ...newDoc, attributes: commonMakeReversePaletteAsCustom(newDoc.attributes) };
|
||||
};
|
||||
|
||||
const renameFilterReferences: SavedObjectMigrationFn<
|
||||
LensDocShape715<VisState716>,
|
||||
LensDocShape715<VisState716>
|
||||
> = (doc) => {
|
||||
const renameFilterReferences: SavedObjectMigrationFn<LensDocShape715, LensDocShape810> = (doc) => {
|
||||
const newDoc = cloneDeep(doc);
|
||||
return { ...newDoc, attributes: commonRenameFilterReferences(newDoc.attributes) };
|
||||
};
|
||||
|
||||
const renameRecordsField: SavedObjectMigrationFn<LensDocShape810, LensDocShape810> = (doc) => {
|
||||
const newDoc = cloneDeep(doc);
|
||||
return { ...newDoc, attributes: commonRenameRecordsField(newDoc.attributes) };
|
||||
};
|
||||
|
||||
const lensMigrations: SavedObjectMigrationMap = {
|
||||
'7.7.0': removeInvalidAccessors,
|
||||
// The order of these migrations matter, since the timefield migration relies on the aggConfigs
|
||||
|
@ -465,7 +469,7 @@ const lensMigrations: SavedObjectMigrationMap = {
|
|||
'7.14.0': removeTimezoneDateHistogramParam,
|
||||
'7.15.0': addLayerTypeToVisualization,
|
||||
'7.16.0': moveDefaultReversedPaletteToCustom,
|
||||
'8.1.0': renameFilterReferences,
|
||||
'8.1.0': flow(renameFilterReferences, renameRecordsField),
|
||||
};
|
||||
|
||||
export const mergeSavedObjectMigrationMaps = (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue