-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Maps] fix term join agg key collision #63324
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6e0c937
[Maps] fix term join agg key collision
nreese 6ec1ebf
Merge branch 'master' into join_agg_key
elasticmachine 238639a
Merge branch 'master' of github.com:elastic/kibana into join_agg_key
nreese e2ac7d7
fix tslint and jest errors
nreese 13cad80
merge with master
nreese 2a12c76
fix join functional test
nreese 1f7080b
Merge branch 'master' into join_agg_key
elasticmachine 3c6c15d
revert LayerDescriptor union and cast to VectorLayerDescriptor instead
nreese 7f49c09
move getJoinKey out of constants and into its own file
nreese 2343f47
Merge branch 'master' into join_agg_key
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
x-pack/legacy/plugins/maps/common/migrations/join_agg_key.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import { LAYER_TYPE } from '../constants'; | ||
| import { migrateJoinAggKey } from './join_agg_key'; | ||
|
|
||
| describe('migrateJoinAggKey', () => { | ||
| const joins = [ | ||
| { | ||
| leftField: 'machine.os', | ||
| right: { | ||
| id: '9055b4aa-136a-4b6d-90ab-9f94ccfe5eb5', | ||
| indexPatternTitle: 'kibana_sample_data_logs', | ||
| term: 'machine.os.keyword', | ||
| metrics: [ | ||
| { | ||
| type: 'avg', | ||
| field: 'bytes', | ||
| }, | ||
| { | ||
| type: 'count', | ||
| }, | ||
| ], | ||
| whereQuery: { | ||
| query: 'bytes > 10000', | ||
| language: 'kuery', | ||
| }, | ||
| indexPatternRefName: 'layer_1_join_0_index_pattern', | ||
| }, | ||
| }, | ||
| { | ||
| leftField: 'machine.os', | ||
| right: { | ||
| id: '9a7f4e71-9500-4512-82f1-b7eaee3d87ff', | ||
| indexPatternTitle: 'kibana_sample_data_logs', | ||
| term: 'machine.os.keyword', | ||
| whereQuery: { | ||
| query: 'bytes < 10000', | ||
| language: 'kuery', | ||
| }, | ||
| metrics: [ | ||
| { | ||
| type: 'avg', | ||
| field: 'bytes', | ||
| }, | ||
| ], | ||
| indexPatternRefName: 'layer_1_join_1_index_pattern', | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| test('Should handle missing layerListJSON attribute', () => { | ||
| const attributes = { | ||
| title: 'my map', | ||
| }; | ||
| expect(migrateJoinAggKey({ attributes })).toEqual({ | ||
| title: 'my map', | ||
| }); | ||
| }); | ||
|
|
||
| test('Should migrate vector styles from legacy join agg key to new join agg key', () => { | ||
| const layerListJSON = JSON.stringify([ | ||
| { | ||
| type: LAYER_TYPE.VECTOR, | ||
| joins, | ||
| style: { | ||
| properties: { | ||
| fillColor: { | ||
| type: 'DYNAMIC', | ||
| options: { | ||
| color: 'Blues', | ||
| colorCategory: 'palette_0', | ||
| field: { | ||
| name: | ||
| '__kbnjoin__avg_of_bytes_groupby_kibana_sample_data_logs.machine.os.keyword', | ||
| origin: 'join', | ||
| }, | ||
| fieldMetaOptions: { | ||
| isEnabled: true, | ||
| sigma: 3, | ||
| }, | ||
| type: 'ORDINAL', | ||
| }, | ||
| }, | ||
| lineColor: { | ||
| type: 'DYNAMIC', | ||
| options: { | ||
| color: 'Blues', | ||
| colorCategory: 'palette_0', | ||
| field: { | ||
| name: '__kbnjoin__count_groupby_kibana_sample_data_logs.machine.os.keyword', | ||
| origin: 'join', | ||
| }, | ||
| fieldMetaOptions: { | ||
| isEnabled: true, | ||
| sigma: 3, | ||
| }, | ||
| type: 'ORDINAL', | ||
| }, | ||
| }, | ||
| lineWidth: { | ||
| type: 'DYNAMIC', | ||
| options: { | ||
| color: 'Blues', | ||
| colorCategory: 'palette_0', | ||
| field: { | ||
| name: 'mySourceField', | ||
| origin: 'source', | ||
| }, | ||
| fieldMetaOptions: { | ||
| isEnabled: true, | ||
| sigma: 3, | ||
| }, | ||
| type: 'ORDINAL', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ]); | ||
| const attributes = { | ||
| title: 'my map', | ||
| layerListJSON, | ||
| }; | ||
| const { layerListJSON: migratedLayerListJSON } = migrateJoinAggKey({ attributes }); | ||
| const migratedLayerList = JSON.parse(migratedLayerListJSON!); | ||
| expect(migratedLayerList[0].style.properties.fillColor.options.field.name).toBe( | ||
| '__kbnjoin__avg_of_bytes__9055b4aa-136a-4b6d-90ab-9f94ccfe5eb5' | ||
| ); | ||
| expect(migratedLayerList[0].style.properties.lineColor.options.field.name).toBe( | ||
| '__kbnjoin__count__9055b4aa-136a-4b6d-90ab-9f94ccfe5eb5' | ||
| ); | ||
| expect(migratedLayerList[0].style.properties.lineWidth.options.field.name).toBe( | ||
| 'mySourceField' | ||
| ); | ||
| }); | ||
| }); |
109 changes: 109 additions & 0 deletions
109
x-pack/legacy/plugins/maps/common/migrations/join_agg_key.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import _ from 'lodash'; | ||
| import { | ||
| AGG_DELIMITER, | ||
| AGG_TYPE, | ||
| FIELD_ORIGIN, | ||
| getJoinAggKey, | ||
| JOIN_FIELD_NAME_PREFIX, | ||
| LAYER_TYPE, | ||
| VECTOR_STYLES, | ||
| } from '../constants'; | ||
| import { AggDescriptor, JoinDescriptor, LayerDescriptor } from '../descriptor_types'; | ||
| import { MapSavedObjectAttributes } from '../../../../../plugins/maps/common/map_saved_object_type'; | ||
|
|
||
| const GROUP_BY_DELIMITER = '_groupby_'; | ||
|
|
||
| function getLegacyAggKey({ | ||
| aggType, | ||
| aggFieldName, | ||
| indexPatternTitle, | ||
| termFieldName, | ||
| }: { | ||
| aggType: AGG_TYPE; | ||
| aggFieldName?: string; | ||
| indexPatternTitle: string; | ||
| termFieldName: string; | ||
| }): string { | ||
| const metricKey = | ||
| aggType !== AGG_TYPE.COUNT ? `${aggType}${AGG_DELIMITER}${aggFieldName}` : aggType; | ||
| return `${JOIN_FIELD_NAME_PREFIX}${metricKey}${GROUP_BY_DELIMITER}${indexPatternTitle}.${termFieldName}`; | ||
| } | ||
|
|
||
| function parseLegacyAggKey(legacyAggKey: string): { aggType: AGG_TYPE; aggFieldName?: string } { | ||
| const groupBySplit = legacyAggKey | ||
| .substring(JOIN_FIELD_NAME_PREFIX.length) | ||
| .split(GROUP_BY_DELIMITER); | ||
| const metricKey = groupBySplit[0]; | ||
| const metricKeySplit = metricKey.split(AGG_DELIMITER); | ||
| return { | ||
| aggType: metricKeySplit[0] as AGG_TYPE, | ||
| aggFieldName: metricKeySplit.length === 2 ? metricKeySplit[1] : undefined, | ||
| }; | ||
| } | ||
|
|
||
| export function migrateJoinAggKey({ | ||
| attributes, | ||
| }: { | ||
| attributes: MapSavedObjectAttributes; | ||
| }): MapSavedObjectAttributes { | ||
| if (!attributes || !attributes.layerListJSON) { | ||
| return attributes; | ||
| } | ||
|
|
||
| const layerList: LayerDescriptor[] = JSON.parse(attributes.layerListJSON); | ||
| layerList.forEach((layerDescriptor: LayerDescriptor) => { | ||
| if ( | ||
| (layerDescriptor.type === LAYER_TYPE.VECTOR || | ||
| layerDescriptor.type === LAYER_TYPE.BLENDED_VECTOR) && | ||
| layerDescriptor.style && | ||
| layerDescriptor.joins && | ||
| layerDescriptor.joins.length | ||
| ) { | ||
| const legacyJoinFields = new Map<string, JoinDescriptor>(); | ||
| layerDescriptor.joins.forEach((joinDescriptor: JoinDescriptor) => { | ||
| _.get(joinDescriptor, 'right.metrics', []).forEach((aggDescriptor: AggDescriptor) => { | ||
| const legacyAggKey = getLegacyAggKey({ | ||
| aggType: aggDescriptor.type, | ||
| aggFieldName: aggDescriptor.field, | ||
| indexPatternTitle: _.get(joinDescriptor, 'right.indexPatternTitle', ''), | ||
| termFieldName: _.get(joinDescriptor, 'right.term', ''), | ||
| }); | ||
| // The legacy getAggKey implemenation has a naming collision bug where | ||
| // aggType, aggFieldName, indexPatternTitle, and termFieldName would result in the identical aggKey. | ||
| // The VectorStyle implemenation used the first matching join descriptor | ||
| // so, in the event of a name collision, the first join descriptor will be used here as well. | ||
| if (!legacyJoinFields.has(legacyAggKey)) { | ||
| legacyJoinFields.set(legacyAggKey, joinDescriptor); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| Object.keys(layerDescriptor.style.properties).forEach(key => { | ||
| const style: any = layerDescriptor.style!.properties[key as VECTOR_STYLES]; | ||
| if (_.get(style, 'options.field.origin') === FIELD_ORIGIN.JOIN) { | ||
| const joinDescriptor = legacyJoinFields.get(style.options.field.name); | ||
| if (joinDescriptor) { | ||
| const { aggType, aggFieldName } = parseLegacyAggKey(style.options.field.name); | ||
| // Update legacy join agg key to new join agg key | ||
| style.options.field.name = getJoinAggKey({ | ||
| aggType, | ||
| aggFieldName, | ||
| rightSourceId: joinDescriptor.right.id!, | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| return { | ||
| ...attributes, | ||
| layerListJSON: JSON.stringify(layerList), | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.