Skip to content

Commit

Permalink
[Maps] Support styles on agg fields with _of_ in name (elastic#54965)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Jan 16, 2020
1 parent 7a9bfaa commit 74e00c1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
33 changes: 6 additions & 27 deletions x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 10 additions & 4 deletions x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

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

Expand Down
16 changes: 16 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/sources/vector_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class MockSource {
getSupportedShapeTypes() {
return this._supportedShapeTypes;
}
getFieldByName(fieldName) {
return new MockField({ fieldName });
}
createField({ fieldName }) {
return new MockField({ fieldName });
}
Expand Down

0 comments on commit 74e00c1

Please sign in to comment.