Skip to content

Commit

Permalink
Remove nested table splits from table vis (#26057)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers authored Jan 25, 2019
1 parent 3038ea8 commit c6f0595
Show file tree
Hide file tree
Showing 14 changed files with 357 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { VisTypesRegistryProvider } from 'ui/registry/vis_types';
import { DocTitleProvider } from 'ui/doc_title';
import { FilterBarQueryFilterProvider } from 'ui/filter_bar/query_filter';
import { stateMonitorFactory } from 'ui/state_management/state_monitor_factory';
import { migrateAppState } from './lib';
import uiRoutes from 'ui/routes';
import { uiModules } from 'ui/modules';
import editorTemplate from './editor.html';
Expand Down Expand Up @@ -265,6 +266,12 @@ function VisEditor(
// This is used to sync visualization state with the url when `appState.save()` is called.
const appState = new AppState(stateDefaults);

// Initializing appState does two things - first it translates the defaults into AppState,
// second it updates appState based on the url (the url trumps the defaults). This means if
// we update the state format at all and want to handle BWC, we must not only migrate the
// data stored with saved vis, but also any old state in the url.
migrateAppState(appState);

// The savedVis is pulled from elasticsearch, but the appState is pulled from the url, with the
// defaults applied. If the url was from a previous session which included modifications to the
// appState then they won't be equal.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export { migrateAppState } from './migrate_app_state';
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { get, omit } from 'lodash';

/**
* Creates a new instance of AppState based on the table vis state.
*
* Dashboards have a similar implementation; see
* core_plugins/kibana/public/dashboard/lib/migrate_app_state
*
* @param appState {AppState} AppState class to instantiate
*/
export function migrateAppState(appState) {
// For BWC in pre 7.0 versions where table visualizations could have multiple aggs
// with `schema === 'split'`. This ensures that bookmarked URLs with deprecated params
// are rewritten to the correct state. See core_plugins/table_vis/migrations.
if (appState.vis.type !== 'table') {
return;
}

const visAggs = get(appState, 'vis.aggs', []);
let splitCount = 0;
const migratedAggs = visAggs.map(agg => {
if (agg.schema !== 'split') {
return agg;
}

splitCount++;
if (splitCount === 1) {
return agg; // leave the first split agg unchanged
}
agg.schema = 'bucket';
// the `row` param is exclusively used by split aggs, so we remove it
agg.params = omit(agg.params, ['row']);
return agg;
});

if (splitCount <= 1) {
return; // do nothing; we only want to touch tables with multiple split aggs
}

appState.vis.aggs = migratedAggs;
appState.save();
}
4 changes: 3 additions & 1 deletion src/legacy/core_plugins/table_vis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { resolve } from 'path';
import { migrations } from './migrations';

export default function (kibana) {

Expand All @@ -27,7 +28,8 @@ export default function (kibana) {
'plugins/table_vis/table_vis'
],
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
}
migrations,
},
});

}
59 changes: 59 additions & 0 deletions src/legacy/core_plugins/table_vis/migrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { cloneDeep, get, omit } from 'lodash';

export const migrations = {
visualization: {
'7.0.0': (doc) => {
try {
const visState = JSON.parse(doc.attributes.visState);
if (get(visState, 'type') !== 'table') {
return doc; // do nothing; we only want to touch tables
}

let splitCount = 0;
visState.aggs = visState.aggs.map(agg => {
if (agg.schema !== 'split') {
return agg;
}

splitCount++;
if (splitCount === 1) {
return agg; // leave the first split agg unchanged
}
agg.schema = 'bucket';
// the `row` param is exclusively used by split aggs, so we remove it
agg.params = omit(agg.params, ['row']);
return agg;
});

if (splitCount <= 1) {
return doc; // do nothing; we only want to touch tables with multiple split aggs
}

const newDoc = cloneDeep(doc);
newDoc.attributes.visState = JSON.stringify(visState);
return newDoc;
} catch (e) {
throw new Error(`Failure attempting to migrate saved object '${doc.attributes.title}' - ${e}`);
}
}
}
};
184 changes: 184 additions & 0 deletions src/legacy/core_plugins/table_vis/migrations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { migrations } from './migrations';

describe('table vis migrations', () => {

describe('7.0.0', () => {
const migrate = doc => migrations.visualization['7.0.0'](doc);
const generateDoc = ({ type, aggs }) => ({
attributes: {
title: 'My Vis',
description: 'This is my super cool vis.',
visState: JSON.stringify({ type, aggs }),
uiStateJSON: '{}',
version: 1,
kibanaSavedObjectMeta: {
searchSourceJSON: '{}'
}
}
});

it('should return a new object if vis is table and has multiple split aggs', () => {
const aggs = [
{
id: '1',
schema: 'metric',
params: {}
},
{
id: '2',
schema: 'split',
params: { foo: 'bar', row: true }
},
{
id: '3',
schema: 'split',
params: { hey: 'ya', row: false }
},
];
const tableDoc = generateDoc({ type: 'table', aggs });
const expected = tableDoc;
const actual = migrate(tableDoc);
expect(actual).not.toBe(expected);
});

it('should not touch any vis that is not table', () => {
const aggs = [];
const pieDoc = generateDoc({ type: 'pie', aggs });
const expected = pieDoc;
const actual = migrate(pieDoc);
expect(actual).toBe(expected);
});

it('should not change values in any vis that is not table', () => {
const aggs = [
{
id: '1',
schema: 'metric',
params: {}
},
{
id: '2',
schema: 'split',
params: { foo: 'bar', row: true }
},
{
id: '3',
schema: 'segment',
params: { hey: 'ya' }
}
];
const pieDoc = generateDoc({ type: 'pie', aggs });
const expected = pieDoc;
const actual = migrate(pieDoc);
expect(actual).toEqual(expected);
});

it('should not touch table vis if there are not multiple split aggs', () => {
const aggs = [
{
id: '1',
schema: 'metric',
params: {}
},
{
id: '2',
schema: 'split',
params: { foo: 'bar', row: true }
}
];
const tableDoc = generateDoc({ type: 'table', aggs });
const expected = tableDoc;
const actual = migrate(tableDoc);
expect(actual).toBe(expected);
});

it('should change all split aggs to `bucket` except the first', () => {
const aggs = [
{
id: '1',
schema: 'metric',
params: {}
},
{
id: '2',
schema: 'split',
params: { foo: 'bar', row: true }
},
{
id: '3',
schema: 'split',
params: { hey: 'ya', row: false }
},
{
id: '4',
schema: 'bucket',
params: { heyyy: 'yaaa' }
}
];
const expected = ['metric', 'split', 'bucket', 'bucket'];
const migrated = migrate(generateDoc({ type: 'table', aggs }));
const actual = JSON.parse(migrated.attributes.visState);
expect(actual.aggs.map(agg => agg.schema)).toEqual(expected);
});

it('should remove `rows` param from any aggs that are not `split`', () => {
const aggs = [
{
id: '1',
schema: 'metric',
params: {}
},
{
id: '2',
schema: 'split',
params: { foo: 'bar', row: true }
},
{
id: '3',
schema: 'split',
params: { hey: 'ya', row: false }
}
];
const expected = [{}, { foo: 'bar', row: true }, { hey: 'ya' }];
const migrated = migrate(generateDoc({ type: 'table', aggs }));
const actual = JSON.parse(migrated.attributes.visState);
expect(actual.aggs.map(agg => agg.params)).toEqual(expected);
});

it('should throw with a reference to the doc name if something goes wrong', () => {
const doc = {
attributes: {
title: 'My Vis',
description: 'This is my super cool vis.',
visState: '!/// Intentionally malformed JSON ///!',
uiStateJSON: '{}',
version: 1,
kibanaSavedObjectMeta: {
searchSourceJSON: '{}'
}
}
};
expect(() => migrate(doc)).toThrowError(/My Vis/);
});
});

});
2 changes: 2 additions & 0 deletions src/legacy/core_plugins/table_vis/public/table_vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ function TableVisTypeProvider(Private) {
title: i18n.translate('tableVis.tableVisEditorConfig.schemas.splitTitle', {
defaultMessage: 'Split Table',
}),
min: 0,
max: 1,
aggFilter: ['!filter']
}
])
Expand Down
3 changes: 3 additions & 0 deletions test/api_integration/apis/saved_objects/bulk_get.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export default function ({ getService }) {
saved_objects: [
{
id: 'dd7caf20-9efd-11e7-acb3-3dab96693fab',
migrationVersion: {
visualization: '7.0.0'
},
type: 'visualization',
updated_at: '2017-09-21T18:51:23.794Z',
version: resp.body.saved_objects[0].version,
Expand Down
Loading

0 comments on commit c6f0595

Please sign in to comment.