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

Remove nested table splits from table vis #26057

Merged
merged 17 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from 15 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 @@ -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 @@ -264,6 +265,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.
lukeelmers marked this conversation as resolved.
Show resolved Hide resolved
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if there was a registry so plugins that create visualizations can migrate their app state as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trevan Your timing is perfect; I just opened #29122 to discuss this exact issue 😃

Copy link
Member Author

@lukeelmers lukeelmers Jan 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, you're talking about appState -- I misread. That's a good idea, we have only used this one other place (dashboards), but it's good to hear this is something you feel would be useful to expose to plugins too!

Perhaps we could implement a solution that mimics whatever resolution we come to for #29122.

// 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
lukeelmers marked this conversation as resolved.
Show resolved Hide resolved
}

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/);
});
});

});
4 changes: 2 additions & 2 deletions src/legacy/core_plugins/table_vis/public/table_vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { VisFactoryProvider } from 'ui/vis/vis_factory';
import { Schemas } from 'ui/vis/editors/default/schemas';
import tableVisTemplate from './table_vis.html';
import { VisTypesRegistryProvider } from 'ui/registry/vis_types';
import { legacyTableResponseHandler } from './legacy_response_handler';

// we need to load the css ourselves

Expand Down Expand Up @@ -101,11 +100,12 @@ function TableVisTypeProvider(Private) {
title: i18n.translate('tableVis.tableVisEditorConfig.schemas.splitTitle', {
defaultMessage: 'Split Table',
}),
min: 0,
max: 1,
aggFilter: ['!filter']
}
])
},
responseHandler: legacyTableResponseHandler,
responseHandlerConfig: {
asAggConfigResults: true
},
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