Skip to content

Commit

Permalink
[Maps] remove getMetricFields from AbstractESSource (#58667) (#58833)
Browse files Browse the repository at this point in the history
* [Maps] remove getMetricFields from AbstractESSource

* do not use formaters for count and unique count

* fix jest test
  • Loading branch information
nreese authored Feb 28, 2020
1 parent 7fd27c3 commit 1bf7e55
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 82 deletions.
21 changes: 15 additions & 6 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 @@ -21,13 +21,17 @@ export class ESAggMetricField extends AbstractField {
}

getName() {
return this._source.formatMetricKey(this.getAggType(), this.getESDocFieldName());
return this._source.getAggKey(this.getAggType(), this.getRootName());
}

getRootName() {
return this._getESDocFieldName();
}

async getLabel() {
return this._label
? await this._label
: this._source.formatMetricLabel(this.getAggType(), this.getESDocFieldName());
? this._label
: this._source.getAggLabel(this.getAggType(), this.getRootName());
}

getAggType() {
Expand All @@ -42,13 +46,13 @@ export class ESAggMetricField extends AbstractField {
return this.getAggType() === AGG_TYPE.TERMS ? 'string' : 'number';
}

getESDocFieldName() {
_getESDocFieldName() {
return this._esDocField ? this._esDocField.getName() : '';
}

getRequestDescription() {
return this.getAggType() !== AGG_TYPE.COUNT
? `${this.getAggType()} ${this.getESDocFieldName()}`
? `${this.getAggType()} ${this.getRootName()}`
: AGG_TYPE.COUNT;
}

Expand All @@ -64,7 +68,7 @@ export class ESAggMetricField extends AbstractField {
}

getValueAggDsl(indexPattern) {
const field = getField(indexPattern, this.getESDocFieldName());
const field = getField(indexPattern, this.getRootName());
const aggType = this.getAggType();
const aggBody = aggType === AGG_TYPE.TERMS ? { size: 1, shard_size: 1 } : {};
return {
Expand All @@ -77,6 +81,11 @@ export class ESAggMetricField extends AbstractField {
return !isMetricCountable(this.getAggType());
}

canValueBeFormatted() {
// Do not use field formatters for counting metrics
return ![AGG_TYPE.COUNT, AGG_TYPE.UNIQUE_COUNT].includes(this.getAggType());
}

async getOrdinalFieldMetaRequest(config) {
return this._esDocField.getOrdinalFieldMetaRequest(config);
}
Expand Down
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 @@ -17,6 +17,14 @@ export class AbstractField {
return this._fieldName;
}

getRootName() {
return this.getName();
}

canValueBeFormatted() {
return true;
}

getSource() {
return this._source;
}
Expand Down
17 changes: 14 additions & 3 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 @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { i18n } from '@kbn/i18n';
import { AbstractESSource } from './es_source';
import { ESAggMetricField } from '../fields/es_agg_field';
import { ESDocField } from '../fields/es_doc_field';
Expand Down Expand Up @@ -72,12 +73,22 @@ export class AbstractESAggSource extends AbstractESSource {
return metrics;
}

formatMetricKey(aggType, fieldName) {
getAggKey(aggType, fieldName) {
return aggType !== AGG_TYPE.COUNT ? `${aggType}${AGG_DELIMITER}${fieldName}` : COUNT_PROP_NAME;
}

formatMetricLabel(aggType, fieldName) {
return aggType !== AGG_TYPE.COUNT ? `${aggType} of ${fieldName}` : COUNT_PROP_LABEL;
getAggLabel(aggType, fieldName) {
switch (aggType) {
case AGG_TYPE.COUNT:
return COUNT_PROP_LABEL;
case AGG_TYPE.TERMS:
return i18n.translate('xpack.maps.source.esAggSource.topTermLabel', {
defaultMessage: `Top {fieldName}`,
values: { fieldName },
});
default:
return `${aggType} ${fieldName}`;
}
}

getValueAggsDsl(indexPattern) {
Expand Down
42 changes: 8 additions & 34 deletions x-pack/legacy/plugins/maps/public/layers/sources/es_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import uuid from 'uuid/v4';
import { copyPersistentState } from '../../reducers/util';
import { ES_GEO_FIELD_TYPE, AGG_TYPE } from '../../../common/constants';
import { ES_GEO_FIELD_TYPE } from '../../../common/constants';
import { DataRequestAbortError } from '../util/data_request';
import { expandToTileBoundaries } from './es_geo_grid_source/geo_tile_utils';

Expand Down Expand Up @@ -72,10 +72,6 @@ export class AbstractESSource extends AbstractVectorSource {
return clonedDescriptor;
}

getMetricFields() {
return [];
}

async _runEsQuery({
requestId,
requestName,
Expand Down Expand Up @@ -254,31 +250,15 @@ export class AbstractESSource extends AbstractVectorSource {
return this._descriptor.id;
}

async getFieldFormatter(fieldName) {
const metricField = this.getMetricFields().find(field => field.getName() === fieldName);

// Do not use field formatters for counting metrics
if (
metricField &&
(metricField.type === AGG_TYPE.COUNT || metricField.type === AGG_TYPE.UNIQUE_COUNT)
) {
return null;
}

// fieldName could be an aggregation so it needs to be unpacked to expose raw field.
const realFieldName = metricField ? metricField.getESDocFieldName() : fieldName;
if (!realFieldName) {
return null;
}

async createFieldFormatter(field) {
let indexPattern;
try {
indexPattern = await this.getIndexPattern();
} catch (error) {
return null;
}

const fieldFromIndexPattern = indexPattern.fields.getByName(realFieldName);
const fieldFromIndexPattern = indexPattern.fields.getByName(field.getRootName());
if (!fieldFromIndexPattern) {
return null;
}
Expand Down Expand Up @@ -336,25 +316,19 @@ export class AbstractESSource extends AbstractVectorSource {
return resp.aggregations;
}

getValueSuggestions = async (fieldName, query) => {
// fieldName could be an aggregation so it needs to be unpacked to expose raw field.
const metricField = this.getMetricFields().find(field => field.getName() === fieldName);
const realFieldName = metricField ? metricField.getESDocFieldName() : fieldName;
if (!realFieldName) {
return [];
}

getValueSuggestions = async (field, query) => {
try {
const indexPattern = await this.getIndexPattern();
const field = indexPattern.fields.getByName(realFieldName);
return await autocompleteService.getValueSuggestions({
indexPattern,
field,
field: indexPattern.fields.getByName(field.getRootName()),
query,
});
} catch (error) {
console.warn(
`Unable to fetch suggestions for field: ${fieldName}, query: ${query}, error: ${error.message}`
`Unable to fetch suggestions for field: ${field.getRootName()}, query: ${query}, error: ${
error.message
}`
);
return [];
}
Expand Down
22 changes: 7 additions & 15 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 @@ -67,29 +67,21 @@ export class ESTermSource extends AbstractESAggSource {
return this._descriptor.whereQuery;
}

formatMetricKey(aggType, fieldName) {
getAggKey(aggType, fieldName) {
const metricKey =
aggType !== AGG_TYPE.COUNT ? `${aggType}${AGG_DELIMITER}${fieldName}` : aggType;
return `${FIELD_NAME_PREFIX}${metricKey}${GROUP_BY_DELIMITER}${
this._descriptor.indexPatternTitle
}.${this._termField.getName()}`;
}

formatMetricLabel(type, fieldName) {
switch (type) {
case AGG_TYPE.COUNT:
return i18n.translate('xpack.maps.source.esJoin.countLabel', {
getAggLabel(aggType, fieldName) {
return aggType === AGG_TYPE.COUNT
? i18n.translate('xpack.maps.source.esJoin.countLabel', {
defaultMessage: `Count of {indexPatternTitle}`,
values: { indexPatternTitle: this._descriptor.indexPatternTitle },
});
case AGG_TYPE.TERMS:
return i18n.translate('xpack.maps.source.esJoin.topTermLabel', {
defaultMessage: `Top {fieldName}`,
values: { fieldName },
});
default:
return `${type} ${fieldName}`;
}
})
: super.getAggLabel(aggType, fieldName);
}

async getPropertiesMap(searchFilters, leftSourceName, leftFieldName, registerCancelCallback) {
Expand All @@ -116,7 +108,7 @@ export class ESTermSource extends AbstractESAggSource {
requestDescription: this._getRequestDescription(leftSourceName, leftFieldName),
});

const countPropertyName = this.formatMetricKey(AGG_TYPE.COUNT);
const countPropertyName = this.getAggKey(AGG_TYPE.COUNT);
return {
propertiesMap: extractPropertiesMap(rawEsData, countPropertyName),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('getMetricFields', () => {
expect(metrics.length).toBe(2);

expect(metrics[0].getAggType()).toEqual('sum');
expect(metrics[0].getESDocFieldName()).toEqual(sumFieldName);
expect(metrics[0].getRootName()).toEqual(sumFieldName);
expect(metrics[0].getName()).toEqual(
'__kbnjoin__sum_of_myFieldGettingSummed_groupby_myIndex.myTermField'
);
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/maps/public/layers/sources/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ export class AbstractSource {
}

// Returns function used to format value
async getFieldFormatter(/* fieldName */) {
async createFieldFormatter(/* field */) {
return null;
}

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

async getValueSuggestions(/* fieldName, query */) {
async getValueSuggestions(/* field, query */) {
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const mockField = {
getName() {
return 'foobar';
},
getRootName() {
return 'foobar';
},
supportsFieldMeta() {
return true;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import React from 'react';
import { OrdinalLegend } from './components/ordinal_legend';
import { CategoricalLegend } from './components/categorical_legend';
import { OrdinalFieldMetaOptionsPopover } from '../components/ordinal_field_meta_options_popover';
import { ESAggMetricField } from '../../../fields/es_agg_field';

export class DynamicStyleProperty extends AbstractStyleProperty {
static type = STYLE_TYPE.DYNAMIC;
Expand All @@ -26,9 +25,9 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
}

getValueSuggestions = query => {
const fieldName = this.getFieldName();
const field = this.getField();
const fieldSource = this.getFieldSource();
return fieldSource && fieldName ? fieldSource.getValueSuggestions(fieldName, query) : [];
return fieldSource && field ? fieldSource.getValueSuggestions(field, query) : [];
};

getFieldMeta() {
Expand Down Expand Up @@ -185,11 +184,7 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
}

_pluckOrdinalStyleMetaFromFieldMetaData(fieldMetaData) {
const realFieldName =
this._field instanceof ESAggMetricField
? this._field.getESDocFieldName()
: this._field.getName();
const stats = fieldMetaData[realFieldName];
const stats = fieldMetaData[this._field.getRootName()];
if (!stats) {
return null;
}
Expand All @@ -209,15 +204,12 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
}

_pluckCategoricalStyleMetaFromFieldMetaData(fieldMetaData) {
const realFieldName =
this._field instanceof ESAggMetricField
? this._field.getESDocFieldName()
: this._field.getName();
if (!fieldMetaData[realFieldName] || !fieldMetaData[realFieldName].buckets) {
const rootFieldName = this._field.getRootName();
if (!fieldMetaData[rootFieldName] || !fieldMetaData[rootFieldName].buckets) {
return null;
}

const ordered = fieldMetaData[realFieldName].buckets.map(bucket => {
const ordered = fieldMetaData[rootFieldName].buckets.map(bucket => {
return {
key: bucket.key,
count: bucket.doc_count,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export class ESAggMetricTooltipProperty extends ESTooltipProperty {
) {
return this._rawValue;
}
const indexPatternField = this._indexPattern.fields.getByName(
this._metricField.getESDocFieldName()
);
const indexPatternField = this._indexPattern.fields.getByName(this._metricField.getRootName());
if (!indexPatternField) {
return this._rawValue;
}
Expand Down
11 changes: 7 additions & 4 deletions x-pack/legacy/plugins/maps/public/layers/vector_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,13 @@ export class VectorLayer extends AbstractLayer {
startLoading(dataRequestId, requestToken, nextMeta);

const formatters = {};
const promises = fields.map(async field => {
const fieldName = field.getName();
formatters[fieldName] = await source.getFieldFormatter(fieldName);
});
const promises = fields
.filter(field => {
return field.canValueBeFormatted();
})
.map(async field => {
formatters[field.getName()] = await source.createFieldFormatter(field);
});
await Promise.all(promises);

stopLoading(dataRequestId, requestToken, formatters, nextMeta);
Expand Down

0 comments on commit 1bf7e55

Please sign in to comment.