Skip to content

Commit

Permalink
Bug fix:Adds a migration for transforming TSVB visualization split_fi…
Browse files Browse the repository at this point in the history
…lters from strings to query:language objects to 7.5
  • Loading branch information
TinaHeiligers committed Oct 22, 2019
1 parent 0cb7035 commit dc4ed92
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
49 changes: 49 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 were'nt 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 executeMigrations750 = flow(
transformSplitFiltersStringToQueryObject
);

export const migrations = {
'index-pattern': {
'6.5.0': doc => {
Expand Down Expand Up @@ -541,6 +589,7 @@ export const migrations = {
'7.2.0': doc => executeMigrations720(doc),
'7.3.0': executeMigrations730,
'7.3.1': executeVisualizationMigrations731,
'7.5.0': executeMigrations750,
},
dashboard: {
'7.0.0': doc => {
Expand Down
115 changes: 115 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,121 @@ Array [
expect(migratedDoc).toEqual({ attributes: { visState: JSON.stringify(expected) } });
});
});
describe('7.5.0 tsvb split_filters migration', () => {
const migrate = doc => migrations.visualization['7.5.0'](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(Object.keys(timeSeriesParams.series[0].split_filters[0].filter)).toEqual(
expect.arrayContaining(['query', 'language'])
);
});
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).toHaveProperty('query');
});
it('should not error out if there are no split filter items', () => {
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;
});
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

0 comments on commit dc4ed92

Please sign in to comment.