-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add migration to convert useTopTerm to scalingType
- Loading branch information
Showing
6 changed files
with
150 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
26 changes: 26 additions & 0 deletions
26
x-pack/legacy/plugins/maps/common/map_saved_object_types.d.ts
This file contains 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,26 @@ | ||
/* | ||
* 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. | ||
*/ | ||
/* eslint-disable @typescript-eslint/consistent-type-definitions */ | ||
|
||
export type MapSavedObjectAttributes = { | ||
title?: string; | ||
description?: string; | ||
mapStateJSON?: string; | ||
layerListJSON?: string; | ||
uiStateJSON?: string; | ||
bounds?: { | ||
type?: string; | ||
coordinates?: []; | ||
}; | ||
}; | ||
|
||
export type MapSavedObject = { | ||
[key: string]: any; | ||
title: string; | ||
id?: string; | ||
type?: string; | ||
attributes?: MapSavedObjectAttributes; | ||
}; |
56 changes: 56 additions & 0 deletions
56
x-pack/legacy/plugins/maps/common/migrations/scaling_type.test.ts
This file contains 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,56 @@ | ||
/* | ||
* 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 { migrateUseTopHitsToScalingType } from './scaling_type'; | ||
|
||
describe('migrateUseTopHitsToScalingType', () => { | ||
test('Should handle missing layerListJSON attribute', () => { | ||
const attributes = { | ||
title: 'my map', | ||
}; | ||
expect(migrateUseTopHitsToScalingType({ attributes })).toEqual({ | ||
title: 'my map', | ||
}); | ||
}); | ||
|
||
test('Should migrate useTopHits: true to scalingType TOP_HITS for ES documents sources', () => { | ||
const layerListJSON = JSON.stringify([ | ||
{ | ||
sourceDescriptor: { | ||
type: 'ES_SEARCH', | ||
useTopHits: true, | ||
}, | ||
}, | ||
]); | ||
const attributes = { | ||
title: 'my map', | ||
layerListJSON, | ||
}; | ||
expect(migrateUseTopHitsToScalingType({ attributes })).toEqual({ | ||
title: 'my map', | ||
layerListJSON: '[{"sourceDescriptor":{"type":"ES_SEARCH","scalingType":"TOP_HITS"}}]', | ||
}); | ||
}); | ||
|
||
test('Should migrate useTopHits: false to scalingType LIMIT for ES documents sources', () => { | ||
const layerListJSON = JSON.stringify([ | ||
{ | ||
sourceDescriptor: { | ||
type: 'ES_SEARCH', | ||
useTopHits: false, | ||
}, | ||
}, | ||
]); | ||
const attributes = { | ||
title: 'my map', | ||
layerListJSON, | ||
}; | ||
expect(migrateUseTopHitsToScalingType({ attributes })).toEqual({ | ||
title: 'my map', | ||
layerListJSON: '[{"sourceDescriptor":{"type":"ES_SEARCH","scalingType":"LIMIT"}}]', | ||
}); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
x-pack/legacy/plugins/maps/common/migrations/scaling_type.ts
This file contains 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,45 @@ | ||
/* | ||
* 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 { ES_SEARCH, SCALING_TYPES } from '../constants'; | ||
import { LayerDescriptor } from '../descriptor_types'; | ||
import { MapSavedObject } from '../map_saved_object_types'; | ||
|
||
function isEsDocumentSource(layerDescriptor: LayerDescriptor) { | ||
const sourceType = _.get(layerDescriptor, 'sourceDescriptor.type'); | ||
return sourceType === ES_SEARCH; | ||
} | ||
|
||
export function migrateUseTopHitsToScalingType({ attributes }: Partial<MapSavedObject>) { | ||
if (!attributes || !attributes.layerListJSON) { | ||
return attributes; | ||
} | ||
|
||
const layerList: LayerDescriptor[] = JSON.parse(attributes.layerListJSON); | ||
layerList.forEach((layerDescriptor: LayerDescriptor) => { | ||
if (isEsDocumentSource(layerDescriptor)) { | ||
if (_.has(layerDescriptor, 'sourceDescriptor.useTopHits')) { | ||
// @ts-ignore TS is too stupid to figure out that scalingType is available on sourceDescriptor | ||
layerDescriptor.sourceDescriptor.scalingType = _.get( | ||
layerDescriptor, | ||
'sourceDescriptor.useTopHits', | ||
false | ||
) | ||
? SCALING_TYPES.TOP_HITS | ||
: SCALING_TYPES.LIMIT; | ||
// @ts-ignore useTopHits no longer in type definition but that does not mean its not in live data | ||
// hence the entire point of this method | ||
delete layerDescriptor.sourceDescriptor.useTopHits; | ||
} | ||
} | ||
}); | ||
|
||
return { | ||
...attributes, | ||
layerListJSON: JSON.stringify(layerList), | ||
}; | ||
} |
This file contains 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 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