Skip to content

Commit

Permalink
[maps] replace isESSource class method with type guard (elastic#175850)
Browse files Browse the repository at this point in the history
PR replaces `ISource.isESSource` method with type guards for IESSource
interface. This provides stronger type guards and paves the way for
addition ES|QL map layer work (ES|QL layers providing geoFieldNames and
data views).

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
nreese and kibanamachine authored Feb 1, 2024
1 parent e54bf7a commit 68785f3
Show file tree
Hide file tree
Showing 21 changed files with 130 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ export class CustomRasterSource implements IRasterSource {
return [];
}

isESSource(): boolean {
return false;
}

// Returns function used to format value
async createFieldFormatter(field: IField): Promise<FieldFormatter | null> {
return null;
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/maps/public/actions/layer_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
import { ILayer } from '../classes/layers/layer';
import { hasVectorLayerMethod } from '../classes/layers/vector_layer';
import { OnSourceChangeArgs } from '../classes/sources/source';
import { isESVectorTileSource } from '../classes/sources/es_source';
import {
DRAW_MODE,
LAYER_STYLE_TYPE,
Expand Down Expand Up @@ -827,7 +828,7 @@ export function setTileState(
newValue: tileErrors,
});

if (!isLayerGroup(layer) && layer.getSource().isESSource()) {
if (!isLayerGroup(layer) && isESVectorTileSource(layer.getSource())) {
getInspectorAdapters(getState()).vectorTiles.setTileResults(
layerId,
tileMetaFeatures,
Expand Down
11 changes: 3 additions & 8 deletions x-pack/plugins/maps/public/classes/layers/layer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { ISource, SourceEditorArgs } from '../sources/source';
import { DataRequestContext } from '../../actions';
import { IStyle } from '../styles/style';
import { LICENSED_FEATURES } from '../../licensed_features';
import { IESSource } from '../sources/es_source';
import { hasESSourceMethod, isESVectorTileSource } from '../sources/es_source';
import { TileErrorsList } from './tile_errors_list';
import { isLayerGroup } from './layer_group';

Expand All @@ -72,7 +72,6 @@ export interface ILayer {
getSource(): ISource;
getSourceForEditing(): ISource;
syncData(syncContext: DataRequestContext): void;
supportsElasticsearchFilters(): boolean;
supportsFitToBounds(): Promise<boolean>;
getAttributions(): Promise<Attribution[]>;
getLabel(): string;
Expand Down Expand Up @@ -215,10 +214,6 @@ export class AbstractLayer implements ILayer {
return !!this._descriptor.__isPreviewLayer;
}

supportsElasticsearchFilters(): boolean {
return this.getSource().isESSource();
}

async supportsFitToBounds(): Promise<boolean> {
return await this.getSource().supportsFitToBounds();
}
Expand Down Expand Up @@ -442,7 +437,7 @@ export class AbstractLayer implements ILayer {
body: (
<TileErrorsList
inspectorAdapters={inspectorAdapters}
isESSource={!isLayerGroup(this) && this.getSource().isESSource()}
isESVectorTileSource={!isLayerGroup(this) && isESVectorTileSource(this.getSource())}
layerId={this.getId()}
tileErrors={this._descriptor.__tileErrors}
/>
Expand Down Expand Up @@ -575,7 +570,7 @@ export class AbstractLayer implements ILayer {

getGeoFieldNames(): string[] {
const source = this.getSource();
return source.isESSource() ? [(source as IESSource).getGeoFieldName()] : [];
return hasESSourceMethod(source, 'getGeoFieldName') ? [source.getGeoFieldName()] : [];
}

async getStyleMetaDescriptorFromLocalFeatures(): Promise<StyleMetaDescriptor | null> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,6 @@ export class LayerGroup implements ILayer {
return !!this._descriptor.__isPreviewLayer;
}

supportsElasticsearchFilters(): boolean {
return this.getChildren().some((child) => {
return child.supportsElasticsearchFilters();
});
}

async supportsFitToBounds(): Promise<boolean> {
return this._asyncSomeChildren('supportsFitToBounds');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { RESPONSE_VIEW_ID } from '../../inspector/vector_tile_adapter/components

interface Props {
inspectorAdapters: Adapters;
isESSource: boolean;
isESVectorTileSource: boolean;
layerId: string;
tileErrors: TileError[];
}
Expand Down Expand Up @@ -59,7 +59,7 @@ export function TileErrorsList(props: Props) {
];

function renderError(tileError: TileError) {
if (!props.isESSource || !tileError.error) {
if (!props.isESVectorTileSource || !tileError.error) {
return tileError.message;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jest.mock('uuid', () => ({
import sinon from 'sinon';
import { MockSyncContext } from '../../__fixtures__/mock_sync_context';
import { IMvtVectorSource } from '../../../sources/vector_source';
import { IESSource } from '../../../sources/es_source';
import { DataRequest } from '../../../util/data_request';
import { syncMvtSourceData } from './mvt_source_data';

Expand Down Expand Up @@ -40,8 +41,8 @@ const mockSource = {
isGeoGridPrecisionAware: () => {
return false;
},
isESSource: () => {
return false;
isMvt: () => {
return true;
},
} as unknown as IMvtVectorSource;

Expand Down Expand Up @@ -471,10 +472,10 @@ describe('syncMvtSourceData', () => {
},
source: {
...mockSource,
isESSource: () => {
return true;
getIndexPatternId: () => {
return '1234';
},
},
} as unknown as IMvtVectorSource & IESSource,
syncContext,
});
sinon.assert.calledOnce(syncContext.inspectorAdapters.vectorTiles.addLayer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DataRequest } from '../../../util/data_request';
import { DataRequestContext } from '../../../../actions';
import { canSkipSourceUpdate } from '../../../util/can_skip_fetch';
import { IMvtVectorSource } from '../../../sources/vector_source';
import { isESVectorTileSource } from '../../../sources/es_source';

// shape of sourceDataRequest.getData()
export interface MvtSourceData {
Expand Down Expand Up @@ -83,7 +84,7 @@ export async function syncMvtSourceData({
: prevData.refreshToken;

const tileUrl = await source.getTileUrl(requestMeta, refreshToken, hasLabels, buffer);
if (source.isESSource()) {
if (isESVectorTileSource(source)) {
syncContext.inspectorAdapters.vectorTiles.addLayer(layerId, layerName, tileUrl);
}
const sourceData = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
VectorLayerDescriptor,
} from '../../../../../common/descriptor_types';
import { ESSearchSource } from '../../../sources/es_search_source';
import { IESSource } from '../../../sources/es_source';
import { hasESSourceMethod, isESVectorTileSource } from '../../../sources/es_source';
import { InnerJoin } from '../../../joins/inner_join';
import { LayerIcon } from '../../layer';
import { MvtSourceData, syncMvtSourceData } from './mvt_source_data';
Expand Down Expand Up @@ -91,10 +91,11 @@ export class MvtVectorLayer extends AbstractVectorLayer {
async getBounds(getDataRequestContext: (layerId: string) => DataRequestContext) {
// Add filter to narrow bounds to features with matching join keys
let joinKeyFilter;
if (this.getSource().isESSource()) {
const source = this.getSource();
if (hasESSourceMethod(source, 'getIndexPattern')) {
const { join, joinPropertiesMap } = this._getJoinResults();
if (join && joinPropertiesMap) {
const indexPattern = await (this.getSource() as IESSource).getIndexPattern();
const indexPattern = await source.getIndexPattern();
const joinField = getField(indexPattern, join.getLeftField().getName());
joinKeyFilter = buildPhrasesFilter(
joinField,
Expand All @@ -120,7 +121,7 @@ export class MvtVectorLayer extends AbstractVectorLayer {
}

getFeatureId(feature: Feature): string | number | undefined {
if (!this.getSource().isESSource()) {
if (!isESVectorTileSource(this.getSource())) {
return feature.id;
}

Expand All @@ -130,7 +131,7 @@ export class MvtVectorLayer extends AbstractVectorLayer {
}

getLayerIcon(isTocIcon: boolean): LayerIcon {
if (!this.getSource().isESSource()) {
if (!isESVectorTileSource(this.getSource())) {
// Only ES-sources can have a special meta-tile, not 3rd party vector tile sources
return {
icon: this.getCurrentStyle().getIcon(false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ import {
VectorStyleRequestMeta,
} from '../../../../common/descriptor_types';
import { IVectorSource } from '../../sources/vector_source';
import { isESVectorTileSource } from '../../sources/es_source';
import { LayerIcon, ILayer, LayerMessage } from '../layer';
import { InnerJoin } from '../../joins/inner_join';
import { isSpatialJoin } from '../../joins/is_spatial_join';
import { IField } from '../../fields/field';
import { DataRequestContext } from '../../../actions';
import { ITooltipProperty } from '../../tooltips/tooltip_property';
import { IDynamicStyleProperty } from '../../styles/vector/properties/dynamic_style_property';
import { IESSource } from '../../sources/es_source';
import { hasESSourceMethod } from '../../sources/es_source';
import type { IJoinSource, ITermJoinSource } from '../../sources/join_sources';
import { isTermJoinSource } from '../../sources/join_sources';
import type { IESAggSource } from '../../sources/es_agg_source';
Expand Down Expand Up @@ -462,7 +463,7 @@ export class AbstractVectorLayer extends AbstractLayer implements IVectorLayer {
sourceQuery?: Query;
style: IVectorStyle;
} & DataRequestContext) {
if (!source.isESSource() || dynamicStyleProps.length === 0) {
if (!hasESSourceMethod(source, 'loadStylePropsMeta') || dynamicStyleProps.length === 0) {
return;
}

Expand All @@ -488,7 +489,7 @@ export class AbstractVectorLayer extends AbstractLayer implements IVectorLayer {
startLoading(dataRequestId, requestToken, nextMeta);
const layerName = await this.getDisplayName(source);

const { styleMeta, warnings } = await (source as IESSource).loadStylePropsMeta({
const { styleMeta, warnings } = await source.loadStylePropsMeta({
layerName,
style,
dynamicStyleProps,
Expand Down Expand Up @@ -873,7 +874,7 @@ export class AbstractVectorLayer extends AbstractLayer implements IVectorLayer {
const isSourceGeoJson = !this.getSource().isMvt();
const filterExpr = getPointFilterExpression(
isSourceGeoJson,
this.getSource().isESSource(),
isESVectorTileSource(this.getSource()),
this._getJoinFilterExpression(),
timesliceMaskConfig
);
Expand Down Expand Up @@ -980,7 +981,7 @@ export class AbstractVectorLayer extends AbstractLayer implements IVectorLayer {
const isSourceGeoJson = !this.getSource().isMvt();
const filterExpr = getLabelFilterExpression(
isSourceGeoJson,
this.getSource().isESSource(),
isESVectorTileSource(this.getSource()),
this._getJoinFilterExpression(),
timesliceMaskConfig
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ import { createExtentFilter } from '../../../../common/elasticsearch_util';
import { copyPersistentState } from '../../../reducers/copy_persistent_state';
import { DataRequestAbortError } from '../../util/data_request';
import { expandToTileBoundaries } from '../../util/geo_tile_utils';
import { IVectorSource } from '../vector_source';
import {
AbstractESSourceDescriptor,
AbstractSourceDescriptor,
DynamicStylePropertyOptions,
MapExtent,
StyleMetaData,
VectorSourceRequestMeta,
} from '../../../../common/descriptor_types';
import { IVectorStyle } from '../../styles/vector/vector_style';
Expand All @@ -45,45 +43,12 @@ import { IField } from '../../fields/field';
import { FieldFormatter } from '../../../../common/constants';
import { isValidStringConfig } from '../../util/valid_string_config';
import { mergeExecutionContext } from '../execution_context_utils';
import type { IESSource } from './types';

export function isSearchSourceAbortError(error: Error) {
return error.name === 'AbortError';
}

export interface IESSource extends IVectorSource {
isESSource(): true;

getId(): string;

getIndexPattern(): Promise<DataView>;

getIndexPatternId(): string;

getGeoFieldName(): string;

loadStylePropsMeta({
layerName,
style,
dynamicStyleProps,
registerCancelCallback,
sourceQuery,
timeFilters,
searchSessionId,
inspectorAdapters,
executionContext,
}: {
layerName: string;
style: IVectorStyle;
dynamicStyleProps: Array<IDynamicStyleProperty<DynamicStylePropertyOptions>>;
registerCancelCallback: (callback: () => void) => void;
sourceQuery?: Query;
timeFilters: TimeRange;
searchSessionId?: string;
inspectorAdapters: Adapters;
executionContext: KibanaExecutionContext;
}): Promise<{ styleMeta: StyleMetaData; warnings: SearchResponseWarning[] }>;
}

export class AbstractESSource extends AbstractVectorSource implements IESSource {
indexPattern?: DataView;

Expand Down Expand Up @@ -151,10 +116,6 @@ export class AbstractESSource extends AbstractVectorSource implements IESSource
return [];
}

isESSource(): true {
return true;
}

cloneDescriptor(): AbstractSourceDescriptor {
const clonedDescriptor = copyPersistentState(this._descriptor);
// id used as uuid to track requests in inspector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
* 2.0.
*/

export * from './es_source';
export { AbstractESSource } from './es_source';
export type { IESSource } from './types';
export { isESSource, isESVectorTileSource, hasESSourceMethod } from './types';
Loading

0 comments on commit 68785f3

Please sign in to comment.