Skip to content

Commit

Permalink
add migration to convert useTopTerm to scalingType
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Mar 13, 2020
1 parent 4ab5e57 commit 8db2894
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 34 deletions.
18 changes: 15 additions & 3 deletions x-pack/legacy/plugins/maps/common/descriptor_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* eslint-disable @typescript-eslint/consistent-type-definitions */

import { DataRequestDescriptor } from './data_request_descriptor_types';
import { AGG_TYPE, GRID_RESOLUTION, RENDER_AS, SORT_ORDER } from './constants';
import { AGG_TYPE, GRID_RESOLUTION, RENDER_AS, SORT_ORDER, SCALING_TYPES } from './constants';

export type AbstractSourceDescriptor = {
id?: string;
Expand Down Expand Up @@ -50,7 +50,7 @@ export type ESSearchSourceDescriptor = AbstractESSourceDescriptor & {
tooltipProperties?: string[];
sortField?: string;
sortOrder?: SORT_ORDER;
useTopHits?: boolean;
scalingType?: SCALING_TYPES;
topHitsSplitField?: string;
topHitsSize?: number;
};
Expand Down Expand Up @@ -89,6 +89,18 @@ export type XYZTMSSourceDescriptor = {
urlTemplate: string;
};

export type SourceDescriptor =
| EMSTMSSourceDescriptor
| EMSFileSourceDescriptor
| ESGeoGridSourceDescriptor
| ESSearchSourceDescriptor
| ESPewPewSourceDescriptor
| ESTermSourceDescriptor
| KibanaRegionmapSourceDescriptor
| KibanaTilemapSourceDescriptor
| WMSSourceDescriptor
| XYZTMSSourceDescriptor;

export type JoinDescriptor = {
leftField: string;
right: ESTermSourceDescriptor;
Expand All @@ -103,7 +115,7 @@ export type LayerDescriptor = {
label?: string;
minZoom?: number;
maxZoom?: number;
sourceDescriptor: AbstractSourceDescriptor;
sourceDescriptor: SourceDescriptor;
type?: string;
visible?: boolean;
};
Expand Down
26 changes: 26 additions & 0 deletions x-pack/legacy/plugins/maps/common/map_saved_object_types.d.ts
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 x-pack/legacy/plugins/maps/common/migrations/scaling_type.test.ts
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 x-pack/legacy/plugins/maps/common/migrations/scaling_type.ts
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),
};
}
6 changes: 4 additions & 2 deletions x-pack/legacy/plugins/maps/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { topHitsTimeToSort } from './common/migrations/top_hits_time_to_sort';
import { moveApplyGlobalQueryToSources } from './common/migrations/move_apply_global_query';
import { addFieldMetaOptions } from './common/migrations/add_field_meta_options';
import { migrateSymbolStyleDescriptor } from './common/migrations/migrate_symbol_style_descriptor';
import { migrateUseTopHitsToScalingType } from './common/migrations/scaling_type';

export const migrations = {
map: {
Expand Down Expand Up @@ -48,11 +49,12 @@ export const migrations = {
};
},
'7.7.0': doc => {
const attributes = migrateSymbolStyleDescriptor(doc);
const attributesPhase1 = migrateSymbolStyleDescriptor(doc);
const attributesPhase2 = migrateUseTopHitsToScalingType({ attributes: attributesPhase1 });

return {
...doc,
attributes,
attributes: attributesPhase2,
};
},
},
Expand Down
33 changes: 4 additions & 29 deletions x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
// @ts-ignore
} from '../../common/constants';
import { LayerDescriptor } from '../../common/descriptor_types';
import { MapSavedObject } from '../../common/map_saved_object_types';

interface IStats {
[key: string]: {
Expand All @@ -32,33 +33,6 @@ interface ILayerTypeCount {
[key: string]: number;
}

interface IMapSavedObject {
[key: string]: any;
fields: IFieldType[];
title: string;
id?: string;
type?: string;
timeFieldName?: string;
fieldFormatMap?: Record<
string,
{
id: string;
params: unknown;
}
>;
attributes?: {
title?: string;
description?: string;
mapStateJSON?: string;
layerListJSON?: string;
uiStateJSON?: string;
bounds?: {
type?: string;
coordinates?: [];
};
};
}

function getUniqueLayerCounts(layerCountsList: ILayerTypeCount[], mapsCount: number) {
const uniqueLayerTypes = _.uniq(_.flatten(layerCountsList.map(lTypes => Object.keys(lTypes))));

Expand Down Expand Up @@ -102,7 +76,7 @@ export function buildMapsTelemetry({
indexPatternSavedObjects,
settings,
}: {
mapSavedObjects: IMapSavedObject[];
mapSavedObjects: MapSavedObject[];
indexPatternSavedObjects: IIndexPattern[];
settings: SavedObjectAttribute;
}): SavedObjectAttributes {
Expand All @@ -114,6 +88,7 @@ export function buildMapsTelemetry({
const mapsCount = layerLists.length;

const dataSourcesCount = layerLists.map(lList => {
// @ts-ignore
const sourceIdList = lList.map((layer: LayerDescriptor) => layer.sourceDescriptor.id);
return _.uniq(sourceIdList).length;
});
Expand Down Expand Up @@ -183,7 +158,7 @@ export async function getMapsTelemetry(
savedObjectsClient: SavedObjectsClientContract,
config: Function
) {
const mapSavedObjects: IMapSavedObject[] = await getMapSavedObjects(savedObjectsClient);
const mapSavedObjects: MapSavedObject[] = await getMapSavedObjects(savedObjectsClient);
const indexPatternSavedObjects: IIndexPattern[] = await getIndexPatternSavedObjects(
savedObjectsClient
);
Expand Down

0 comments on commit 8db2894

Please sign in to comment.