Skip to content

Commit

Permalink
Bug fix:Adds a migration for transforming TSVB vis split_filters (ela…
Browse files Browse the repository at this point in the history
…stic#49000)

* Bug fix:Adds a migration for transforming TSVB visualization split_filters from strings to query:language objects to 7.5

* Changes hard coded migration version to get the version number from the saved object's migrationVersion in failing functional test

* Changes migration number from '7.5.0' to '7.4.2', fixes typo, changes test expectations to more explicit ones
  • Loading branch information
TinaHeiligers authored and timroes committed Oct 28, 2019
1 parent 4f4cd8b commit d0f7aa6
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 3 deletions.
50 changes: 50 additions & 0 deletions src/legacy/core_plugins/kibana/migrations/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,50 @@ function transformFilterStringToQueryObject(doc) {
}
return newDoc;
}
function transformSplitFiltersStringToQueryObject(doc) {
// Migrate split_filters in TSVB objects that weren't migrated in 7.3
// If any filters exist and they are a string, we assume them to be lucene syntax and transform the filter into an object accordingly
const newDoc = cloneDeep(doc);
const visStateJSON = get(doc, 'attributes.visState');
if (visStateJSON) {
let visState;
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// let it go, the data is invalid and we'll leave it as is
}
if (visState) {
const visType = get(visState, 'params.type');
const tsvbTypes = ['metric', 'markdown', 'top_n', 'gauge', 'table', 'timeseries'];
if (tsvbTypes.indexOf(visType) === -1) {
// skip
return doc;
}
// migrate the series split_filter filters
const series = get(visState, 'params.series') || [];
series.forEach(item => {
// series item split filters filter
if (item.split_filters) {
const splitFilters = get(item, 'split_filters') || [];
if (splitFilters.length > 0) {
// only transform split_filter filters if we have filters
splitFilters.forEach(filter => {
if (typeof filter.filter === 'string') {
const filterfilterObject = {
query: filter.filter,
language: 'lucene',
};
filter.filter = filterfilterObject;
}
});
}
}
});
newDoc.attributes.visState = JSON.stringify(visState);
}
}
return newDoc;
}

function migrateFiltersAggQuery(doc) {
const visStateJSON = get(doc, 'attributes.visState');
Expand Down Expand Up @@ -435,6 +479,10 @@ const executeSearchMigrations740 = flow(
migrateSearchSortToNestedArray,
);

const executeMigrations742 = flow(
transformSplitFiltersStringToQueryObject
);

export const migrations = {
'index-pattern': {
'6.5.0': doc => {
Expand Down Expand Up @@ -541,6 +589,8 @@ export const migrations = {
'7.2.0': doc => executeMigrations720(doc),
'7.3.0': executeMigrations730,
'7.3.1': executeVisualizationMigrations731,
// migrate split_filters that were not migrated in 7.3.0 (transformFilterStringToQueryObject).
'7.4.2': executeMigrations742,
},
dashboard: {
'7.0.0': doc => {
Expand Down
117 changes: 117 additions & 0 deletions src/legacy/core_plugins/kibana/migrations/migrations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,123 @@ Array [
expect(migratedDoc).toEqual({ attributes: { visState: JSON.stringify(expected) } });
});
});
describe('7.4.2 tsvb split_filters migration', () => {
const migrate = doc => migrations.visualization['7.4.2'](doc);
const generateDoc = ({ params }) => ({
attributes: {
title: 'My Vis',
description: 'This is my super cool vis.',
visState: JSON.stringify({ params }),
uiStateJSON: '{}',
version: 1,
kibanaSavedObjectMeta: {
searchSourceJSON: '{}',
},
},
});
it('should change series item filters from a string into an object for all filters', () => {
const params = {
type: 'timeseries',
filter: {
query: 'bytes:>1000',
language: 'lucene'
},
series: [
{
split_filters: [{ filter: 'bytes:>1000' }],
},
]
};
const timeSeriesDoc = generateDoc({ params: params });
const migratedtimeSeriesDoc = migrate(timeSeriesDoc);
const timeSeriesParams = JSON.parse(migratedtimeSeriesDoc.attributes.visState).params;
expect(Object.keys(timeSeriesParams.filter)).toEqual(
expect.arrayContaining(['query', 'language'])
);
expect(timeSeriesParams.series[0].split_filters[0].filter).toEqual(
{ query: 'bytes:>1000', language: 'lucene' }
);
});
it('should change series item split filters when there is no filter item', () => {
const params = {
type: 'timeseries',
filter: {
query: 'bytes:>1000',
language: 'lucene'
},
series: [
{
split_filters: [{ filter: 'bytes:>1000' }],
},
],
annotations: [
{
query_string: {
query: 'bytes:>1000',
language: 'lucene'
}
}
],
};
const timeSeriesDoc = generateDoc({ params: params });
const migratedtimeSeriesDoc = migrate(timeSeriesDoc);
const timeSeriesParams = JSON.parse(migratedtimeSeriesDoc.attributes.visState).params;
expect(timeSeriesParams.series[0].split_filters[0].filter).toEqual(
{ query: 'bytes:>1000', language: 'lucene' }
);
});
it('should not convert split_filters to objects if there are no split filter filters', () => {
const params = {
type: 'timeseries',
filter: {
query: 'bytes:>1000',
language: 'lucene'
},
series: [
{
split_filters: [],
},
]
};
const timeSeriesDoc = generateDoc({ params: params });
const migratedtimeSeriesDoc = migrate(timeSeriesDoc);
const timeSeriesParams = JSON.parse(migratedtimeSeriesDoc.attributes.visState).params;
expect(timeSeriesParams.series[0].split_filters).not.toHaveProperty('query');
});
it('should do nothing if a split_filter is already a query:language object', () => {
const params = {
type: 'timeseries',
filter: {
query: 'bytes:>1000',
language: 'lucene'
},
series: [
{
split_filters: [{
filter: {
query: 'bytes:>1000',
language: 'lucene',
}
}],
},
],
annotations: [
{
query_string: {
query: 'bytes:>1000',
language: 'lucene'
}
}
],
};
const timeSeriesDoc = generateDoc({ params: params });
const migratedtimeSeriesDoc = migrate(timeSeriesDoc);
const timeSeriesParams = JSON.parse(migratedtimeSeriesDoc.attributes.visState).params;
expect(timeSeriesParams.series[0].split_filters[0].filter.query).toEqual('bytes:>1000');
expect(timeSeriesParams.series[0].split_filters[0].filter.language).toEqual('lucene');

});
});
});

describe('dashboard', () => {
Expand Down
4 changes: 1 addition & 3 deletions test/api_integration/apis/saved_objects/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ export default function ({ getService }) {
id: '91200a00-9efd-11e7-acb3-3dab96693fab',
}
],
migrationVersion: {
visualization: '7.3.1',
},
migrationVersion: resp.body.saved_objects[0].migrationVersion,
updated_at: '2017-09-21T18:51:23.794Z',
version: 'WzIsMV0=',
},
Expand Down

0 comments on commit d0f7aa6

Please sign in to comment.