[Lens] Add new Metric formatter migrations (#159539)

## Summary

Part of [54306](https://github.com/elastic/kibana/issues/154306) 
Required #158468 to be merged first.

This PR integrates only the migration part of the new Metric new
formatter task.

This migration implementation defines takes the simplest route for the
migration route of metric visualization formatter:
* if any custom formatter is set in Lens, then apply a `compact` flag on
it to keep it consistent
* if `Default` formatter is set in Lens, then assume nothing and let the
regular formatter hierarchy do its own work

### Example

Original dashboard


![image](e5cff808-456d-406e-a221-706c207f7629)

After migration


![image](d3f2052f-13d8-4228-b446-0eaf5a339e13)


### Checklist

Delete any items that are not applicable to this PR.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces—unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes—Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: Stratoula Kalafateli <efstratia.kalafateli@elastic.co>
This commit is contained in:
Marco Liberati 2023-06-15 13:06:14 +02:00 committed by GitHub
parent 4f4a18a308
commit 536a8d4194
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 186 additions and 11 deletions

View file

@ -66,11 +66,11 @@ describe('checking migration metadata changes on all registered SO types', () =>
"app_search_telemetry": "36234f19573ad397ac30197c45ac219921cc3106",
"application_usage_daily": "20142d23fe5d05ba22b4bc46614d99883bc488f0",
"application_usage_totals": "a29ab014edc20382b9ce22ede221b18cee5d93a6",
"canvas-element": "b39dfe54b9ff3ecc4c6bc5bed6a14b0a0fe83644",
"canvas-workpad": "4df66cf25eba8e7e25c061a1b2a5aadbb1f436e9",
"canvas-workpad-template": "52a35f737b579a570510fca361fddd158d2a92ad",
"canvas-element": "cdedc2123eb8a1506b87a56b0bcce60f4ec08bc8",
"canvas-workpad": "9d82aafb19586b119e5c9382f938abe28c26ca5c",
"canvas-workpad-template": "c077b0087346776bb3542b51e1385d172cb24179",
"cases": "b43a8ce985c406167e1d115381805a48cb3b0e61",
"cases-comments": "ded400d82c5ea26959c2ee8e54896981d499a226",
"cases-comments": "5cb0a421588831c2a950e50f486048b8aabbae25",
"cases-configure": "44ed7b8e0f44df39516b8870589b89e32224d2bf",
"cases-connector-mappings": "f9d1ac57e484e69506c36a8051e4d61f4a8cfd25",
"cases-telemetry": "f219eb7e26772884342487fc9602cfea07b3cedc",
@ -80,7 +80,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
"connector_token": "5a9ac29fe9c740eb114e9c40517245c71706b005",
"core-usage-stats": "b3c04da317c957741ebcdedfea4524049fdc79ff",
"csp-rule-template": "c151324d5f85178169395eecb12bac6b96064654",
"dashboard": "1635368413415b340ae6f43fcd0a55c5dcdd4f41",
"dashboard": "0611794ce10d25a36da0770c91376c575e92e8f2",
"endpoint:user-artifact-manifest": "1c3533161811a58772e30cdc77bac4631da3ef2b",
"enterprise_search_telemetry": "9ac912e1417fc8681e0cd383775382117c9e3d3d",
"epm-packages": "2449bb565f987eff70b1b39578bb17e90c404c6e",
@ -111,7 +111,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
"inventory-view": "b8683c8e352a286b4aca1ab21003115a4800af83",
"kql-telemetry": "93c1d16c1a0dfca9c8842062cf5ef8f62ae401ad",
"legacy-url-alias": "9b8cca3fbb2da46fd12823d3cd38fdf1c9f24bc8",
"lens": "fd6a7938f41888e5ee499fcc5e749489981393aa",
"lens": "5cfa2c52b979b4f8df56dd13c477e152183468b9",
"lens-ui-telemetry": "8c47a9e393861f76e268345ecbadfc8a5fb1e0bd",
"maintenance-window": "d893544460abad56ff7a0e25b78f78776dfe10d1",
"map": "76c71023bd198fb6b1163b31bafd926fe2ceb9da",

View file

@ -230,10 +230,10 @@ export default function ({ getService }: FtrProviderContext) {
type: 'dashboard',
namespaces: ['default'],
migrationVersion: {
dashboard: '8.7.0',
dashboard: '8.9.0',
},
coreMigrationVersion: '8.8.0',
typeMigrationVersion: '8.7.0',
typeMigrationVersion: '8.9.0',
updated_at: '2015-01-01T00:00:00.000Z',
created_at: '2015-01-01T00:00:00.000Z',
version: resp.body.saved_objects[3].version,

View file

@ -80,10 +80,10 @@ export default function ({ getService }: FtrProviderContext) {
type: 'dashboard',
namespaces: ['default'],
migrationVersion: {
dashboard: '8.7.0',
dashboard: '8.9.0',
},
coreMigrationVersion: '8.8.0',
typeMigrationVersion: '8.7.0',
typeMigrationVersion: '8.9.0',
updated_at: resp.body.updated_at,
created_at: resp.body.created_at,
version: resp.body.version,

View file

@ -34,6 +34,7 @@ import {
commonMigratePartitionChartGroups,
commonMigratePartitionMetrics,
commonMigrateIndexPatternDatasource,
commonMigrateMetricFormatter,
} from '../migrations/common_migrations';
import {
CustomVisualizationMigrations,
@ -42,6 +43,7 @@ import {
LensDocShape810,
LensDocShape850,
LensDocShapePre712,
LensDocShape860,
VisState716,
VisState810,
VisState850,
@ -168,6 +170,13 @@ export const makeLensEmbeddableFactory =
attributes: migratedLensState,
} as unknown as SerializableRecord;
},
'8.9.0': (state) => {
const lensState = state as unknown as SavedObject<LensDocShape860>;
return {
...lensState,
attributes: commonMigrateMetricFormatter(lensState.attributes),
} as unknown as SerializableRecord;
},
// FOLLOW THESE GUIDELINES IF YOU ARE ADDING A NEW MIGRATION!
// 1. Make sure you are applying migrations for a given version in the same order here as they are applied in x-pack/plugins/lens/server/migrations/saved_object_migrations.ts
}),

View file

@ -11,8 +11,9 @@ import {
getLensDataViewMigrations,
getLensFilterMigrations,
commonMigratePartitionChartGroups,
commonMigrateMetricFormatter,
} from './common_migrations';
import { LensDocShape840 } from './types';
import { LensDocShape840, LensDocShape860 } from './types';
describe('Lens migrations', () => {
describe('applying filter migrations', () => {
@ -169,4 +170,94 @@ describe('Lens migrations', () => {
`);
});
});
describe('migrate new metric formatters to uniform configuration', () => {
function createVisualizationSO(
type: string,
lensFormatId: undefined | string,
bucketed: boolean = false
): { attributes: LensDocShape860<unknown> } {
return {
attributes: {
filters: [],
visualizationType: type,
title: '',
expression: '',
state: {
datasourceMetaData: { filterableIndexPatterns: [] },
visualization: {},
filters: [],
query: { query: '', language: 'kuery' },
datasourceStates: {
formBased: {
currentIndexPatternId: '',
layers: {
test: {
columnOrder: [],
columns: {
col1: {
operationType: 'test',
isBucketed: bucketed,
params: {
...(lensFormatId ? { format: { id: lensFormatId } } : {}),
},
},
},
},
},
},
},
},
},
};
}
it('should not apply the migration non-metric visualization types', () => {
const lensFormatId = 'number';
const lensVisualizationSavedObject = createVisualizationSO('lnsXY', lensFormatId);
const migratedColumn = commonMigrateMetricFormatter(lensVisualizationSavedObject.attributes)
.state.datasourceStates.formBased.layers.test.columns.col1;
expect(migratedColumn.params).toEqual({ format: { id: lensFormatId } });
});
it.each`
lensFormat | bucketed | expectedCompact
${'default'} | ${true} | ${false}
${'number'} | ${true} | ${false}
${'number'} | ${false} | ${true}
${'percentage'} | ${false} | ${true}
${'bytes'} | ${false} | ${false}
${'bits'} | ${false} | ${false}
${'default'} | ${false} | ${false}
`(
'should migrate lens column (bucketed: $bucketed) format configuration "$lensFormat" into new explicit formatter (compact: $expectedCompact)',
({ lensFormat, bucketed, expectedCompact }) => {
const lensFormatId = lensFormat === 'default' ? undefined : lensFormat;
const lensVisualizationSavedObject = createVisualizationSO(
'lnsMetric',
lensFormatId,
bucketed
);
const migratedColumn = commonMigrateMetricFormatter(lensVisualizationSavedObject.attributes)
.state.datasourceStates.formBased.layers.test.columns.col1;
if (bucketed) {
expect(migratedColumn.params).toEqual(
lensFormatId ? { format: { id: lensFormatId } } : {}
);
}
if (!lensFormatId) {
expect(migratedColumn.params).toEqual({});
} else if (bucketed || !expectedCompact) {
expect(migratedColumn.params).toEqual({ format: { id: lensFormatId } });
} else {
expect(migratedColumn.params).toEqual({
format: { id: lensFormatId, params: { compact: true } },
});
}
}
);
});
});

View file

@ -571,3 +571,70 @@ export const commonMigratePartitionMetrics = (attributes: LensDocShape860<unknow
layers: Array<{ metrics: string[] }>;
}>;
};
export const commonMigrateMetricFormatter = (attributes: LensDocShape860<unknown>) => {
if (attributes.visualizationType !== 'lnsMetric') {
return attributes as LensDocShape860<unknown>;
}
if (!attributes.state.datasourceStates.formBased) {
return attributes as LensDocShape860<unknown>;
}
type LayersType = LensDocShape860['state']['datasourceStates']['formBased']['layers'];
const updatedLayersWithCompactFormatters: LayersType = {};
for (const [layerId, layer] of Object.entries(
attributes.state.datasourceStates.formBased.layers
)) {
const newColumns: Record<string, Record<string, unknown>> = {};
for (const [id, column] of Object.entries(layer.columns)) {
const params = column.params as {
format?: { id: string; params: Record<string, string | boolean> };
};
if (column.isBucketed) {
newColumns[id] = column;
} else {
// When value formatting is set to Default, assume nothing
// Bytes and bits are already compact
if (!params?.format || ['bytes', 'bits'].includes(params.format.id)) {
newColumns[id] = column;
} else {
// Metric only support numeric values
// suffix is not taken into account as it wasn't possible in metric visualization before this version
newColumns[id] = {
...column,
params: {
...params,
format: {
...params?.format,
id: params?.format.id || 'number',
params: {
...params?.format?.params,
compact: true,
},
},
},
};
}
}
}
updatedLayersWithCompactFormatters[layerId] = {
...layer,
columns: newColumns,
};
}
return {
...attributes,
state: {
...attributes.state,
datasourceStates: {
...attributes.state.datasourceStates,
formBased: {
...attributes.state.datasourceStates.formBased,
layers: updatedLayersWithCompactFormatters,
},
},
},
};
};

View file

@ -2537,4 +2537,5 @@ describe('Lens migrations', () => {
);
});
});
// For 8.8.0 tests are already executed at unit level in common_migrations
});

View file

@ -62,6 +62,7 @@ import {
commonMigratePartitionChartGroups,
commonMigratePartitionMetrics,
commonMigrateIndexPatternDatasource,
commonMigrateMetricFormatter,
} from './common_migrations';
interface LensDocShapePre710<VisualizationState = unknown> {
@ -562,6 +563,11 @@ const migratePartitionMetrics: SavedObjectMigrationFn<LensDocShape860, LensDocSh
attributes: commonMigratePartitionMetrics(doc.attributes),
});
const migrateMetricFormatter: SavedObjectMigrationFn<LensDocShape860, LensDocShape860> = (doc) => ({
...doc,
attributes: commonMigrateMetricFormatter(doc.attributes),
});
const lensMigrations: SavedObjectMigrationMap = {
'7.7.0': removeInvalidAccessors,
// The order of these migrations matter, since the timefield migration relies on the aggConfigs
@ -584,6 +590,7 @@ const lensMigrations: SavedObjectMigrationMap = {
'8.3.0': flow(lockOldMetricVisSettings, preserveOldLegendSizeDefault, fixValueLabelsInXY),
'8.5.0': flow(migrateMetricIds, enrichAnnotationLayers, migratePartitionChartGroups),
'8.6.0': flow(migrateIndexPatternDatasource, migratePartitionMetrics),
'8.9.0': migrateMetricFormatter,
// FOLLOW THESE GUIDELINES IF YOU ARE ADDING A NEW MIGRATION!
// 1. Make sure you are applying migrations for a given version in the same order here as they are applied in x-pack/plugins/lens/server/embeddable/make_lens_embeddable_factory.ts
};