Skip to content

Commit

Permalink
[maps] convert ESPewPewSource to typescript (elastic#132656)
Browse files Browse the repository at this point in the history
* [maps] convert ESPewPewSource to typescript

* move @ts-expect-error moved by fix
  • Loading branch information
nreese committed May 20, 2022
1 parent eb6a061 commit 642290b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import {
} from '../../../../../../common/constants';
import { GeoJsonVectorLayer } from '../../../vector_layer';
import { VectorStyle } from '../../../../styles/vector/vector_style';
// @ts-ignore
import { ESSearchSource } from '../../../../sources/es_search_source';
// @ts-ignore
import { ESPewPewSource } from '../../../../sources/es_pew_pew_source';
import { getDefaultDynamicProperties } from '../../../../styles/vector/vector_style_defaults';
import { APM_INDEX_PATTERN_TITLE } from '../observability';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,35 @@
import React from 'react';
import turfBbox from '@turf/bbox';
import { multiPoint } from '@turf/helpers';
import { Adapters } from '@kbn/inspector-plugin/common/adapters';
import { type Filter, buildExistsFilter } from '@kbn/es-query';
import { lastValueFrom } from 'rxjs';
import type {
AggregationsGeoBoundsAggregate,
LatLonGeoLocation,
TopLeftBottomRightGeoBounds,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';

import { UpdateSourceEditor } from './update_source_editor';
import { i18n } from '@kbn/i18n';
// @ts-expect-error
import { UpdateSourceEditor } from './update_source_editor';
import { SOURCE_TYPES, VECTOR_SHAPE_TYPE } from '../../../../common/constants';
import { getDataSourceLabel, getDataViewLabel } from '../../../../common/i18n_getters';
// @ts-expect-error
import { convertToLines } from './convert_to_lines';
import { AbstractESAggSource } from '../es_agg_source';
import { registerSource } from '../source_registry';
import { turfBboxToBounds } from '../../../../common/elasticsearch_util';
import { DataRequestAbortError } from '../../util/data_request';
import { makePublicExecutionContext } from '../../../util';
import { SourceEditorArgs } from '../source';
import {
ESPewPewSourceDescriptor,
MapExtent,
VectorSourceRequestMeta,
} from '../../../../common/descriptor_types';
import { isValidStringConfig } from '../../util/valid_string_config';
import { BoundsRequestMeta, GeoJsonWithMeta } from '../vector_source';

const MAX_GEOTILE_LEVEL = 29;

Expand All @@ -27,20 +45,30 @@ export const sourceTitle = i18n.translate('xpack.maps.source.pewPewTitle', {
});

export class ESPewPewSource extends AbstractESAggSource {
static type = SOURCE_TYPES.ES_PEW_PEW;
readonly _descriptor: ESPewPewSourceDescriptor;

static createDescriptor(descriptor) {
static createDescriptor(descriptor: Partial<ESPewPewSourceDescriptor>): ESPewPewSourceDescriptor {
const normalizedDescriptor = AbstractESAggSource.createDescriptor(descriptor);
if (!isValidStringConfig(descriptor.sourceGeoField)) {
throw new Error('Cannot create ESPewPewSourceDescriptor, sourceGeoField is not provided');
}
if (!isValidStringConfig(descriptor.destGeoField)) {
throw new Error('Cannot create ESPewPewSourceDescriptor, destGeoField is not provided');
}
return {
...normalizedDescriptor,
type: ESPewPewSource.type,
indexPatternId: descriptor.indexPatternId,
sourceGeoField: descriptor.sourceGeoField,
destGeoField: descriptor.destGeoField,
type: SOURCE_TYPES.ES_PEW_PEW,
sourceGeoField: descriptor.sourceGeoField!,
destGeoField: descriptor.destGeoField!,
};
}

renderSourceSettingsEditor({ onChange }) {
constructor(descriptor: ESPewPewSourceDescriptor) {
super(descriptor);
this._descriptor = descriptor;
}

renderSourceSettingsEditor({ onChange }: SourceEditorArgs) {
return (
<UpdateSourceEditor
indexPatternId={this.getIndexPatternId()}
Expand Down Expand Up @@ -100,18 +128,18 @@ export class ESPewPewSource extends AbstractESAggSource {
];
}

getGeoGridPrecision(zoom) {
getGeoGridPrecision(zoom: number) {
const targetGeotileLevel = Math.ceil(zoom) + 2;
return Math.min(targetGeotileLevel, MAX_GEOTILE_LEVEL);
}

async getGeoJsonWithMeta(
layerName,
searchFilters,
registerCancelCallback,
isRequestStillActive,
inspectorAdapters
) {
layerName: string,
searchFilters: VectorSourceRequestMeta,
registerCancelCallback: (callback: () => void) => void,
isRequestStillActive: () => boolean,
inspectorAdapters: Adapters
): Promise<GeoJsonWithMeta> {
const indexPattern = await this.getIndexPattern();
const searchSource = await this.makeSearchSource(searchFilters, 0);
searchSource.setField('trackTotalHits', false);
Expand Down Expand Up @@ -151,14 +179,10 @@ export class ESPewPewSource extends AbstractESAggSource {
// Some underlying indices may not contain geo fields
// Filter out documents without geo fields to avoid shard failures for those indices
searchSource.setField('filter', [
...searchSource.getField('filter'),
...(searchSource.getField('filter') as Filter[]),
// destGeoField exists ensured by buffer filter
// so only need additional check for sourceGeoField
{
exists: {
field: this._descriptor.sourceGeoField,
},
},
buildExistsFilter({ name: this._descriptor.sourceGeoField, type: 'geo_point' }, indexPattern),
]);

const esResponse = await this._runEsQuery({
Expand Down Expand Up @@ -188,7 +212,10 @@ export class ESPewPewSource extends AbstractESAggSource {
return this._descriptor.destGeoField;
}

async getBoundsForFilters(boundsFilters, registerCancelCallback) {
async getBoundsForFilters(
boundsFilters: BoundsRequestMeta,
registerCancelCallback: (callback: () => void) => void
): Promise<MapExtent | null> {
const searchSource = await this.makeSearchSource(boundsFilters, 0);
searchSource.setField('trackTotalHits', false);
searchSource.setField('aggs', {
Expand All @@ -208,31 +235,36 @@ export class ESPewPewSource extends AbstractESAggSource {
try {
const abortController = new AbortController();
registerCancelCallback(() => abortController.abort());
const { rawResponse: esResp } = await searchSource
.fetch$({
const { rawResponse: esResp } = await lastValueFrom(
searchSource.fetch$({
abortSignal: abortController.signal,
legacyHitsTotal: false,
executionContext: makePublicExecutionContext('es_pew_pew_source:bounds'),
})
.toPromise();
if (esResp.aggregations.destFitToBounds.bounds) {
);
const destBounds = (esResp.aggregations?.destFitToBounds as AggregationsGeoBoundsAggregate)
.bounds as TopLeftBottomRightGeoBounds;
if (destBounds) {
corners.push([
esResp.aggregations.destFitToBounds.bounds.top_left.lon,
esResp.aggregations.destFitToBounds.bounds.top_left.lat,
(destBounds.top_left as LatLonGeoLocation).lon,
(destBounds.top_left as LatLonGeoLocation).lat,
]);
corners.push([
esResp.aggregations.destFitToBounds.bounds.bottom_right.lon,
esResp.aggregations.destFitToBounds.bounds.bottom_right.lat,
(destBounds.bottom_right as LatLonGeoLocation).lon,
(destBounds.bottom_right as LatLonGeoLocation).lat,
]);
}
if (esResp.aggregations.sourceFitToBounds.bounds) {
const sourceBounds = (
esResp.aggregations?.sourceFitToBounds as AggregationsGeoBoundsAggregate
).bounds as TopLeftBottomRightGeoBounds;
if (sourceBounds) {
corners.push([
esResp.aggregations.sourceFitToBounds.bounds.top_left.lon,
esResp.aggregations.sourceFitToBounds.bounds.top_left.lat,
(sourceBounds.top_left as LatLonGeoLocation).lon,
(sourceBounds.top_left as LatLonGeoLocation).lat,
]);
corners.push([
esResp.aggregations.sourceFitToBounds.bounds.bottom_right.lon,
esResp.aggregations.sourceFitToBounds.bounds.bottom_right.lat,
(sourceBounds.bottom_right as LatLonGeoLocation).lon,
(sourceBounds.bottom_right as LatLonGeoLocation).lat,
]);
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import React from 'react';
import { i18n } from '@kbn/i18n';
import { getDefaultDynamicProperties } from '../../styles/vector/vector_style_defaults';
import { GeoJsonVectorLayer } from '../../layers/vector_layer';
// @ts-ignore
import { ESPewPewSource, sourceTitle } from './es_pew_pew_source';
import { VectorStyle } from '../../styles/vector/vector_style';
import {
Expand All @@ -24,7 +23,11 @@ import { NUMERICAL_COLOR_PALETTES } from '../../styles/color_palettes';
// @ts-ignore
import { CreateSourceEditor } from './create_source_editor';
import { LayerWizard, RenderWizardArguments } from '../../layers';
import { ColorDynamicOptions, SizeDynamicOptions } from '../../../../common/descriptor_types';
import {
ColorDynamicOptions,
ESPewPewSourceDescriptor,
SizeDynamicOptions,
} from '../../../../common/descriptor_types';
import { Point2PointLayerIcon } from '../../layers/wizards/icons/point_2_point_layer_icon';

export const point2PointLayerWizardConfig: LayerWizard = {
Expand All @@ -36,7 +39,7 @@ export const point2PointLayerWizardConfig: LayerWizard = {
}),
icon: Point2PointLayerIcon,
renderWizard: ({ previewLayers }: RenderWizardArguments) => {
const onSourceConfigChange = (sourceConfig: unknown) => {
const onSourceConfigChange = (sourceConfig: Partial<ESPewPewSourceDescriptor>) => {
if (!sourceConfig) {
previewLayers([]);
return;
Expand Down

0 comments on commit 642290b

Please sign in to comment.