From 1aed32368b09368dcea2497cc96f480f8bbff6a6 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Tue, 26 Nov 2019 09:27:55 -0700 Subject: [PATCH] load join source style meta --- .../maps/public/layers/joins/inner_join.js | 7 ++- .../maps/public/layers/vector_layer.js | 59 +++++++++++++------ 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/x-pack/legacy/plugins/maps/public/layers/joins/inner_join.js b/x-pack/legacy/plugins/maps/public/layers/joins/inner_join.js index 184fdc0663bd7..432492973cce0 100644 --- a/x-pack/legacy/plugins/maps/public/layers/joins/inner_join.js +++ b/x-pack/legacy/plugins/maps/public/layers/joins/inner_join.js @@ -7,6 +7,7 @@ import { ESTermSource } from '../sources/es_term_source'; import { getComputedFieldNamePrefix } from '../styles/vector/style_util'; +import { META_ID_ORIGIN_SUFFIX } from '../../../common/constants'; export class InnerJoin { @@ -36,10 +37,14 @@ export class InnerJoin { // 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. - getSourceId() { + getSourceDataRequestId() { return `join_source_${this._rightSource.getId()}`; } + getSourceMetaDataRequestId() { + return `${this.getSourceDataRequestId()}_${META_ID_ORIGIN_SUFFIX}`; + } + getLeftField() { return this._leftField; } diff --git a/x-pack/legacy/plugins/maps/public/layers/vector_layer.js b/x-pack/legacy/plugins/maps/public/layers/vector_layer.js index d1c6ee92eb1d7..a4646f9c304eb 100644 --- a/x-pack/legacy/plugins/maps/public/layers/vector_layer.js +++ b/x-pack/legacy/plugins/maps/public/layers/vector_layer.js @@ -90,7 +90,7 @@ export class VectorLayer extends AbstractLayer { const joins = this.getValidJoins(); for (let i = 0; i < joins.length; i++) { - const joinDataRequest = this.getDataRequest(joins[i].getSourceId()); + const joinDataRequest = this.getDataRequest(joins[i].getSourceDataRequestId()); if (!joinDataRequest || !joinDataRequest.hasData()) { return false; } @@ -236,7 +236,7 @@ export class VectorLayer extends AbstractLayer { async _syncJoin({ join, startLoading, stopLoading, onLoadError, registerCancelCallback, dataFilters }) { const joinSource = join.getRightJoinSource(); - const sourceDataId = join.getSourceId(); + const sourceDataId = join.getSourceDataRequestId(); const requestToken = Symbol(`layer-join-refresh:${this.getId()} - ${sourceDataId}`); const searchFilters = { ...dataFilters, @@ -289,6 +289,7 @@ export class VectorLayer extends AbstractLayer { async _syncJoins(syncContext) { const joinSyncs = this.getValidJoins().map(async join => { + await this._syncJoinStyleMeta(syncContext, join); return this._syncJoin({ join, ...syncContext }); }); @@ -391,19 +392,43 @@ export class VectorLayer extends AbstractLayer { } } - async _syncSourceStyleMeta({ - startLoading, stopLoading, onLoadError, registerCancelCallback - }) { + async _syncSourceStyleMeta(syncContext) { + const dynamicStyleProps = this._style.getDynamicPropertiesArray() + .filter(dynamicStyleProp => { + return dynamicStyleProp.getFieldOrigin() === FIELD_ORIGIN.SOURCE && dynamicStyleProp.supportsFieldMeta(); + }); - if (!this._source.isElasticsearchSource()) { - return; - } + return this._syncStyleMeta({ + source: this._source, + dataRequestId: SOURCE_META_ID_ORIGIN, + dynamicStyleProps, + ...syncContext + }); + } + async _syncJoinStyleMeta(syncContext, join) { + const joinSource = join.getRightJoinSource(); const dynamicStyleProps = this._style.getDynamicPropertiesArray() .filter(dynamicStyleProp => { - return dynamicStyleProp.getFieldOrigin() === FIELD_ORIGIN.SOURCE && dynamicStyleProp.supportsFieldMeta(); + const matchingField = joinSource.getMetricFieldForName(dynamicStyleProp.getField().getName()); + return dynamicStyleProp.getFieldOrigin() === FIELD_ORIGIN.JOIN + && !!matchingField + && dynamicStyleProp.supportsFieldMeta(); }); - if (dynamicStyleProps.length === 0) { + + return this._syncStyleMeta({ + source: joinSource, + dataRequestId: join.getSourceMetaDataRequestId(), + dynamicStyleProps, + ...syncContext + }); + } + + async _syncStyleMeta({ + source, dataRequestId, dynamicStyleProps, startLoading, stopLoading, onLoadError, registerCancelCallback + }) { + + if (!source.isElasticsearchSource() || dynamicStyleProps.length === 0) { return; } @@ -413,22 +438,22 @@ export class VectorLayer extends AbstractLayer { }), // TODO include time range?, make this user configurable? }; - const prevDataRequest = this._findDataRequestForSource(SOURCE_META_ID_ORIGIN); + const prevDataRequest = this._findDataRequestForSource(dataRequestId); const canSkipFetch = canSkipStyleMetaUpdate({ prevDataRequest, nextMeta }); if (canSkipFetch) { return; } - const requestToken = Symbol(`layer-source-meta:${this.getId()}`); + const requestToken = Symbol(`layer-${this.getId()}-style-meta`); try { - startLoading(SOURCE_META_ID_ORIGIN, requestToken, nextMeta); + startLoading(dataRequestId, requestToken, nextMeta); const layerName = await this.getDisplayName(); - const styleMeta = await this._source.loadStylePropsMeta(layerName, dynamicStyleProps, registerCancelCallback); + const styleMeta = await source.loadStylePropsMeta(layerName, dynamicStyleProps, registerCancelCallback); console.log(styleMeta); - stopLoading(SOURCE_META_ID_ORIGIN, requestToken, styleMeta, nextMeta); + stopLoading(dataRequestId, requestToken, styleMeta, nextMeta); } catch (error) { if (!(error instanceof DataRequestAbortError)) { - onLoadError(SOURCE_META_ID_ORIGIN, requestToken, error.message); + onLoadError(dataRequestId, requestToken, error.message); } } } @@ -438,8 +463,8 @@ export class VectorLayer extends AbstractLayer { return; } - const sourceResult = await this._syncSource(syncContext); await this._syncSourceStyleMeta(syncContext); + const sourceResult = await this._syncSource(syncContext); if ( !sourceResult.featureCollection || !sourceResult.featureCollection.features.length ||