Skip to content

Commit

Permalink
load join source style meta
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Nov 26, 2019
1 parent 44b6bdc commit 1aed323
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
7 changes: 6 additions & 1 deletion x-pack/legacy/plugins/maps/public/layers/joins/inner_join.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
}
Expand Down
59 changes: 42 additions & 17 deletions x-pack/legacy/plugins/maps/public/layers/vector_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 });
});

Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
}
}
Expand All @@ -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 ||
Expand Down

0 comments on commit 1aed323

Please sign in to comment.