Skip to content

Commit

Permalink
[Maps] Fix term-join creation (#83974)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck authored Nov 24, 2020
1 parent e559a61 commit ba7a872
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 131 deletions.
44 changes: 0 additions & 44 deletions x-pack/plugins/maps/public/classes/joins/inner_join.d.ts

This file was deleted.

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

import { Query } from 'src/plugins/data/public';
import { Feature, GeoJsonProperties } from 'geojson';
import { ESTermSource } from '../sources/es_term_source';
import { getComputedFieldNamePrefix } from '../styles/vector/style_util';
import {
META_DATA_REQUEST_ID_SUFFIX,
FORMATTERS_DATA_REQUEST_ID_SUFFIX,
} from '../../../common/constants';
import { JoinDescriptor } from '../../../common/descriptor_types';
import { IVectorSource } from '../sources/vector_source';
import { IField } from '../fields/field';
import { PropertiesMap } from '../../../common/elasticsearch_util';

export class InnerJoin {
constructor(joinDescriptor, leftSource) {
private readonly _descriptor: JoinDescriptor;
private readonly _rightSource?: ESTermSource;
private readonly _leftField?: IField;

constructor(joinDescriptor: JoinDescriptor, leftSource: IVectorSource) {
this._descriptor = joinDescriptor;
const inspectorAdapters = leftSource.getInspectorAdapters();
this._rightSource = new ESTermSource(joinDescriptor.right, inspectorAdapters);
this._leftField = this._descriptor.leftField
if (
joinDescriptor.right &&
'indexPatternId' in joinDescriptor.right &&
'term' in joinDescriptor.right
) {
this._rightSource = new ESTermSource(joinDescriptor.right, inspectorAdapters);
}
this._leftField = joinDescriptor.leftField
? leftSource.createField({ fieldName: joinDescriptor.leftField })
: null;
: undefined;
}

destroy() {
this._rightSource.destroy();
if (this._rightSource) {
this._rightSource.destroy();
}
}

hasCompleteConfig() {
if (this._leftField && this._rightSource) {
return this._rightSource.hasCompleteConfig();
}

return false;
return this._leftField && this._rightSource ? this._rightSource.hasCompleteConfig() : false;
}

getJoinFields() {
return this._rightSource.getMetricFields();
return this._rightSource ? this._rightSource.getMetricFields() : [];
}

// Source request id must be static and unique because the re-fetch logic uses the id to locate the previous request.
// Elasticsearch sources have a static and unique id so that requests can be modified in the inspector.
// Using the right source id as the source request id because it meets the above criteria.
getSourceDataRequestId() {
return `join_source_${this._rightSource.getId()}`;
return `join_source_${this._rightSource!.getId()}`;
}

getSourceMetaDataRequestId() {
Expand All @@ -52,11 +66,17 @@ export class InnerJoin {
return `${this.getSourceDataRequestId()}_${FORMATTERS_DATA_REQUEST_ID_SUFFIX}`;
}

getLeftField() {
getLeftField(): IField {
if (!this._leftField) {
throw new Error('Cannot get leftField from InnerJoin with incomplete config');
}
return this._leftField;
}

joinPropertiesToFeature(feature, propertiesMap) {
joinPropertiesToFeature(feature: Feature, propertiesMap: PropertiesMap): boolean {
if (!feature.properties || !this._leftField || !this._rightSource) {
return false;
}
const rightMetricFields = this._rightSource.getMetricFields();
// delete feature properties added by previous join
for (let j = 0; j < rightMetricFields.length; j++) {
Expand All @@ -70,43 +90,46 @@ export class InnerJoin {
featurePropertyKey.length >= stylePropertyPrefix.length &&
featurePropertyKey.substring(0, stylePropertyPrefix.length) === stylePropertyPrefix
) {
delete feature.properties[featurePropertyKey];
delete feature.properties![featurePropertyKey];
}
});
}

const joinKey = feature.properties[this._leftField.getName()];
const coercedKey =
typeof joinKey === 'undefined' || joinKey === null ? null : joinKey.toString();
if (propertiesMap && coercedKey !== null && propertiesMap.has(coercedKey)) {
if (coercedKey !== null && propertiesMap.has(coercedKey)) {
Object.assign(feature.properties, propertiesMap.get(coercedKey));
return true;
} else {
return false;
}
}

getRightJoinSource() {
getRightJoinSource(): ESTermSource {
if (!this._rightSource) {
throw new Error('Cannot get rightSource from InnerJoin with incomplete config');
}
return this._rightSource;
}

toDescriptor() {
toDescriptor(): JoinDescriptor {
return this._descriptor;
}

async getTooltipProperties(properties) {
return await this._rightSource.getTooltipProperties(properties);
async getTooltipProperties(properties: GeoJsonProperties) {
return await this.getRightJoinSource().getTooltipProperties(properties);
}

getIndexPatternIds() {
return this._rightSource.getIndexPatternIds();
return this.getRightJoinSource().getIndexPatternIds();
}

getQueryableIndexPatternIds() {
return this._rightSource.getQueryableIndexPatternIds();
return this.getRightJoinSource().getQueryableIndexPatternIds();
}

getWhereQuery() {
return this._rightSource.getWhereQuery();
getWhereQuery(): Query | undefined {
return this.getRightJoinSource().getWhereQuery();
}
}
40 changes: 0 additions & 40 deletions x-pack/plugins/maps/public/classes/joins/join.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
} from '../../../../common/descriptor_types';
import { IVectorSource } from '../../sources/vector_source';
import { CustomIconAndTooltipContent, ILayer } from '../layer';
import { IJoin } from '../../joins/join';
import { InnerJoin } from '../../joins/inner_join';
import { IField } from '../../fields/field';
import { DataRequestContext } from '../../../actions';
import { ITooltipProperty } from '../../tooltips/tooltip_property';
Expand All @@ -68,21 +68,21 @@ interface SourceResult {

interface JoinState {
dataHasChanged: boolean;
join: IJoin;
join: InnerJoin;
propertiesMap?: PropertiesMap;
}

export interface VectorLayerArguments {
source: IVectorSource;
joins?: IJoin[];
joins?: InnerJoin[];
layerDescriptor: VectorLayerDescriptor;
}

export interface IVectorLayer extends ILayer {
getFields(): Promise<IField[]>;
getStyleEditorFields(): Promise<IField[]>;
getJoins(): IJoin[];
getValidJoins(): IJoin[];
getJoins(): InnerJoin[];
getValidJoins(): InnerJoin[];
getSource(): IVectorSource;
getFeatureById(id: string | number): Feature | null;
getPropertiesForTooltip(properties: GeoJsonProperties): Promise<ITooltipProperty[]>;
Expand All @@ -93,7 +93,7 @@ export class VectorLayer extends AbstractLayer {
static type = LAYER_TYPE.VECTOR;

protected readonly _style: IVectorStyle;
private readonly _joins: IJoin[];
private readonly _joins: InnerJoin[];

static createDescriptor(
options: Partial<VectorLayerDescriptor>,
Expand Down Expand Up @@ -339,7 +339,7 @@ export class VectorLayer extends AbstractLayer {
onLoadError,
registerCancelCallback,
dataFilters,
}: { join: IJoin } & DataRequestContext): Promise<JoinState> {
}: { join: InnerJoin } & DataRequestContext): Promise<JoinState> {
const joinSource = join.getRightJoinSource();
const sourceDataId = join.getSourceDataRequestId();
const requestToken = Symbol(`layer-join-refresh:${this.getId()} - ${sourceDataId}`);
Expand Down Expand Up @@ -453,10 +453,9 @@ export class VectorLayer extends AbstractLayer {
for (let j = 0; j < joinStates.length; j++) {
const joinState = joinStates[j];
const innerJoin = joinState.join;
const canJoinOnCurrent = innerJoin.joinPropertiesToFeature(
feature,
joinState.propertiesMap
);
const canJoinOnCurrent = joinState.propertiesMap
? innerJoin.joinPropertiesToFeature(feature, joinState.propertiesMap)
: false;
isFeatureVisible = isFeatureVisible && canJoinOnCurrent;
}

Expand Down Expand Up @@ -559,7 +558,7 @@ export class VectorLayer extends AbstractLayer {
});
}

async _syncJoinStyleMeta(syncContext: DataRequestContext, join: IJoin, style: IVectorStyle) {
async _syncJoinStyleMeta(syncContext: DataRequestContext, join: InnerJoin, style: IVectorStyle) {
const joinSource = join.getRightJoinSource();
return this._syncStyleMeta({
source: joinSource,
Expand Down Expand Up @@ -663,7 +662,7 @@ export class VectorLayer extends AbstractLayer {
});
}

async _syncJoinFormatters(syncContext: DataRequestContext, join: IJoin, style: IVectorStyle) {
async _syncJoinFormatters(syncContext: DataRequestContext, join: InnerJoin, style: IVectorStyle) {
const joinSource = join.getRightJoinSource();
return this._syncFormatters({
source: joinSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export class ESTermSource extends AbstractESAggSource {
}
return {
...normalizedDescriptor,
indexPatternTitle: descriptor.indexPatternTitle
? descriptor.indexPatternTitle
: descriptor.indexPatternId,
term: descriptor.term!,
type: SOURCE_TYPES.ES_TERM_SOURCE,
};
Expand All @@ -64,7 +67,7 @@ export class ESTermSource extends AbstractESAggSource {
private readonly _termField: ESDocField;
readonly _descriptor: ESTermSourceDescriptor;

constructor(descriptor: ESTermSourceDescriptor, inspectorAdapters: Adapters) {
constructor(descriptor: ESTermSourceDescriptor, inspectorAdapters?: Adapters) {
const sourceDescriptor = ESTermSource.createDescriptor(descriptor);
super(sourceDescriptor, inspectorAdapters);
this._descriptor = sourceDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '../../../../../common/descriptor_types';
import { IField } from '../../../fields/field';
import { IVectorLayer } from '../../../layers/vector_layer/vector_layer';
import { IJoin } from '../../../joins/join';
import { InnerJoin } from '../../../joins/inner_join';
import { IVectorStyle } from '../vector_style';
import { getComputedFieldName } from '../style_util';

Expand Down Expand Up @@ -88,7 +88,7 @@ export class DynamicStyleProperty<T>
return SOURCE_META_DATA_REQUEST_ID;
}

const join = this._layer.getValidJoins().find((validJoin: IJoin) => {
const join = this._layer.getValidJoins().find((validJoin: InnerJoin) => {
return validJoin.getRightJoinSource().hasMatchingMetricField(fieldName);
});
return join ? join.getSourceMetaDataRequestId() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
*/

import { ITooltipProperty } from './tooltip_property';
import { IJoin } from '../joins/join';
import { InnerJoin } from '../joins/inner_join';
import { Filter } from '../../../../../../src/plugins/data/public';

export class JoinTooltipProperty implements ITooltipProperty {
private readonly _tooltipProperty: ITooltipProperty;
private readonly _leftInnerJoins: IJoin[];
private readonly _leftInnerJoins: InnerJoin[];

constructor(tooltipProperty: ITooltipProperty, leftInnerJoins: IJoin[]) {
constructor(tooltipProperty: ITooltipProperty, leftInnerJoins: InnerJoin[]) {
this._tooltipProperty = tooltipProperty;
this._leftInnerJoins = leftInnerJoins;
}
Expand Down
Loading

0 comments on commit ba7a872

Please sign in to comment.