[Maps] Support styles on agg fields with _of_ in name (#54965) (#55130)

This commit is contained in:
Thomas Neirynck 2020-01-17 11:00:18 -05:00 committed by GitHub
parent ea7bfbf36e
commit 7bd967a1d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 34 deletions

View file

@ -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) {

View file

@ -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()}`;
}

View file

@ -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(
{

View file

@ -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);

View file

@ -30,6 +30,9 @@ class MockSource {
getSupportedShapeTypes() {
return this._supportedShapeTypes;
}
getFieldByName(fieldName) {
return new MockField({ fieldName });
}
createField({ fieldName }) {
return new MockField({ fieldName });
}