Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
commonMigrateVislibPie,
commonAddEmptyValueColorRule,
commonMigrateTagCloud,
commonAddDropLastBucketIntoTSVBModel,
} from '../migrations/visualization_common_migrations';

const byValueAddSupportOfDualIndexSelectionModeInTSVB = (state: SerializableRecord) => {
Expand All @@ -32,6 +33,13 @@ const byValueHideTSVBLastValueIndicator = (state: SerializableRecord) => {
};
};

const byValueAddDropLastBucketIntoTSVBModel = (state: SerializableRecord) => {
return {
...state,
savedVis: commonAddDropLastBucketIntoTSVBModel(state.savedVis),
};
};

const byValueRemoveDefaultIndexPatternAndTimeFieldFromTSVBModel = (state: SerializableRecord) => {
return {
...state,
Expand Down Expand Up @@ -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),
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ 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
? visState.params.series.map((s: any) =>
s.override_index_pattern
? {
...s,
series_drop_last_bucket: s.series_drop_last_bucket ?? 1,
}
: s
)
: undefined,
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
commonMigrateVislibPie,
commonAddEmptyValueColorRule,
commonMigrateTagCloud,
commonAddDropLastBucketIntoTSVBModel,
} from './visualization_common_migrations';

const migrateIndexPattern: SavedObjectMigrationFn<any, any> = (doc) => {
Expand Down Expand Up @@ -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
) => {
Expand Down Expand Up @@ -1100,6 +1118,7 @@ export const visualizationSavedObjectTypeMigrations = {
addEmptyValueColorRule,
migrateVislibPie,
migrateTagCloud,
replaceIndexPatternReference
replaceIndexPatternReference,
addDropLastBucketIntoTSVBModel
),
};