Skip to content

Commit

Permalink
[Maps] Convert ES-sources to typescript (#81951) (#82717)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck authored Nov 5, 2020
1 parent ba05688 commit cb0bc2f
Show file tree
Hide file tree
Showing 46 changed files with 1,065 additions and 878 deletions.
39 changes: 20 additions & 19 deletions src/plugins/region_map/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,29 @@

import { schema, TypeOf } from '@kbn/config-schema';

export const configSchema = schema.object({
includeElasticMapsService: schema.boolean({ defaultValue: true }),
layers: schema.arrayOf(
const layerConfigSchema = schema.object({
url: schema.string(),
format: schema.object({
type: schema.string({ defaultValue: 'geojson' }),
}),
meta: schema.object({
feature_collection_path: schema.string({ defaultValue: 'data' }),
}),
attribution: schema.string(),
name: schema.string(),
fields: schema.arrayOf(
schema.object({
url: schema.string(),
format: schema.object({
type: schema.string({ defaultValue: 'geojson' }),
}),
meta: schema.object({
feature_collection_path: schema.string({ defaultValue: 'data' }),
}),
attribution: schema.string(),
name: schema.string(),
fields: schema.arrayOf(
schema.object({
name: schema.string(),
description: schema.string(),
})
),
}),
{ defaultValue: [] }
description: schema.string(),
})
),
});

export const configSchema = schema.object({
includeElasticMapsService: schema.boolean({ defaultValue: true }),
layers: schema.arrayOf(layerConfigSchema, { defaultValue: [] }),
});

export type LayerConfig = TypeOf<typeof layerConfigSchema>;

export type ConfigSchema = TypeOf<typeof configSchema>;
10 changes: 5 additions & 5 deletions x-pack/plugins/maps/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ export enum LAYER_TYPE {
TILED_VECTOR = 'TILED_VECTOR', // similar to a regular vector-layer, but it consumes the data as .mvt tilea iso GeoJson. It supports similar ad-hoc configurations like a regular vector layer (E.g. using IVectorStyle), although there is some loss of functionality e.g. does not support term joining
}

export enum SORT_ORDER {
ASC = 'asc',
DESC = 'desc',
}

export enum SOURCE_TYPES {
EMS_TMS = 'EMS_TMS',
EMS_FILE = 'EMS_FILE',
Expand Down Expand Up @@ -237,6 +232,11 @@ export enum SCALING_TYPES {
MVT = 'MVT',
}

export enum FORMAT_TYPE {
GEOJSON = 'geojson',
TOPOJSON = 'topojson',
}

export enum MVT_FIELD_TYPE {
STRING = 'String',
NUMBER = 'Number',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/
/* eslint-disable @typescript-eslint/consistent-type-definitions */

import { RENDER_AS, SORT_ORDER, SCALING_TYPES } from '../constants';
import { Query } from 'src/plugins/data/public';
import { SortDirection } from 'src/plugins/data/common/search';
import { RENDER_AS, SCALING_TYPES } from '../constants';
import { MapExtent, MapQuery } from './map_descriptor';
import { Filter, TimeRange } from '../../../../../src/plugins/data/common';

Expand All @@ -22,7 +24,7 @@ export type MapFilters = {

type ESSearchSourceSyncMeta = {
sortField: string;
sortOrder: SORT_ORDER;
sortOrder: SortDirection;
scalingType: SCALING_TYPES;
topHitsSplitField: string;
topHitsSize: number;
Expand All @@ -45,7 +47,7 @@ export type VectorSourceRequestMeta = MapFilters & {
export type VectorJoinSourceRequestMeta = MapFilters & {
applyGlobalQuery: boolean;
fieldNames: string[];
sourceQuery: MapQuery;
sourceQuery?: Query;
};

export type VectorStyleRequestMeta = MapFilters & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@

import { FeatureCollection } from 'geojson';
import { Query } from 'src/plugins/data/public';
import {
AGG_TYPE,
GRID_RESOLUTION,
RENDER_AS,
SORT_ORDER,
SCALING_TYPES,
MVT_FIELD_TYPE,
} from '../constants';
import { SortDirection } from 'src/plugins/data/common/search';
import { AGG_TYPE, GRID_RESOLUTION, RENDER_AS, SCALING_TYPES, MVT_FIELD_TYPE } from '../constants';

export type AttributionDescriptor = {
attributionText?: string;
Expand All @@ -40,6 +34,7 @@ export type EMSFileSourceDescriptor = AbstractSourceDescriptor & {

export type AbstractESSourceDescriptor = AbstractSourceDescriptor & {
// id: UUID
id: string;
indexPatternId: string;
geoField?: string;
};
Expand All @@ -55,18 +50,20 @@ export type AbstractESAggSourceDescriptor = AbstractESSourceDescriptor & {
};

export type ESGeoGridSourceDescriptor = AbstractESAggSourceDescriptor & {
requestType?: RENDER_AS;
resolution?: GRID_RESOLUTION;
geoField: string;
requestType: RENDER_AS;
resolution: GRID_RESOLUTION;
};

export type ESSearchSourceDescriptor = AbstractESSourceDescriptor & {
geoField: string;
filterByMapBounds?: boolean;
tooltipProperties?: string[];
sortField?: string;
sortOrder?: SORT_ORDER;
sortField: string;
sortOrder: SortDirection;
scalingType: SCALING_TYPES;
topHitsSplitField?: string;
topHitsSize?: number;
topHitsSplitField: string;
topHitsSize: number;
};

export type ESPewPewSourceDescriptor = AbstractESAggSourceDescriptor & {
Expand All @@ -76,7 +73,7 @@ export type ESPewPewSourceDescriptor = AbstractESAggSourceDescriptor & {

export type ESTermSourceDescriptor = AbstractESAggSourceDescriptor & {
indexPatternTitle?: string;
term?: string; // term field name
term: string; // term field name
whereQuery?: Query;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { FeatureCollection, GeoJsonProperties } from 'geojson';
import { FeatureCollection, GeoJsonProperties, Polygon } from 'geojson';
import { MapExtent } from '../descriptor_types';
import { ES_GEO_FIELD_TYPE } from '../constants';
import { ES_GEO_FIELD_TYPE, ES_SPATIAL_RELATIONS } from '../constants';

export function scaleBounds(bounds: MapExtent, scaleFactor: number): MapExtent;

Expand All @@ -23,3 +23,31 @@ export function hitsToGeoJson(
geoFieldType: ES_GEO_FIELD_TYPE,
epochMillisFields: string[]
): FeatureCollection;

export interface ESBBox {
top_left: number[];
bottom_right: number[];
}

export interface ESGeoBoundingBoxFilter {
geo_bounding_box: {
[geoFieldName: string]: ESBBox;
};
}

export interface ESPolygonFilter {
geo_shape: {
[geoFieldName: string]: {
shape: Polygon;
relation: ES_SPATIAL_RELATIONS.INTERSECTS;
};
};
}

export function createExtentFilter(
mapExtent: MapExtent,
geoFieldName: string,
geoFieldType: ES_GEO_FIELD_TYPE
): ESPolygonFilter | ESGeoBoundingBoxFilter;

export function makeESBbox({ maxLat, maxLon, minLat, minLon }: MapExtent): ESBBox;
12 changes: 8 additions & 4 deletions x-pack/plugins/maps/common/elasticsearch_util/es_agg_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import _ from 'lodash';
import { IndexPattern, IFieldType } from '../../../../../src/plugins/data/common';
import { TOP_TERM_PERCENTAGE_SUFFIX } from '../constants';

export function getField(indexPattern: IndexPattern, fieldName: string) {
export type BucketProperties = Record<string | number, unknown>;
export type PropertiesMap = Map<string, BucketProperties>;

export function getField(indexPattern: IndexPattern, fieldName: string): IFieldType {
const field = indexPattern.fields.getByName(fieldName);
if (!field) {
throw new Error(
Expand All @@ -33,9 +36,10 @@ export function addFieldToDSL(dsl: object, field: IFieldType) {
};
}

export type BucketProperties = Record<string | number, unknown>;

export function extractPropertiesFromBucket(bucket: any, ignoreKeys: string[] = []) {
export function extractPropertiesFromBucket(
bucket: any,
ignoreKeys: string[] = []
): BucketProperties {
const properties: BucketProperties = {};
for (const key in bucket) {
if (ignoreKeys.includes(key) || !bucket.hasOwnProperty(key)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

import _ from 'lodash';
import { SOURCE_TYPES, SORT_ORDER } from '../constants';
import { SOURCE_TYPES } from '../constants';
import { SortDirection } from '../../../../../src/plugins/data/common/search';

function isEsDocumentSource(layerDescriptor) {
const sourceType = _.get(layerDescriptor, 'sourceDescriptor.type');
Expand All @@ -23,7 +24,7 @@ export function topHitsTimeToSort({ attributes }) {
if (_.has(layerDescriptor, 'sourceDescriptor.topHitsTimeField')) {
layerDescriptor.sourceDescriptor.sortField =
layerDescriptor.sourceDescriptor.topHitsTimeField;
layerDescriptor.sourceDescriptor.sortOrder = SORT_ORDER.DESC;
layerDescriptor.sourceDescriptor.sortOrder = SortDirection.desc;
delete layerDescriptor.sourceDescriptor.topHitsTimeField;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
*/

import { IField, AbstractField } from './field';
import { IKibanaRegionSource } from '../sources/kibana_regionmap_source/kibana_regionmap_source';
import { KibanaRegionmapSource } from '../sources/kibana_regionmap_source/kibana_regionmap_source';
import { FIELD_ORIGIN } from '../../../common/constants';
import { IVectorSource } from '../sources/vector_source';

export class KibanaRegionField extends AbstractField implements IField {
private readonly _source: IKibanaRegionSource;
private readonly _source: KibanaRegionmapSource;

constructor({
fieldName,
source,
origin,
}: {
fieldName: string;
source: IKibanaRegionSource;
source: KibanaRegionmapSource;
origin: FIELD_ORIGIN;
}) {
super({ fieldName, origin });
Expand Down
7 changes: 4 additions & 3 deletions x-pack/plugins/maps/public/classes/joins/inner_join.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
*/

import { Feature, GeoJsonProperties } from 'geojson';
import { IESTermSource } from '../sources/es_term_source';
import { IJoin, PropertiesMap } from './join';
import { ESTermSource } from '../sources/es_term_source';
import { IJoin } from './join';
import { JoinDescriptor } from '../../../common/descriptor_types';
import { ISource } from '../sources/source';
import { ITooltipProperty } from '../tooltips/tooltip_property';
import { IField } from '../fields/field';
import { PropertiesMap } from '../../../common/elasticsearch_util';

export class InnerJoin implements IJoin {
constructor(joinDescriptor: JoinDescriptor, leftSource: ISource);

destroy: () => void;

getRightJoinSource(): IESTermSource;
getRightJoinSource(): ESTermSource;

toDescriptor(): JoinDescriptor;

Expand Down
8 changes: 3 additions & 5 deletions x-pack/plugins/maps/public/classes/joins/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
*/

import { Feature, GeoJsonProperties } from 'geojson';
import { IESTermSource } from '../sources/es_term_source';
import { ESTermSource } from '../sources/es_term_source';
import { JoinDescriptor } from '../../../common/descriptor_types';
import { ITooltipProperty } from '../tooltips/tooltip_property';
import { IField } from '../fields/field';
import { BucketProperties } from '../../../common/elasticsearch_util';

export type PropertiesMap = Map<string, BucketProperties>;
import { PropertiesMap } from '../../../common/elasticsearch_util';

export interface IJoin {
destroy: () => void;

getRightJoinSource: () => IESTermSource;
getRightJoinSource: () => ESTermSource;

toDescriptor: () => JoinDescriptor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { ESGeoGridSource } from '../../sources/es_geo_grid_source/es_geo_grid_so
import { canSkipSourceUpdate } from '../../util/can_skip_fetch';
import { IVectorLayer } from '../vector_layer/vector_layer';
import { IESSource } from '../../sources/es_source';
import { IESAggSource } from '../../sources/es_agg_source';
import { ISource } from '../../sources/source';
import { DataRequestContext } from '../../../actions';
import { DataRequestAbortError } from '../../util/data_request';
Expand All @@ -36,9 +35,11 @@ import {
StylePropertyOptions,
LayerDescriptor,
VectorLayerDescriptor,
VectorSourceRequestMeta,
} from '../../../../common/descriptor_types';
import { IVectorSource } from '../../sources/vector_source';
import { LICENSED_FEATURES } from '../../../licensed_features';
import { ESSearchSource } from '../../sources/es_search_source/es_search_source';

const ACTIVE_COUNT_DATA_ID = 'ACTIVE_COUNT_DATA_ID';

Expand All @@ -50,7 +51,7 @@ function getAggType(dynamicProperty: IDynamicStyleProperty<DynamicStylePropertyO
return dynamicProperty.isOrdinal() ? AGG_TYPE.AVG : AGG_TYPE.TERMS;
}

function getClusterSource(documentSource: IESSource, documentStyle: IVectorStyle): IESAggSource {
function getClusterSource(documentSource: IESSource, documentStyle: IVectorStyle): ESGeoGridSource {
const clusterSourceDescriptor = ESGeoGridSource.createDescriptor({
indexPatternId: documentSource.getIndexPatternId(),
geoField: documentSource.getGeoFieldName(),
Expand All @@ -75,7 +76,7 @@ function getClusterSource(documentSource: IESSource, documentStyle: IVectorStyle

function getClusterStyleDescriptor(
documentStyle: IVectorStyle,
clusterSource: IESAggSource
clusterSource: ESGeoGridSource
): VectorStyleDescriptor {
const defaultDynamicProperties = getDefaultDynamicProperties();
const clusterStyleDescriptor: VectorStyleDescriptor = {
Expand Down Expand Up @@ -177,9 +178,9 @@ export class BlendedVectorLayer extends VectorLayer implements IVectorLayer {
}

private readonly _isClustered: boolean;
private readonly _clusterSource: IESAggSource;
private readonly _clusterSource: ESGeoGridSource;
private readonly _clusterStyle: IVectorStyle;
private readonly _documentSource: IESSource;
private readonly _documentSource: ESSearchSource;
private readonly _documentStyle: IVectorStyle;

constructor(options: BlendedVectorLayerArguments) {
Expand All @@ -188,7 +189,7 @@ export class BlendedVectorLayer extends VectorLayer implements IVectorLayer {
joins: [],
});

this._documentSource = this._source as IESSource; // VectorLayer constructor sets _source as document source
this._documentSource = this._source as ESSearchSource; // VectorLayer constructor sets _source as document source
this._documentStyle = this._style as IVectorStyle; // VectorLayer constructor sets _style as document source

this._clusterSource = getClusterSource(this._documentSource, this._documentStyle);
Expand Down Expand Up @@ -279,7 +280,7 @@ export class BlendedVectorLayer extends VectorLayer implements IVectorLayer {
async syncData(syncContext: DataRequestContext) {
const dataRequestId = ACTIVE_COUNT_DATA_ID;
const requestToken = Symbol(`layer-active-count:${this.getId()}`);
const searchFilters = this._getSearchFilters(
const searchFilters: VectorSourceRequestMeta = this._getSearchFilters(
syncContext.dataFilters,
this.getSource(),
this.getCurrentStyle()
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/maps/public/classes/layers/layer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ export class AbstractLayer implements ILayer {
return this._descriptor.query ? this._descriptor.query : null;
}

async getImmutableSourceProperties() {
async getImmutableSourceProperties(): Promise<ImmutableSourceProperty[]> {
const source = this.getSource();
return await source.getImmutableProperties();
}
Expand Down
Loading

0 comments on commit cb0bc2f

Please sign in to comment.