[TSVB] Add migration script for 'drop_last_bucket' value (#110782)

* [TSVB] Add migration script for 'drop_last_bucket' value

* Update visualization_saved_object_migrations.test.ts

* fix PR comments
This commit is contained in:
Alexey Antonov 2021-09-02 12:49:47 +03:00 committed by GitHub
parent 8a357fdabd
commit 88617325f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 136 additions and 2 deletions

View file

@ -16,6 +16,7 @@ import {
commonMigrateVislibPie,
commonAddEmptyValueColorRule,
commonMigrateTagCloud,
commonAddDropLastBucketIntoTSVBModel,
} from '../migrations/visualization_common_migrations';
const byValueAddSupportOfDualIndexSelectionModeInTSVB = (state: SerializableRecord) => {
@ -32,6 +33,13 @@ const byValueHideTSVBLastValueIndicator = (state: SerializableRecord) => {
};
};
const byValueAddDropLastBucketIntoTSVBModel = (state: SerializableRecord) => {
return {
...state,
savedVis: commonAddDropLastBucketIntoTSVBModel(state.savedVis),
};
};
const byValueRemoveDefaultIndexPatternAndTimeFieldFromTSVBModel = (state: SerializableRecord) => {
return {
...state,
@ -72,7 +80,12 @@ export const visualizeEmbeddableFactory = (): EmbeddableRegistryDefinition => {
byValueRemoveDefaultIndexPatternAndTimeFieldFromTSVBModel
)(state),
'7.14.0': (state) =>
flow(byValueAddEmptyValueColorRule, byValueMigrateVislibPie, byValueMigrateTagcloud)(state),
flow(
byValueAddEmptyValueColorRule,
byValueMigrateVislibPie,
byValueMigrateTagcloud,
byValueAddDropLastBucketIntoTSVBModel
)(state),
},
};
};

View file

@ -20,6 +20,27 @@ export const commonAddSupportOfDualIndexSelectionModeInTSVB = (visState: any) =>
return visState;
};
export const commonAddDropLastBucketIntoTSVBModel = (visState: any) => {
if (visState && visState.type === 'metrics') {
return {
...visState,
params: {
...visState.params,
series: visState.params?.series?.map((s: any) =>
s.override_index_pattern
? {
...s,
series_drop_last_bucket: s.series_drop_last_bucket ?? 1,
}
: s
),
drop_last_bucket: visState.params.drop_last_bucket ?? 1,
},
};
}
return visState;
};
export const commonHideTSVBLastValueIndicator = (visState: any) => {
if (visState && visState.type === 'metrics' && visState.params.type !== 'timeseries') {
return {

View file

@ -2115,6 +2115,87 @@ describe('migration visualization', () => {
});
});
describe('7.14.0 tsvb - add drop last bucket into TSVB model', () => {
const migrate = (doc: any) =>
visualizationSavedObjectTypeMigrations['7.14.0'](
doc as Parameters<SavedObjectMigrationFn>[0],
savedObjectMigrationContext
);
const createTestDocWithType = (params: any) => ({
attributes: {
title: 'My Vis',
description: 'This is my super cool vis.',
visState: `{
"type":"metrics",
"params": ${JSON.stringify(params)}
}`,
},
});
it('should add "drop_last_bucket" into model if it not exist', () => {
const params = {};
const migratedTestDoc = migrate(createTestDocWithType(params));
const { params: migratedParams } = JSON.parse(migratedTestDoc.attributes.visState);
expect(migratedParams).toMatchInlineSnapshot(`
Object {
"drop_last_bucket": 1,
}
`);
});
it('should add "series_drop_last_bucket" into model if it not exist', () => {
const params = {
series: [
{
override_index_pattern: 1,
},
{
override_index_pattern: 1,
},
{ override_index_pattern: 0 },
{},
{
override_index_pattern: 1,
series_drop_last_bucket: 0,
},
{
override_index_pattern: 1,
series_drop_last_bucket: 1,
},
],
};
const migratedTestDoc = migrate(createTestDocWithType(params));
const { params: migratedParams } = JSON.parse(migratedTestDoc.attributes.visState);
expect(migratedParams.series).toMatchInlineSnapshot(`
Array [
Object {
"override_index_pattern": 1,
"series_drop_last_bucket": 1,
},
Object {
"override_index_pattern": 1,
"series_drop_last_bucket": 1,
},
Object {
"override_index_pattern": 0,
},
Object {},
Object {
"override_index_pattern": 1,
"series_drop_last_bucket": 0,
},
Object {
"override_index_pattern": 1,
"series_drop_last_bucket": 1,
},
]
`);
});
});
describe('7.14.0 update pie visualization defaults', () => {
const migrate = (doc: any) =>
visualizationSavedObjectTypeMigrations['7.14.0'](

View file

@ -18,6 +18,7 @@ import {
commonMigrateVislibPie,
commonAddEmptyValueColorRule,
commonMigrateTagCloud,
commonAddDropLastBucketIntoTSVBModel,
} from './visualization_common_migrations';
const migrateIndexPattern: SavedObjectMigrationFn<any, any> = (doc) => {
@ -945,6 +946,23 @@ const hideTSVBLastValueIndicator: SavedObjectMigrationFn<any, any> = (doc) => {
return doc;
};
const addDropLastBucketIntoTSVBModel: SavedObjectMigrationFn<any, any> = (doc) => {
try {
const visState = JSON.parse(doc.attributes.visState);
const newVisState = commonAddDropLastBucketIntoTSVBModel(visState);
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(newVisState),
},
};
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
return doc;
};
const removeDefaultIndexPatternAndTimeFieldFromTSVBModel: SavedObjectMigrationFn<any, any> = (
doc
) => {
@ -1100,6 +1118,7 @@ export const visualizationSavedObjectTypeMigrations = {
addEmptyValueColorRule,
migrateVislibPie,
migrateTagCloud,
replaceIndexPatternReference
replaceIndexPatternReference,
addDropLastBucketIntoTSVBModel
),
};