Skip to content

Commit

Permalink
[Maps] use style metadata to calculate symbolization bands
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Nov 26, 2019
1 parent b5133b5 commit 9fc4d03
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 9 deletions.
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/maps/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export const FIELD_ORIGIN = {
};

export const SOURCE_DATA_ID_ORIGIN = 'source';
export const META_ID_ORIGIN_SUFFIX = 'meta';
export const SOURCE_META_ID_ORIGIN = `${SOURCE_DATA_ID_ORIGIN}_${META_ID_ORIGIN_SUFFIX}`;

export const GEOJSON_FILE = 'GEOJSON_FILE';

Expand Down
12 changes: 10 additions & 2 deletions x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


import { AbstractField } from './field';
import { COUNT_AGG_TYPE } from '../../../common/constants';
import { COUNT_AGG_TYPE, METRIC_TYPE } from '../../../common/constants';
import { ESAggMetricTooltipProperty } from '../tooltips/es_aggmetric_tooltip_property';

export class ESAggMetricField extends AbstractField {
Expand Down Expand Up @@ -55,7 +55,6 @@ export class ESAggMetricField extends AbstractField {
);
}


makeMetricAggConfig() {
const metricAggConfig = {
id: this.getName(),
Expand All @@ -69,4 +68,13 @@ export class ESAggMetricField extends AbstractField {
}
return metricAggConfig;
}

supportsFieldMeta() {
// count and sum aggregations are not within field bounds so they do not support field meta.
return ![METRIC_TYPE.COUNT, METRIC_TYPE.SUM, METRIC_TYPE.UNIQUE_COUNT].includes(this.getAggType());
}

async getFieldMetaRequest(config) {
return this._esDocField.getFieldMetaRequest(config);
}
}
27 changes: 27 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/fields/es_doc_field.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,31 @@ export class ESDocField extends AbstractField {
return field.type;
}

supportsFieldMeta() {
return true;
}

async getFieldMetaRequest({ sigma }) {
const field = await this._getField();

if (field.type === 'number') {
const extendedStats = { sigma };
if (field.scripted) {
extendedStats.script = {
source: field.script,
lang: field.lang
};
} else {
extendedStats.field = this._fieldName;
}
return {
[`${this._fieldName}_range_stats`]: {
extended_stats: extendedStats
}
};
}

return null;
}

}
8 changes: 8 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/fields/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ export class AbstractField {
getOrigin() {
return this._origin;
}

supportsFieldMeta() {
return false;
}

async getFieldMetaRequest(/* config */) {
return null;
}
}
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/maps/public/layers/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class AbstractLayer {
}

supportsElasticsearchFilters() {
return this._source.supportsElasticsearchFilters();
return this._source.isElasticsearchSource();
}

async supportsFitToBounds() {
Expand Down
45 changes: 42 additions & 3 deletions x-pack/legacy/plugins/maps/public/layers/sources/es_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class AbstractESSource extends AbstractVectorSource {
return [];
}

supportsElasticsearchFilters() {
isElasticsearchSource() {
return true;
}

Expand All @@ -73,7 +73,7 @@ export class AbstractESSource extends AbstractVectorSource {
return [];
}

async _runEsQuery(requestName, searchSource, registerCancelCallback, requestDescription) {
async _runEsQuery(requestName, searchSource, registerCancelCallback, requestDescription, requestId) {
const abortController = new AbortController();
registerCancelCallback(() => abortController.abort());

Expand All @@ -82,7 +82,7 @@ export class AbstractESSource extends AbstractVectorSource {
inspectorAdapters: this._inspectorAdapters,
searchSource,
requestName,
requestId: this.getId(),
requestId: requestId ? requestId : this.getId(),
requestDesc: requestDescription,
abortSignal: abortController.signal,
});
Expand Down Expand Up @@ -271,4 +271,43 @@ export class AbstractESSource extends AbstractVectorSource {
return fieldFromIndexPattern.format.getConverterFor('text');
}

async loadStylePropsMeta(layerName, dynamicStyleProps, registerCancelCallback) {
const promises = dynamicStyleProps
.map(dynamicStyleProp => {
return dynamicStyleProp.getFieldMetaRequest();
});

const fieldAggRequests = await Promise.all(promises);
const aggs = fieldAggRequests.reduce((aggs, fieldAggRequest) => {
if (!fieldAggRequest) {
return aggs;
}

return {
...aggs,
...fieldAggRequest,
};
}, {});

const indexPattern = await this.getIndexPattern();
const searchSource = new SearchSource();
searchSource.setField('index', indexPattern);
searchSource.setField('size', 0);
searchSource.setField('aggs', aggs);

const resp = await this._runEsQuery(
i18n.translate('xpack.maps.source.esSource.stylePropsMetaRequestName', {
defaultMessage: '{layerName} - metadata',
values: { layerName }
}),
searchSource,
registerCancelCallback,
i18n.translate('xpack.maps.source.esSource.stylePropsMetaRequestDescription', {
defaultMessage: 'Elasticsearch request retrieving field metadata used for calculating symbolization bands.',
}),
`${this.getId()}_styleMeta`);

return resp;
}

}
6 changes: 5 additions & 1 deletion x-pack/legacy/plugins/maps/public/layers/sources/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class AbstractSource {
return AbstractSource.isIndexingSource;
}

supportsElasticsearchFilters() {
isElasticsearchSource() {
return false;
}

Expand All @@ -136,6 +136,10 @@ export class AbstractSource {
async getFieldFormatter(/* fieldName */) {
return null;
}

async loadStylePropsMeta() {
throw new Error(`Source#loadStylePropsMeta not implemented`);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
return this._field.getOrigin();
}

supportsFieldMeta() {
return this._field.supportsFieldMeta();
}

async getFieldMetaRequest() {
return this._field.getFieldMetaRequest({ sigma: 3 });
}

supportsFeatureState() {
return true;
}
Expand Down
43 changes: 41 additions & 2 deletions x-pack/legacy/plugins/maps/public/layers/vector_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import {
GEO_JSON_TYPE,
FEATURE_ID_PROPERTY_NAME,
SOURCE_DATA_ID_ORIGIN,
SOURCE_META_ID_ORIGIN,
FEATURE_VISIBLE_PROPERTY_NAME,
EMPTY_FEATURE_COLLECTION,
LAYER_TYPE
LAYER_TYPE,
FIELD_ORIGIN,
} from '../../common/constants';
import _ from 'lodash';
import { JoinTooltipProperty } from './tooltips/join_tooltip_property';
Expand Down Expand Up @@ -464,7 +466,7 @@ export class VectorLayer extends AbstractLayer {
startLoading, stopLoading, onLoadError, registerCancelCallback, dataFilters
}) {

const requestToken = Symbol(`layer-source-refresh:${this.getId()} - source`);
const requestToken = Symbol(`layer-source-data:${this.getId()}`);
const searchFilters = this._getSearchFilters(dataFilters);
const canSkip = await this._canSkipSourceUpdate(this._source, SOURCE_DATA_ID_ORIGIN, searchFilters);
if (canSkip) {
Expand Down Expand Up @@ -498,6 +500,42 @@ export class VectorLayer extends AbstractLayer {
}
}

async _syncSourceStyleMeta({
startLoading, stopLoading, onLoadError, registerCancelCallback
}) {

if (!this._source.isElasticsearchSource()) {
return;
}

const sourceMetaDataRequest = this._findDataRequestForSource(SOURCE_META_ID_ORIGIN);
if (sourceMetaDataRequest) {
return;
}

const dynamicStyleProps = this._style.getDynamicPropertiesArray()
.filter(dynamicStyleProp => {
return dynamicStyleProp.getFieldOrigin() === FIELD_ORIGIN.SOURCE && dynamicStyleProp.supportsFieldMeta();
});
if (dynamicStyleProps.length === 0) {
return;
}

const requestToken = Symbol(`layer-source-meta:${this.getId()}`);
try {
const requestMeta = {}; // TODO include time range?
startLoading(SOURCE_META_ID_ORIGIN, requestToken, requestMeta);
const layerName = await this.getDisplayName();
const styleMeta = await this._source.loadStylePropsMeta(layerName, dynamicStyleProps, registerCancelCallback);
console.log(styleMeta);
stopLoading(SOURCE_META_ID_ORIGIN, requestToken, styleMeta, requestMeta);
} catch (error) {
if (!(error instanceof DataRequestAbortError)) {
onLoadError(SOURCE_META_ID_ORIGIN, requestToken, error.message);
}
}
}

_assignIdsToFeatures(featureCollection) {

//wrt https://github.com/elastic/kibana/issues/39317
Expand Down Expand Up @@ -529,6 +567,7 @@ export class VectorLayer extends AbstractLayer {
}

const sourceResult = await this._syncSource(syncContext);
await this._syncSourceStyleMeta(syncContext);
if (!sourceResult.featureCollection || !sourceResult.featureCollection.features.length) {
return;
}
Expand Down

0 comments on commit 9fc4d03

Please sign in to comment.