[Lens] allow top metric for last value (#127151)

This commit is contained in:
Andrew Tate 2022-03-15 10:02:31 -05:00 committed by GitHub
parent 7b86f00528
commit a389226138
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 652 additions and 125 deletions

View file

@ -19,6 +19,7 @@ import {
commonRenameFilterReferences,
commonRenameOperationsForFormula,
commonRenameRecordsField,
commonSetLastValueShowArrayValues,
commonUpdateVisLayerType,
getLensCustomVisualizationMigrations,
getLensFilterMigrations,
@ -93,7 +94,8 @@ export const makeLensEmbeddableFactory =
},
'8.2.0': (state) => {
const lensState = state as unknown as { attributes: LensDocShape810<VisState810> };
const migratedLensState = commonEnhanceTableRowHeight(lensState.attributes);
let migratedLensState = commonSetLastValueShowArrayValues(lensState.attributes);
migratedLensState = commonEnhanceTableRowHeight(lensState.attributes);
return {
...lensState,
attributes: migratedLensState,

View file

@ -194,6 +194,23 @@ export const commonRenameFilterReferences = (attributes: LensDocShape715): LensD
return newAttributes as LensDocShape810;
};
export const commonSetLastValueShowArrayValues = (
attributes: LensDocShape810
): LensDocShape810<VisState820> => {
const newAttributes = cloneDeep(attributes);
for (const layer of Object.values(newAttributes.state.datasourceStates.indexpattern.layers)) {
for (const column of Object.values(layer.columns)) {
if (
column.operationType === 'last_value' &&
!(typeof column.params.showArrayValues === 'boolean')
) {
column.params.showArrayValues = true;
}
}
}
return newAttributes;
};
export const commonEnhanceTableRowHeight = (
attributes: LensDocShape810<VisState810>
): LensDocShape810<VisState820> => {

View file

@ -1782,121 +1782,215 @@ describe('Lens migrations', () => {
});
});
describe('8.2.0 rename fitRowToContent to new detailed rowHeight and rowHeightLines', () => {
const context = { log: { warning: () => {} } } as unknown as SavedObjectMigrationContext;
function getExample(fitToContent: boolean) {
return {
describe('8.2.0', () => {
describe('last_value columns', () => {
const context = { log: { warning: () => {} } } as unknown as SavedObjectMigrationContext;
const example = {
type: 'lens',
id: 'mocked-saved-object-id',
attributes: {
visualizationType: 'lnsDatatable',
title: 'Lens visualization',
references: [
{
id: 'ff959d40-b880-11e8-a6d9-e546fe2bba5f',
name: 'indexpattern-datasource-current-indexpattern',
type: 'index-pattern',
},
{
id: 'ff959d40-b880-11e8-a6d9-e546fe2bba5f',
name: 'indexpattern-datasource-layer-cddd8f79-fb20-4191-a3e7-92484780cc62',
type: 'index-pattern',
},
],
savedObjectId: '1',
title: 'MyRenamedOps',
description: '',
visualizationType: null,
state: {
datasourceMetaData: {
filterableIndexPatterns: [],
},
datasourceStates: {
indexpattern: {
currentIndexPatternId: 'logstash-*',
layers: {
'cddd8f79-fb20-4191-a3e7-92484780cc62': {
indexPatternId: 'ff959d40-b880-11e8-a6d9-e546fe2bba5f',
'2': {
columns: {
'221f0abf-6e54-4c61-9316-4107ad6fa500': {
label: 'Top values of category.keyword',
'3': {
dataType: 'string',
operationType: 'terms',
scale: 'ordinal',
sourceField: 'category.keyword',
isBucketed: true,
params: {
size: 5,
orderBy: {
type: 'column',
columnId: 'c6f07a26-64eb-4871-ad62-c7d937230e33',
},
orderDirection: 'desc',
otherBucket: true,
missingBucket: false,
parentFormat: {
id: 'terms',
},
},
label: 'Top values of geoip.country_iso_code',
operationType: 'terms',
params: {},
scale: 'ordinal',
sourceField: 'geoip.country_iso_code',
},
'c6f07a26-64eb-4871-ad62-c7d937230e33': {
label: 'Count of records',
'4': {
label: 'Anzahl der Aufnahmen',
dataType: 'number',
operationType: 'count',
sourceField: 'Aufnahmen',
isBucketed: false,
scale: 'ratio',
},
'5': {
label: 'Sum of bytes',
dataType: 'numver',
operationType: 'last_value',
params: {
// no showArrayValues
},
sourceField: 'bytes',
isBucketed: false,
scale: 'ratio',
sourceField: '___records___',
},
},
columnOrder: [
'221f0abf-6e54-4c61-9316-4107ad6fa500',
'c6f07a26-64eb-4871-ad62-c7d937230e33',
],
incompleteColumns: {},
columnOrder: ['3', '4', '5'],
},
'3': {
columns: {
'5': {
label: 'Sum of bytes',
dataType: 'numver',
operationType: 'last_value',
params: {
// no showArrayValues
},
sourceField: 'bytes',
isBucketed: false,
scale: 'ratio',
},
},
columnOrder: ['3', '4', '5'],
},
},
},
},
visualization: {
columns: [
{
isTransposed: false,
columnId: '221f0abf-6e54-4c61-9316-4107ad6fa500',
},
{
isTransposed: false,
columnId: 'c6f07a26-64eb-4871-ad62-c7d937230e33',
},
],
layerId: 'cddd8f79-fb20-4191-a3e7-92484780cc62',
layerType: 'data',
fitRowToContent: fitToContent,
},
visualization: {},
query: { query: '', language: 'kuery' },
filters: [],
query: {
query: '',
language: 'kuery',
},
},
},
} as unknown as SavedObjectUnsanitizedDoc<LensDocShape810>;
}
it('should migrate enabled fitRowToContent to new rowHeight: "auto"', () => {
const result = migrations['8.2.0'](getExample(true), context) as ReturnType<
SavedObjectMigrationFn<LensDocShape810<VisState810>, LensDocShape810<VisState820>>
>;
it('should set showArrayValues for last-value columns', () => {
const result = migrations['8.2.0'](example, context) as ReturnType<
SavedObjectMigrationFn<LensDocShape, LensDocShape>
>;
expect(result.attributes.state.visualization as VisState820).toEqual(
expect.objectContaining({
rowHeight: 'auto',
})
);
const layer2Columns =
result.attributes.state.datasourceStates.indexpattern.layers['2'].columns;
const layer3Columns =
result.attributes.state.datasourceStates.indexpattern.layers['3'].columns;
expect(layer2Columns['5'].params).toHaveProperty('showArrayValues', true);
expect(layer2Columns['3'].params).not.toHaveProperty('showArrayValues');
expect(layer3Columns['5'].params).toHaveProperty('showArrayValues', true);
});
});
it('should migrate disabled fitRowToContent to new rowHeight: "single"', () => {
const result = migrations['8.2.0'](getExample(false), context) as ReturnType<
SavedObjectMigrationFn<LensDocShape810<VisState810>, LensDocShape810<VisState820>>
>;
describe('rename fitRowToContent to new detailed rowHeight and rowHeightLines', () => {
const context = { log: { warning: () => {} } } as unknown as SavedObjectMigrationContext;
function getExample(fitToContent: boolean) {
return {
type: 'lens',
id: 'mocked-saved-object-id',
attributes: {
visualizationType: 'lnsDatatable',
title: 'Lens visualization',
references: [
{
id: 'ff959d40-b880-11e8-a6d9-e546fe2bba5f',
name: 'indexpattern-datasource-current-indexpattern',
type: 'index-pattern',
},
{
id: 'ff959d40-b880-11e8-a6d9-e546fe2bba5f',
name: 'indexpattern-datasource-layer-cddd8f79-fb20-4191-a3e7-92484780cc62',
type: 'index-pattern',
},
],
state: {
datasourceStates: {
indexpattern: {
layers: {
'cddd8f79-fb20-4191-a3e7-92484780cc62': {
indexPatternId: 'ff959d40-b880-11e8-a6d9-e546fe2bba5f',
columns: {
'221f0abf-6e54-4c61-9316-4107ad6fa500': {
label: 'Top values of category.keyword',
dataType: 'string',
operationType: 'terms',
scale: 'ordinal',
sourceField: 'category.keyword',
isBucketed: true,
params: {
size: 5,
orderBy: {
type: 'column',
columnId: 'c6f07a26-64eb-4871-ad62-c7d937230e33',
},
orderDirection: 'desc',
otherBucket: true,
missingBucket: false,
parentFormat: {
id: 'terms',
},
},
},
'c6f07a26-64eb-4871-ad62-c7d937230e33': {
label: 'Count of records',
dataType: 'number',
operationType: 'count',
isBucketed: false,
scale: 'ratio',
sourceField: '___records___',
},
},
columnOrder: [
'221f0abf-6e54-4c61-9316-4107ad6fa500',
'c6f07a26-64eb-4871-ad62-c7d937230e33',
],
incompleteColumns: {},
},
},
},
},
visualization: {
columns: [
{
isTransposed: false,
columnId: '221f0abf-6e54-4c61-9316-4107ad6fa500',
},
{
isTransposed: false,
columnId: 'c6f07a26-64eb-4871-ad62-c7d937230e33',
},
],
layerId: 'cddd8f79-fb20-4191-a3e7-92484780cc62',
layerType: 'data',
fitRowToContent: fitToContent,
},
filters: [],
query: {
query: '',
language: 'kuery',
},
},
},
} as unknown as SavedObjectUnsanitizedDoc<LensDocShape810>;
}
expect(result.attributes.state.visualization as VisState820).toEqual(
expect.objectContaining({
rowHeight: 'single',
rowHeightLines: 1,
})
);
it('should migrate enabled fitRowToContent to new rowHeight: "auto"', () => {
const result = migrations['8.2.0'](getExample(true), context) as ReturnType<
SavedObjectMigrationFn<LensDocShape810<VisState810>, LensDocShape810<VisState820>>
>;
expect(result.attributes.state.visualization as VisState820).toEqual(
expect.objectContaining({
rowHeight: 'auto',
})
);
});
it('should migrate disabled fitRowToContent to new rowHeight: "single"', () => {
const result = migrations['8.2.0'](getExample(false), context) as ReturnType<
SavedObjectMigrationFn<LensDocShape810<VisState810>, LensDocShape810<VisState820>>
>;
expect(result.attributes.state.visualization as VisState820).toEqual(
expect.objectContaining({
rowHeight: 'single',
rowHeightLines: 1,
})
);
});
});
});
});

View file

@ -40,6 +40,7 @@ import {
getLensCustomVisualizationMigrations,
commonRenameRecordsField,
fixLensTopValuesCustomFormatting,
commonSetLastValueShowArrayValues,
commonEnhanceTableRowHeight,
} from './common_migrations';
@ -465,6 +466,12 @@ const addParentFormatter: SavedObjectMigrationFn<LensDocShape810, LensDocShape81
return { ...newDoc, attributes: fixLensTopValuesCustomFormatting(newDoc.attributes) };
};
const setLastValueShowArrayValues: SavedObjectMigrationFn<LensDocShape810, LensDocShape810> = (
doc
) => {
return { ...doc, attributes: commonSetLastValueShowArrayValues(doc.attributes) };
};
const enhanceTableRowHeight: SavedObjectMigrationFn<LensDocShape810, LensDocShape810> = (doc) => {
const newDoc = cloneDeep(doc);
return { ...newDoc, attributes: commonEnhanceTableRowHeight(newDoc.attributes) };
@ -484,7 +491,7 @@ const lensMigrations: SavedObjectMigrationMap = {
'7.15.0': addLayerTypeToVisualization,
'7.16.0': moveDefaultReversedPaletteToCustom,
'8.1.0': flow(renameFilterReferences, renameRecordsField, addParentFormatter),
'8.2.0': enhanceTableRowHeight,
'8.2.0': flow(setLastValueShowArrayValues, enhanceTableRowHeight),
};
export const getAllMigrations = (