|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import _ from 'lodash'; |
| 8 | +import { |
| 9 | + AGG_DELIMITER, |
| 10 | + AGG_TYPE, |
| 11 | + FIELD_ORIGIN, |
| 12 | + JOIN_FIELD_NAME_PREFIX, |
| 13 | + LAYER_TYPE, |
| 14 | + VECTOR_STYLES, |
| 15 | +} from '../constants'; |
| 16 | +import { getJoinAggKey } from '../get_join_key'; |
| 17 | +import { |
| 18 | + AggDescriptor, |
| 19 | + JoinDescriptor, |
| 20 | + LayerDescriptor, |
| 21 | + VectorLayerDescriptor, |
| 22 | +} from '../descriptor_types'; |
| 23 | +import { MapSavedObjectAttributes } from '../../../../../plugins/maps/common/map_saved_object_type'; |
| 24 | + |
| 25 | +const GROUP_BY_DELIMITER = '_groupby_'; |
| 26 | + |
| 27 | +function getLegacyAggKey({ |
| 28 | + aggType, |
| 29 | + aggFieldName, |
| 30 | + indexPatternTitle, |
| 31 | + termFieldName, |
| 32 | +}: { |
| 33 | + aggType: AGG_TYPE; |
| 34 | + aggFieldName?: string; |
| 35 | + indexPatternTitle: string; |
| 36 | + termFieldName: string; |
| 37 | +}): string { |
| 38 | + const metricKey = |
| 39 | + aggType !== AGG_TYPE.COUNT ? `${aggType}${AGG_DELIMITER}${aggFieldName}` : aggType; |
| 40 | + return `${JOIN_FIELD_NAME_PREFIX}${metricKey}${GROUP_BY_DELIMITER}${indexPatternTitle}.${termFieldName}`; |
| 41 | +} |
| 42 | + |
| 43 | +function parseLegacyAggKey(legacyAggKey: string): { aggType: AGG_TYPE; aggFieldName?: string } { |
| 44 | + const groupBySplit = legacyAggKey |
| 45 | + .substring(JOIN_FIELD_NAME_PREFIX.length) |
| 46 | + .split(GROUP_BY_DELIMITER); |
| 47 | + const metricKey = groupBySplit[0]; |
| 48 | + const metricKeySplit = metricKey.split(AGG_DELIMITER); |
| 49 | + return { |
| 50 | + aggType: metricKeySplit[0] as AGG_TYPE, |
| 51 | + aggFieldName: metricKeySplit.length === 2 ? metricKeySplit[1] : undefined, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +export function migrateJoinAggKey({ |
| 56 | + attributes, |
| 57 | +}: { |
| 58 | + attributes: MapSavedObjectAttributes; |
| 59 | +}): MapSavedObjectAttributes { |
| 60 | + if (!attributes || !attributes.layerListJSON) { |
| 61 | + return attributes; |
| 62 | + } |
| 63 | + |
| 64 | + const layerList: LayerDescriptor[] = JSON.parse(attributes.layerListJSON); |
| 65 | + layerList.forEach((layerDescriptor: LayerDescriptor) => { |
| 66 | + if ( |
| 67 | + layerDescriptor.type === LAYER_TYPE.VECTOR || |
| 68 | + layerDescriptor.type === LAYER_TYPE.BLENDED_VECTOR |
| 69 | + ) { |
| 70 | + const vectorLayerDescriptor = layerDescriptor as VectorLayerDescriptor; |
| 71 | + |
| 72 | + if ( |
| 73 | + !vectorLayerDescriptor.style || |
| 74 | + !vectorLayerDescriptor.joins || |
| 75 | + vectorLayerDescriptor.joins.length === 0 |
| 76 | + ) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const legacyJoinFields = new Map<string, JoinDescriptor>(); |
| 81 | + vectorLayerDescriptor.joins.forEach((joinDescriptor: JoinDescriptor) => { |
| 82 | + _.get(joinDescriptor, 'right.metrics', []).forEach((aggDescriptor: AggDescriptor) => { |
| 83 | + const legacyAggKey = getLegacyAggKey({ |
| 84 | + aggType: aggDescriptor.type, |
| 85 | + aggFieldName: aggDescriptor.field, |
| 86 | + indexPatternTitle: _.get(joinDescriptor, 'right.indexPatternTitle', ''), |
| 87 | + termFieldName: _.get(joinDescriptor, 'right.term', ''), |
| 88 | + }); |
| 89 | + // The legacy getAggKey implemenation has a naming collision bug where |
| 90 | + // aggType, aggFieldName, indexPatternTitle, and termFieldName would result in the identical aggKey. |
| 91 | + // The VectorStyle implemenation used the first matching join descriptor |
| 92 | + // so, in the event of a name collision, the first join descriptor will be used here as well. |
| 93 | + if (!legacyJoinFields.has(legacyAggKey)) { |
| 94 | + legacyJoinFields.set(legacyAggKey, joinDescriptor); |
| 95 | + } |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + Object.keys(vectorLayerDescriptor.style.properties).forEach(key => { |
| 100 | + const style: any = vectorLayerDescriptor.style!.properties[key as VECTOR_STYLES]; |
| 101 | + if (_.get(style, 'options.field.origin') === FIELD_ORIGIN.JOIN) { |
| 102 | + const joinDescriptor = legacyJoinFields.get(style.options.field.name); |
| 103 | + if (joinDescriptor) { |
| 104 | + const { aggType, aggFieldName } = parseLegacyAggKey(style.options.field.name); |
| 105 | + // Update legacy join agg key to new join agg key |
| 106 | + style.options.field.name = getJoinAggKey({ |
| 107 | + aggType, |
| 108 | + aggFieldName, |
| 109 | + rightSourceId: joinDescriptor.right.id!, |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + return { |
| 118 | + ...attributes, |
| 119 | + layerListJSON: JSON.stringify(layerList), |
| 120 | + }; |
| 121 | +} |
0 commit comments