Skip to content

Commit ba88a2b

Browse files
[TSVB] Add migration script for 'drop_last_bucket' value (#110782) (#110956)
* [TSVB] Add migration script for 'drop_last_bucket' value * Update visualization_saved_object_migrations.test.ts * fix PR comments Co-authored-by: Alexey Antonov <alexwizp@gmail.com>
1 parent f3b33f6 commit ba88a2b

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

src/plugins/visualizations/server/embeddable/visualize_embeddable_factory.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
commonMigrateVislibPie,
1717
commonAddEmptyValueColorRule,
1818
commonMigrateTagCloud,
19+
commonAddDropLastBucketIntoTSVBModel,
1920
} from '../migrations/visualization_common_migrations';
2021

2122
const byValueAddSupportOfDualIndexSelectionModeInTSVB = (state: SerializableRecord) => {
@@ -32,6 +33,13 @@ const byValueHideTSVBLastValueIndicator = (state: SerializableRecord) => {
3233
};
3334
};
3435

36+
const byValueAddDropLastBucketIntoTSVBModel = (state: SerializableRecord) => {
37+
return {
38+
...state,
39+
savedVis: commonAddDropLastBucketIntoTSVBModel(state.savedVis),
40+
};
41+
};
42+
3543
const byValueRemoveDefaultIndexPatternAndTimeFieldFromTSVBModel = (state: SerializableRecord) => {
3644
return {
3745
...state,
@@ -72,7 +80,12 @@ export const visualizeEmbeddableFactory = (): EmbeddableRegistryDefinition => {
7280
byValueRemoveDefaultIndexPatternAndTimeFieldFromTSVBModel
7381
)(state),
7482
'7.14.0': (state) =>
75-
flow(byValueAddEmptyValueColorRule, byValueMigrateVislibPie, byValueMigrateTagcloud)(state),
83+
flow(
84+
byValueAddEmptyValueColorRule,
85+
byValueMigrateVislibPie,
86+
byValueMigrateTagcloud,
87+
byValueAddDropLastBucketIntoTSVBModel
88+
)(state),
7689
},
7790
};
7891
};

src/plugins/visualizations/server/migrations/visualization_common_migrations.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ export const commonAddSupportOfDualIndexSelectionModeInTSVB = (visState: any) =>
2020
return visState;
2121
};
2222

23+
export const commonAddDropLastBucketIntoTSVBModel = (visState: any) => {
24+
if (visState && visState.type === 'metrics') {
25+
return {
26+
...visState,
27+
params: {
28+
...visState.params,
29+
series: visState.params?.series?.map((s: any) =>
30+
s.override_index_pattern
31+
? {
32+
...s,
33+
series_drop_last_bucket: s.series_drop_last_bucket ?? 1,
34+
}
35+
: s
36+
),
37+
drop_last_bucket: visState.params.drop_last_bucket ?? 1,
38+
},
39+
};
40+
}
41+
return visState;
42+
};
43+
2344
export const commonHideTSVBLastValueIndicator = (visState: any) => {
2445
if (visState && visState.type === 'metrics' && visState.params.type !== 'timeseries') {
2546
return {

src/plugins/visualizations/server/migrations/visualization_saved_object_migrations.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,6 +2115,87 @@ describe('migration visualization', () => {
21152115
});
21162116
});
21172117

2118+
describe('7.14.0 tsvb - add drop last bucket into TSVB model', () => {
2119+
const migrate = (doc: any) =>
2120+
visualizationSavedObjectTypeMigrations['7.14.0'](
2121+
doc as Parameters<SavedObjectMigrationFn>[0],
2122+
savedObjectMigrationContext
2123+
);
2124+
2125+
const createTestDocWithType = (params: any) => ({
2126+
attributes: {
2127+
title: 'My Vis',
2128+
description: 'This is my super cool vis.',
2129+
visState: `{
2130+
"type":"metrics",
2131+
"params": ${JSON.stringify(params)}
2132+
}`,
2133+
},
2134+
});
2135+
2136+
it('should add "drop_last_bucket" into model if it not exist', () => {
2137+
const params = {};
2138+
const migratedTestDoc = migrate(createTestDocWithType(params));
2139+
const { params: migratedParams } = JSON.parse(migratedTestDoc.attributes.visState);
2140+
2141+
expect(migratedParams).toMatchInlineSnapshot(`
2142+
Object {
2143+
"drop_last_bucket": 1,
2144+
}
2145+
`);
2146+
});
2147+
2148+
it('should add "series_drop_last_bucket" into model if it not exist', () => {
2149+
const params = {
2150+
series: [
2151+
{
2152+
override_index_pattern: 1,
2153+
},
2154+
{
2155+
override_index_pattern: 1,
2156+
},
2157+
{ override_index_pattern: 0 },
2158+
{},
2159+
{
2160+
override_index_pattern: 1,
2161+
series_drop_last_bucket: 0,
2162+
},
2163+
{
2164+
override_index_pattern: 1,
2165+
series_drop_last_bucket: 1,
2166+
},
2167+
],
2168+
};
2169+
const migratedTestDoc = migrate(createTestDocWithType(params));
2170+
const { params: migratedParams } = JSON.parse(migratedTestDoc.attributes.visState);
2171+
2172+
expect(migratedParams.series).toMatchInlineSnapshot(`
2173+
Array [
2174+
Object {
2175+
"override_index_pattern": 1,
2176+
"series_drop_last_bucket": 1,
2177+
},
2178+
Object {
2179+
"override_index_pattern": 1,
2180+
"series_drop_last_bucket": 1,
2181+
},
2182+
Object {
2183+
"override_index_pattern": 0,
2184+
},
2185+
Object {},
2186+
Object {
2187+
"override_index_pattern": 1,
2188+
"series_drop_last_bucket": 0,
2189+
},
2190+
Object {
2191+
"override_index_pattern": 1,
2192+
"series_drop_last_bucket": 1,
2193+
},
2194+
]
2195+
`);
2196+
});
2197+
});
2198+
21182199
describe('7.14.0 update pie visualization defaults', () => {
21192200
const migrate = (doc: any) =>
21202201
visualizationSavedObjectTypeMigrations['7.14.0'](

src/plugins/visualizations/server/migrations/visualization_saved_object_migrations.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
commonMigrateVislibPie,
1919
commonAddEmptyValueColorRule,
2020
commonMigrateTagCloud,
21+
commonAddDropLastBucketIntoTSVBModel,
2122
} from './visualization_common_migrations';
2223

2324
const migrateIndexPattern: SavedObjectMigrationFn<any, any> = (doc) => {
@@ -945,6 +946,23 @@ const hideTSVBLastValueIndicator: SavedObjectMigrationFn<any, any> = (doc) => {
945946
return doc;
946947
};
947948

949+
const addDropLastBucketIntoTSVBModel: SavedObjectMigrationFn<any, any> = (doc) => {
950+
try {
951+
const visState = JSON.parse(doc.attributes.visState);
952+
const newVisState = commonAddDropLastBucketIntoTSVBModel(visState);
953+
return {
954+
...doc,
955+
attributes: {
956+
...doc.attributes,
957+
visState: JSON.stringify(newVisState),
958+
},
959+
};
960+
} catch (e) {
961+
// Let it go, the data is invalid and we'll leave it as is
962+
}
963+
return doc;
964+
};
965+
948966
const removeDefaultIndexPatternAndTimeFieldFromTSVBModel: SavedObjectMigrationFn<any, any> = (
949967
doc
950968
) => {
@@ -1100,6 +1118,7 @@ export const visualizationSavedObjectTypeMigrations = {
11001118
addEmptyValueColorRule,
11011119
migrateVislibPie,
11021120
migrateTagCloud,
1103-
replaceIndexPatternReference
1121+
replaceIndexPatternReference,
1122+
addDropLastBucketIntoTSVBModel
11041123
),
11051124
};

0 commit comments

Comments
 (0)