Skip to content

Commit

Permalink
fix records field name and migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Jan 27, 2022
1 parent 36722fa commit d71093d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 15 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/lens/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export const layerTypes: Record<string, LayerType> = {
REFERENCELINE: 'referenceLine',
};

// might collide with user-supplied field names, try to make as unique as possible
export const DOCUMENT_FIELD_NAME = '___records___';

export function getBasePath() {
return `#/`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { i18n } from '@kbn/i18n';
import { DOCUMENT_FIELD_NAME } from '../../common';
import { IndexPatternField } from './types';

/**
Expand All @@ -16,9 +17,7 @@ export const documentField: IndexPatternField = {
displayName: i18n.translate('xpack.lens.indexPattern.records', {
defaultMessage: 'Records',
}),
name: i18n.translate('xpack.lens.indexPattern.records', {
defaultMessage: 'Records',
}),
name: DOCUMENT_FIELD_NAME,
type: 'document',
aggregatable: true,
searchable: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
commonRemoveTimezoneDateHistogramParam,
commonRenameFilterReferences,
commonRenameOperationsForFormula,
commonRenameRecordsField,
commonUpdateVisLayerType,
getLensFilterMigrations,
} from '../migrations/common_migrations';
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 22 additions & 3 deletions x-pack/plugins/lens/server/migrations/common_migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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[]>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';

Expand Down
16 changes: 10 additions & 6 deletions x-pack/plugins/lens/server/migrations/saved_object_migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,6 +27,7 @@ import {
VisStatePost715,
VisStatePre715,
VisState716,
LensDocShape810,
} from './types';
import {
commonRenameOperationsForFormula,
Expand All @@ -35,6 +36,7 @@ import {
commonMakeReversePaletteAsCustom,
commonRenameFilterReferences,
getLensFilterMigrations,
commonRenameRecordsField,
} from './common_migrations';

interface LensDocShapePre710<VisualizationState = unknown> {
Expand Down Expand Up @@ -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
Expand All @@ -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 = (
Expand Down

0 comments on commit d71093d

Please sign in to comment.