diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.js index fd3ae8f0ab7e3..967a3c41aec26 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.js @@ -15,7 +15,7 @@ import { FIELD_ORIGIN, } from '../../../common/constants'; -const AGG_DELIMITER = '_of_'; +export const AGG_DELIMITER = '_of_'; export class AbstractESAggSource extends AbstractESSource { static METRIC_SCHEMA_CONFIG = { @@ -53,33 +53,12 @@ export class AbstractESAggSource extends AbstractESSource { : []; } - createField({ fieldName, label }) { - //if there is a corresponding field with a custom label, use that one. - if (!label) { - const matchField = this._metricFields.find(field => field.getName() === fieldName); - if (matchField) { - label = matchField.getLabel(); - } - } + getFieldByName(name) { + return this.getMetricFieldForName(name); + } - if (fieldName === COUNT_PROP_NAME) { - return new ESAggMetricField({ - aggType: COUNT_AGG_TYPE, - label: label, - source: this, - origin: this.getOriginForField(), - }); - } - //this only works because aggType is a fixed set and does not include the `_of_` string - const [aggType, docField] = fieldName.split(AGG_DELIMITER); - const esDocField = new ESDocField({ fieldName: docField, source: this }); - return new ESAggMetricField({ - label: label, - esDocField, - aggType, - source: this, - origin: this.getOriginForField(), - }); + createField() { + throw new Error('Cannot create a new field from just a fieldname for an es_agg_source.'); } hasMatchingMetricField(fieldName) { diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.js index 6e8b2e1e7c6f5..443668984b0b4 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.js @@ -9,9 +9,14 @@ import _ from 'lodash'; import { Schemas } from 'ui/vis/editors/default/schemas'; import { AggConfigs } from 'ui/agg_types'; import { i18n } from '@kbn/i18n'; -import { DEFAULT_MAX_BUCKETS_LIMIT, FIELD_ORIGIN, METRIC_TYPE } from '../../../common/constants'; +import { + COUNT_PROP_LABEL, + DEFAULT_MAX_BUCKETS_LIMIT, + FIELD_ORIGIN, + METRIC_TYPE, +} from '../../../common/constants'; import { ESDocField } from '../fields/es_doc_field'; -import { AbstractESAggSource } from './es_agg_source'; +import { AbstractESAggSource, AGG_DELIMITER } from './es_agg_source'; const TERMS_AGG_NAME = 'join'; @@ -85,14 +90,15 @@ export class ESTermSource extends AbstractESAggSource { } formatMetricKey(aggType, fieldName) { - const metricKey = aggType !== METRIC_TYPE.COUNT ? `${aggType}_of_${fieldName}` : aggType; + const metricKey = + aggType !== METRIC_TYPE.COUNT ? `${aggType}${AGG_DELIMITER}${fieldName}` : aggType; return `${FIELD_NAME_PREFIX}${metricKey}${GROUP_BY_DELIMITER}${ this._descriptor.indexPatternTitle }.${this._termField.getName()}`; } formatMetricLabel(type, fieldName) { - const metricLabel = type !== METRIC_TYPE.COUNT ? `${type} ${fieldName}` : 'count'; + const metricLabel = type !== METRIC_TYPE.COUNT ? `${type} ${fieldName}` : COUNT_PROP_LABEL; return `${metricLabel} of ${this._descriptor.indexPatternTitle}:${this._termField.getName()}`; } diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/vector_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/vector_source.js index b9d8ae86c5850..3952aacf03b33 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/vector_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/vector_source.js @@ -50,10 +50,26 @@ export class AbstractVectorSource extends AbstractSource { ); } + /** + * factory function creating a new field-instance + * @param fieldName + * @param label + * @returns {ESAggMetricField} + */ createField() { throw new Error(`Should implemement ${this.constructor.type} ${this}`); } + /** + * Retrieves a field. This may be an existing instance. + * @param fieldName + * @param label + * @returns {ESAggMetricField} + */ + getFieldByName(name) { + return this.createField({ fieldName: name }); + } + _createDefaultLayerDescriptor(options, mapColors) { return VectorLayer.createDescriptor( { diff --git a/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.js b/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.js index 30d1c5726ba48..ec0afb6a6a37e 100644 --- a/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.js +++ b/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.js @@ -579,9 +579,7 @@ export class VectorStyle extends AbstractStyle { //fieldDescriptor.label is ignored. This is essentially cruft duplicating label-info from the metric-selection //Ignore this custom label if (fieldDescriptor.origin === FIELD_ORIGIN.SOURCE) { - return this._source.createField({ - fieldName: fieldDescriptor.name, - }); + return this._source.getFieldByName(fieldDescriptor.name); } else if (fieldDescriptor.origin === FIELD_ORIGIN.JOIN) { const join = this._layer.getValidJoins().find(join => { return join.getRightJoinSource().hasMatchingMetricField(fieldDescriptor.name); diff --git a/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.test.js b/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.test.js index c250d83720580..b8f5c74139a63 100644 --- a/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.test.js +++ b/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.test.js @@ -30,6 +30,9 @@ class MockSource { getSupportedShapeTypes() { return this._supportedShapeTypes; } + getFieldByName(fieldName) { + return new MockField({ fieldName }); + } createField({ fieldName }) { return new MockField({ fieldName }); }