From 578137fb204fa70fe1a8251abd1dea442949a538 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Thu, 5 Mar 2020 19:58:22 -0700 Subject: [PATCH] [Maps] top term percentage field property (#59386) * [Maps] top term percentage property * populate percentage in feature properties * TS work * clean up TS * fix all type errors * unit test for esAggFieldsFactory * clean up * i18n cleanup * do not show decimal place for perentage * fix jest expects * fix eslint errors * tslint errors * handle empty top bucket aggregation Co-authored-by: Elastic Machine --- .../legacy/plugins/maps/common/constants.ts | 10 +- .../plugins/maps/common/descriptor_types.d.ts | 4 +- .../maps/public/layers/fields/es_agg_field.js | 96 ---------- .../public/layers/fields/es_agg_field.test.js | 28 --- .../public/layers/fields/es_agg_field.test.ts | 80 +++++++++ .../maps/public/layers/fields/es_agg_field.ts | 169 ++++++++++++++++++ .../maps/public/layers/fields/field.ts | 11 +- .../fields/top_term_percentage_field.ts | 70 ++++++++ .../public/layers/sources/es_agg_source.d.ts | 19 ++ .../public/layers/sources/es_agg_source.js | 50 ++---- .../convert_to_geojson.test.ts | 4 + .../es_geo_grid_source.d.ts | 12 ++ .../es_geo_grid_source/es_geo_grid_source.js | 6 +- .../convert_to_lines.test.ts | 1 + .../maps/public/layers/sources/es_source.d.ts | 10 +- .../public/layers/sources/es_term_source.js | 32 +--- .../public/layers/sources/vector_source.js | 4 +- .../properties/dynamic_style_property.js | 5 +- .../public/layers/util/es_agg_utils.test.ts | 19 +- .../maps/public/layers/util/es_agg_utils.ts | 13 ++ ...ic_countable.js => is_metric_countable.ts} | 2 +- .../translations/translations/ja-JP.json | 3 - .../translations/translations/zh-CN.json | 3 - 23 files changed, 439 insertions(+), 212 deletions(-) delete mode 100644 x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.js delete mode 100644 x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.test.js create mode 100644 x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.test.ts create mode 100644 x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.ts create mode 100644 x-pack/legacy/plugins/maps/public/layers/fields/top_term_percentage_field.ts create mode 100644 x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.d.ts create mode 100644 x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.d.ts rename x-pack/legacy/plugins/maps/public/layers/util/{is_metric_countable.js => is_metric_countable.ts} (85%) diff --git a/x-pack/legacy/plugins/maps/common/constants.ts b/x-pack/legacy/plugins/maps/common/constants.ts index 4f1b3223967a5..53289fbbc9005 100644 --- a/x-pack/legacy/plugins/maps/common/constants.ts +++ b/x-pack/legacy/plugins/maps/common/constants.ts @@ -55,10 +55,10 @@ export const ES_SEARCH = 'ES_SEARCH'; export const ES_PEW_PEW = 'ES_PEW_PEW'; export const EMS_XYZ = 'EMS_XYZ'; // identifies a custom TMS source. Name is a little unfortunate. -export const FIELD_ORIGIN = { - SOURCE: 'source', - JOIN: 'join', -}; +export enum FIELD_ORIGIN { + SOURCE = 'source', + JOIN = 'join', +} export const SOURCE_DATA_ID_ORIGIN = 'source'; export const META_ID_ORIGIN_SUFFIX = 'meta'; @@ -139,6 +139,8 @@ export enum GRID_RESOLUTION { MOST_FINE = 'MOST_FINE', } +export const TOP_TERM_PERCENTAGE_SUFFIX = '__percentage'; + export const COUNT_PROP_LABEL = i18n.translate('xpack.maps.aggs.defaultCountLabel', { defaultMessage: 'count', }); diff --git a/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts b/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts index f342260c3e7a4..f03f828200bbd 100644 --- a/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts +++ b/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts @@ -40,8 +40,8 @@ export type AbstractESAggDescriptor = AbstractESSourceDescriptor & { }; export type ESGeoGridSourceDescriptor = AbstractESAggDescriptor & { - requestType: RENDER_AS; - resolution: GRID_RESOLUTION; + requestType?: RENDER_AS; + resolution?: GRID_RESOLUTION; }; export type ESSearchSourceDescriptor = AbstractESSourceDescriptor & { diff --git a/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.js b/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.js deleted file mode 100644 index 27ab8fc5bfb3a..0000000000000 --- a/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.js +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { AbstractField } from './field'; -import { AGG_TYPE } from '../../../common/constants'; -import { isMetricCountable } from '../util/is_metric_countable'; -import { ESAggMetricTooltipProperty } from '../tooltips/es_aggmetric_tooltip_property'; -import { getField, addFieldToDSL } from '../util/es_agg_utils'; - -export class ESAggMetricField extends AbstractField { - static type = 'ES_AGG'; - - constructor({ label, source, aggType, esDocField, origin }) { - super({ source, origin }); - this._label = label; - this._aggType = aggType; - this._esDocField = esDocField; - } - - getName() { - return this._source.getAggKey(this.getAggType(), this.getRootName()); - } - - getRootName() { - return this._getESDocFieldName(); - } - - async getLabel() { - return this._label - ? this._label - : this._source.getAggLabel(this.getAggType(), this.getRootName()); - } - - getAggType() { - return this._aggType; - } - - isValid() { - return this.getAggType() === AGG_TYPE.COUNT ? true : !!this._esDocField; - } - - async getDataType() { - return this.getAggType() === AGG_TYPE.TERMS ? 'string' : 'number'; - } - - _getESDocFieldName() { - return this._esDocField ? this._esDocField.getName() : ''; - } - - getRequestDescription() { - return this.getAggType() !== AGG_TYPE.COUNT - ? `${this.getAggType()} ${this.getRootName()}` - : AGG_TYPE.COUNT; - } - - async createTooltipProperty(value) { - const indexPattern = await this._source.getIndexPattern(); - return new ESAggMetricTooltipProperty( - this.getName(), - await this.getLabel(), - value, - indexPattern, - this - ); - } - - getValueAggDsl(indexPattern) { - const field = getField(indexPattern, this.getRootName()); - const aggType = this.getAggType(); - const aggBody = aggType === AGG_TYPE.TERMS ? { size: 1, shard_size: 1 } : {}; - return { - [aggType]: addFieldToDSL(aggBody, field), - }; - } - - supportsFieldMeta() { - // count and sum aggregations are not within field bounds so they do not support field meta. - 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); - } - - async getCategoricalFieldMetaRequest() { - return this._esDocField.getCategoricalFieldMetaRequest(); - } -} diff --git a/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.test.js b/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.test.js deleted file mode 100644 index aeeffd63607ee..0000000000000 --- a/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.test.js +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { ESAggMetricField } from './es_agg_field'; -import { AGG_TYPE } from '../../../common/constants'; - -describe('supportsFieldMeta', () => { - test('Non-counting aggregations should support field meta', () => { - const avgMetric = new ESAggMetricField({ aggType: AGG_TYPE.AVG }); - expect(avgMetric.supportsFieldMeta()).toBe(true); - const maxMetric = new ESAggMetricField({ aggType: AGG_TYPE.MAX }); - expect(maxMetric.supportsFieldMeta()).toBe(true); - const minMetric = new ESAggMetricField({ aggType: AGG_TYPE.MIN }); - expect(minMetric.supportsFieldMeta()).toBe(true); - }); - - test('Counting aggregations should not support field meta', () => { - const countMetric = new ESAggMetricField({ aggType: AGG_TYPE.COUNT }); - expect(countMetric.supportsFieldMeta()).toBe(false); - const sumMetric = new ESAggMetricField({ aggType: AGG_TYPE.SUM }); - expect(sumMetric.supportsFieldMeta()).toBe(false); - const uniqueCountMetric = new ESAggMetricField({ aggType: AGG_TYPE.UNIQUE_COUNT }); - expect(uniqueCountMetric.supportsFieldMeta()).toBe(false); - }); -}); diff --git a/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.test.ts b/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.test.ts new file mode 100644 index 0000000000000..7a65b5f9f6b46 --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.test.ts @@ -0,0 +1,80 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { ESAggField, esAggFieldsFactory } from './es_agg_field'; +import { AGG_TYPE, FIELD_ORIGIN } from '../../../common/constants'; +import { IESAggSource } from '../sources/es_agg_source'; +import { IIndexPattern } from 'src/plugins/data/public'; + +const mockIndexPattern = { + title: 'wildIndex', + fields: [ + { + name: 'foo*', + }, + ], +} as IIndexPattern; + +const mockEsAggSource = { + getAggKey: (aggType: AGG_TYPE, fieldName: string) => { + return 'agg_key'; + }, + getAggLabel: (aggType: AGG_TYPE, fieldName: string) => { + return 'agg_label'; + }, + getIndexPattern: async () => { + return mockIndexPattern; + }, +} as IESAggSource; + +const defaultParams = { + label: 'my agg field', + source: mockEsAggSource, + aggType: AGG_TYPE.COUNT, + origin: FIELD_ORIGIN.SOURCE, +}; + +describe('supportsFieldMeta', () => { + test('Non-counting aggregations should support field meta', () => { + const avgMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.AVG }); + expect(avgMetric.supportsFieldMeta()).toBe(true); + const maxMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.MAX }); + expect(maxMetric.supportsFieldMeta()).toBe(true); + const minMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.MIN }); + expect(minMetric.supportsFieldMeta()).toBe(true); + const termsMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.TERMS }); + expect(termsMetric.supportsFieldMeta()).toBe(true); + }); + + test('Counting aggregations should not support field meta', () => { + const countMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.COUNT }); + expect(countMetric.supportsFieldMeta()).toBe(false); + const sumMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.SUM }); + expect(sumMetric.supportsFieldMeta()).toBe(false); + const uniqueCountMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.UNIQUE_COUNT }); + expect(uniqueCountMetric.supportsFieldMeta()).toBe(false); + }); +}); + +describe('esAggFieldsFactory', () => { + test('Should only create top terms field when term field is not provided', () => { + const fields = esAggFieldsFactory( + { type: AGG_TYPE.TERMS }, + mockEsAggSource, + FIELD_ORIGIN.SOURCE + ); + expect(fields.length).toBe(1); + }); + + test('Should create top terms and top terms percentage fields', () => { + const fields = esAggFieldsFactory( + { type: AGG_TYPE.TERMS, field: 'myField' }, + mockEsAggSource, + FIELD_ORIGIN.SOURCE + ); + expect(fields.length).toBe(2); + }); +}); diff --git a/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.ts b/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.ts new file mode 100644 index 0000000000000..9f08200442fea --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.ts @@ -0,0 +1,169 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { IndexPattern } from 'src/plugins/data/public'; +import { IField } from './field'; +import { AggDescriptor } from '../../../common/descriptor_types'; +import { IESAggSource } from '../sources/es_agg_source'; +import { IVectorSource } from '../sources/vector_source'; +// @ts-ignore +import { ESDocField } from './es_doc_field'; +import { AGG_TYPE, FIELD_ORIGIN } from '../../../common/constants'; +import { isMetricCountable } from '../util/is_metric_countable'; +// @ts-ignore +import { ESAggMetricTooltipProperty } from '../tooltips/es_aggmetric_tooltip_property'; +import { getField, addFieldToDSL } from '../util/es_agg_utils'; +import { TopTermPercentageField } from './top_term_percentage_field'; + +export interface IESAggField extends IField { + getValueAggDsl(indexPattern: IndexPattern): unknown | null; + getBucketCount(): number; +} + +export class ESAggField implements IESAggField { + static type = 'ES_AGG'; + + private _source: IESAggSource; + private _origin: FIELD_ORIGIN; + private _label?: string; + private _aggType: AGG_TYPE; + private _esDocField?: unknown; + + constructor({ + label, + source, + aggType, + esDocField, + origin, + }: { + label?: string; + source: IESAggSource; + aggType: AGG_TYPE; + esDocField?: unknown; + origin: FIELD_ORIGIN; + }) { + this._source = source; + this._origin = origin; + this._label = label; + this._aggType = aggType; + this._esDocField = esDocField; + } + + getSource(): IVectorSource { + return this._source; + } + + getOrigin(): FIELD_ORIGIN { + return this._origin; + } + + getName(): string { + return this._source.getAggKey(this.getAggType(), this.getRootName()); + } + + getRootName(): string { + return this._getESDocFieldName(); + } + + async getLabel(): Promise { + return this._label + ? this._label + : this._source.getAggLabel(this.getAggType(), this.getRootName()); + } + + getAggType(): AGG_TYPE { + return this._aggType; + } + + isValid(): boolean { + return this.getAggType() === AGG_TYPE.COUNT ? true : !!this._esDocField; + } + + async getDataType(): Promise { + return this.getAggType() === AGG_TYPE.TERMS ? 'string' : 'number'; + } + + _getESDocFieldName(): string { + // TODO remove when esDocField is typed + // @ts-ignore + return this._esDocField ? this._esDocField.getName() : ''; + } + + async createTooltipProperty(value: number | string): Promise { + const indexPattern = await this._source.getIndexPattern(); + return new ESAggMetricTooltipProperty( + this.getName(), + await this.getLabel(), + value, + indexPattern, + this + ); + } + + getValueAggDsl(indexPattern: IndexPattern): unknown | null { + if (this.getAggType() === AGG_TYPE.COUNT) { + return null; + } + + const field = getField(indexPattern, this.getRootName()); + const aggType = this.getAggType(); + const aggBody = aggType === AGG_TYPE.TERMS ? { size: 1, shard_size: 1 } : {}; + return { + [aggType]: addFieldToDSL(aggBody, field), + }; + } + + getBucketCount(): number { + // terms aggregation increases the overall number of buckets per split bucket + return this.getAggType() === AGG_TYPE.TERMS ? 1 : 0; + } + + supportsFieldMeta(): boolean { + // count and sum aggregations are not within field bounds so they do not support field meta. + return !isMetricCountable(this.getAggType()); + } + + canValueBeFormatted(): boolean { + // Do not use field formatters for counting metrics + return ![AGG_TYPE.COUNT, AGG_TYPE.UNIQUE_COUNT].includes(this.getAggType()); + } + + async getOrdinalFieldMetaRequest(): Promise { + // TODO remove when esDocField is typed + // @ts-ignore + return this._esDocField.getOrdinalFieldMetaRequest(); + } + + async getCategoricalFieldMetaRequest(): Promise { + // TODO remove when esDocField is typed + // @ts-ignore + return this._esDocField.getCategoricalFieldMetaRequest(); + } +} + +export function esAggFieldsFactory( + aggDescriptor: AggDescriptor, + source: IESAggSource, + origin: FIELD_ORIGIN +): IESAggField[] { + const aggField = new ESAggField({ + label: aggDescriptor.label, + esDocField: aggDescriptor.field + ? new ESDocField({ fieldName: aggDescriptor.field, source }) + : null, + aggType: aggDescriptor.type, + source, + origin, + }); + + const aggFields: IESAggField[] = [aggField]; + + if (aggDescriptor.field && aggDescriptor.type === AGG_TYPE.TERMS) { + aggFields.push(new TopTermPercentageField(aggField)); + } + + return aggFields; +} diff --git a/x-pack/legacy/plugins/maps/public/layers/fields/field.ts b/x-pack/legacy/plugins/maps/public/layers/fields/field.ts index 57a916e93ffe0..f7c27fec1c6c7 100644 --- a/x-pack/legacy/plugins/maps/public/layers/fields/field.ts +++ b/x-pack/legacy/plugins/maps/public/layers/fields/field.ts @@ -13,12 +13,15 @@ export interface IField { canValueBeFormatted(): boolean; getLabel(): Promise; getDataType(): Promise; + getSource(): IVectorSource; + getOrigin(): FIELD_ORIGIN; + isValid(): boolean; } export class AbstractField implements IField { private _fieldName: string; private _source: IVectorSource; - private _origin: string; + private _origin: FIELD_ORIGIN; constructor({ fieldName, @@ -27,7 +30,7 @@ export class AbstractField implements IField { }: { fieldName: string; source: IVectorSource; - origin: string; + origin: FIELD_ORIGIN; }) { this._fieldName = fieldName; this._source = source; @@ -66,7 +69,7 @@ export class AbstractField implements IField { throw new Error('must implement Field#createTooltipProperty'); } - getOrigin(): string { + getOrigin(): FIELD_ORIGIN { return this._origin; } @@ -74,7 +77,7 @@ export class AbstractField implements IField { return false; } - async getOrdinalFieldMetaRequest(/* config */): Promise { + async getOrdinalFieldMetaRequest(): Promise { return null; } diff --git a/x-pack/legacy/plugins/maps/public/layers/fields/top_term_percentage_field.ts b/x-pack/legacy/plugins/maps/public/layers/fields/top_term_percentage_field.ts new file mode 100644 index 0000000000000..cadf325652370 --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/fields/top_term_percentage_field.ts @@ -0,0 +1,70 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { IESAggField } from './es_agg_field'; +import { IVectorSource } from '../sources/vector_source'; +// @ts-ignore +import { TooltipProperty } from '../tooltips/tooltip_property'; +import { TOP_TERM_PERCENTAGE_SUFFIX } from '../../../common/constants'; +import { FIELD_ORIGIN } from '../../../common/constants'; + +export class TopTermPercentageField implements IESAggField { + private _topTermAggField: IESAggField; + + constructor(topTermAggField: IESAggField) { + this._topTermAggField = topTermAggField; + } + + getSource(): IVectorSource { + return this._topTermAggField.getSource(); + } + + getOrigin(): FIELD_ORIGIN { + return this._topTermAggField.getOrigin(); + } + + getName(): string { + return `${this._topTermAggField.getName()}${TOP_TERM_PERCENTAGE_SUFFIX}`; + } + + getRootName(): string { + // top term percentage is a derived value so it has no root field + return ''; + } + + async getLabel(): Promise { + const baseLabel = await this._topTermAggField.getLabel(); + return `${baseLabel}%`; + } + + isValid(): boolean { + return this._topTermAggField.isValid(); + } + + async getDataType(): Promise { + return 'number'; + } + + async createTooltipProperty(value: unknown): Promise { + return new TooltipProperty(this.getName(), await this.getLabel(), value); + } + + getValueAggDsl(): null { + return null; + } + + getBucketCount(): number { + return 0; + } + + supportsFieldMeta(): boolean { + return false; + } + + canValueBeFormatted(): boolean { + return false; + } +} diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.d.ts b/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.d.ts new file mode 100644 index 0000000000000..a91bb4a8bb1a7 --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.d.ts @@ -0,0 +1,19 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { IESSource } from './es_source'; +import { AbstractESSource } from './es_source'; +import { AGG_TYPE } from '../../../common/constants'; + +export interface IESAggSource extends IESSource { + getAggKey(aggType: AGG_TYPE, fieldName: string): string; + getAggLabel(aggType: AGG_TYPE, fieldName: string): string; +} + +export class AbstractESAggSource extends AbstractESSource implements IESAggSource { + getAggKey(aggType: AGG_TYPE, fieldName: string): string; + getAggLabel(aggType: AGG_TYPE, fieldName: string): string; +} 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 775535d9e2299..62f3369ceb3a3 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 @@ -6,8 +6,8 @@ import { i18n } from '@kbn/i18n'; import { AbstractESSource } from './es_source'; -import { ESAggMetricField } from '../fields/es_agg_field'; -import { ESDocField } from '../fields/es_doc_field'; +import { esAggFieldsFactory } from '../fields/es_agg_field'; + import { AGG_TYPE, COUNT_PROP_LABEL, @@ -20,20 +20,14 @@ export const AGG_DELIMITER = '_of_'; export class AbstractESAggSource extends AbstractESSource { constructor(descriptor, inspectorAdapters) { super(descriptor, inspectorAdapters); - this._metricFields = this._descriptor.metrics - ? this._descriptor.metrics.map(metric => { - const esDocField = metric.field - ? new ESDocField({ fieldName: metric.field, source: this }) - : null; - return new ESAggMetricField({ - label: metric.label, - esDocField: esDocField, - aggType: metric.type, - source: this, - origin: this.getOriginForField(), - }); - }) - : []; + this._metricFields = []; + if (this._descriptor.metrics) { + this._descriptor.metrics.forEach(aggDescriptor => { + this._metricFields.push( + ...esAggFieldsFactory(aggDescriptor, this, this.getOriginForField()) + ); + }); + } } getFieldByName(name) { @@ -61,16 +55,9 @@ export class AbstractESAggSource extends AbstractESSource { getMetricFields() { const metrics = this._metricFields.filter(esAggField => esAggField.isValid()); - if (metrics.length === 0) { - metrics.push( - new ESAggMetricField({ - aggType: AGG_TYPE.COUNT, - source: this, - origin: this.getOriginForField(), - }) - ); - } - return metrics; + return metrics.length === 0 + ? esAggFieldsFactory({ type: AGG_TYPE.COUNT }, this, this.getOriginForField()) + : metrics; } getAggKey(aggType, fieldName) { @@ -93,13 +80,12 @@ export class AbstractESAggSource extends AbstractESSource { getValueAggsDsl(indexPattern) { const valueAggsDsl = {}; - this.getMetricFields() - .filter(esAggMetric => { - return esAggMetric.getAggType() !== AGG_TYPE.COUNT; - }) - .forEach(esAggMetric => { + this.getMetricFields().forEach(esAggMetric => { + const aggDsl = esAggMetric.getValueAggDsl(indexPattern); + if (aggDsl) { valueAggsDsl[esAggMetric.getName()] = esAggMetric.getValueAggDsl(indexPattern); - }); + } + }); return valueAggsDsl; } diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/convert_to_geojson.test.ts b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/convert_to_geojson.test.ts index a8223c36df349..e79d8e09fce9b 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/convert_to_geojson.test.ts +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/convert_to_geojson.test.ts @@ -53,6 +53,7 @@ describe('convertCompositeRespToGeoJson', () => { avg_of_bytes: 5359.2307692307695, doc_count: 65, 'terms_of_machine.os.keyword': 'win xp', + 'terms_of_machine.os.keyword__percentage': 25, }, type: 'Feature', }); @@ -79,6 +80,7 @@ describe('convertCompositeRespToGeoJson', () => { avg_of_bytes: 5359.2307692307695, doc_count: 65, 'terms_of_machine.os.keyword': 'win xp', + 'terms_of_machine.os.keyword__percentage': 25, }, type: 'Feature', }); @@ -125,6 +127,7 @@ describe('convertRegularRespToGeoJson', () => { avg_of_bytes: 5359.2307692307695, doc_count: 65, 'terms_of_machine.os.keyword': 'win xp', + 'terms_of_machine.os.keyword__percentage': 25, }, type: 'Feature', }); @@ -151,6 +154,7 @@ describe('convertRegularRespToGeoJson', () => { avg_of_bytes: 5359.2307692307695, doc_count: 65, 'terms_of_machine.os.keyword': 'win xp', + 'terms_of_machine.os.keyword__percentage': 25, }, type: 'Feature', }); diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.d.ts b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.d.ts new file mode 100644 index 0000000000000..652409b61fd72 --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.d.ts @@ -0,0 +1,12 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { AbstractESAggSource } from '../es_agg_source'; +import { ESGeoGridSourceDescriptor } from '../../../../common/descriptor_types'; + +export class ESGeoGridSource extends AbstractESAggSource { + constructor(sourceDescriptor: ESGeoGridSourceDescriptor, inspectorAdapters: unknown); +} diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.js index b2463275dad0a..4987d052b8ab7 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.js @@ -20,7 +20,6 @@ import { COLOR_GRADIENTS } from '../../styles/color_utils'; import { CreateSourceEditor } from './create_source_editor'; import { UpdateSourceEditor } from './update_source_editor'; import { - AGG_TYPE, DEFAULT_MAX_BUCKETS_LIMIT, SOURCE_DATA_ID_ORIGIN, ES_GEO_GRID, @@ -297,10 +296,7 @@ export class ESGeoGridSource extends AbstractESAggSource { let bucketsPerGrid = 1; this.getMetricFields().forEach(metricField => { - if (metricField.getAggType() === AGG_TYPE.TERMS) { - // each terms aggregation increases the overall number of buckets per grid - bucketsPerGrid++; - } + bucketsPerGrid += metricField.getBucketCount(); }); const features = diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_pew_pew_source/convert_to_lines.test.ts b/x-pack/legacy/plugins/maps/public/layers/sources/es_pew_pew_source/convert_to_lines.test.ts index 5fbd5a3ad20c0..14c62aa0207fe 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_pew_pew_source/convert_to_lines.test.ts +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_pew_pew_source/convert_to_lines.test.ts @@ -62,6 +62,7 @@ it('Should convert elasticsearch aggregation response into feature collection of avg_of_FlightDelayMin: 3, doc_count: 1, terms_of_Carrier: 'ES-Air', + terms_of_Carrier__percentage: 100, }, type: 'Feature', }); diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_source.d.ts b/x-pack/legacy/plugins/maps/public/layers/sources/es_source.d.ts index 2aaaad15d6321..25c4fae89f024 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_source.d.ts +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_source.d.ts @@ -5,5 +5,13 @@ */ import { AbstractVectorSource } from './vector_source'; +import { IVectorSource } from './vector_source'; +import { IndexPattern } from '../../../../../../../src/plugins/data/public'; -export class AbstractESSource extends AbstractVectorSource {} +export interface IESSource extends IVectorSource { + getIndexPattern(): Promise; +} + +export class AbstractESSource extends AbstractVectorSource implements IESSource { + getIndexPattern(): Promise; +} 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 30f60f543d38d..c12b4befc0684 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 @@ -105,7 +105,13 @@ export class ESTermSource extends AbstractESAggSource { requestName: `${this._descriptor.indexPatternTitle}.${this._termField.getName()}`, searchSource, registerCancelCallback, - requestDescription: this._getRequestDescription(leftSourceName, leftFieldName), + requestDescription: i18n.translate('xpack.maps.source.esJoin.joinDescription', { + defaultMessage: `Elasticsearch terms aggregation request, left source: {leftSource}, right source: {rightSource}`, + values: { + leftSource: `${leftSourceName}:${leftFieldName}`, + rightSource: `${this._descriptor.indexPatternTitle}:${this._termField.getName()}`, + }, + }), }); const countPropertyName = this.getAggKey(AGG_TYPE.COUNT); @@ -118,30 +124,6 @@ export class ESTermSource extends AbstractESAggSource { return false; } - _getRequestDescription(leftSourceName, leftFieldName) { - const metrics = this.getMetricFields().map(esAggMetric => esAggMetric.getRequestDescription()); - const joinStatement = []; - joinStatement.push( - i18n.translate('xpack.maps.source.esJoin.joinLeftDescription', { - defaultMessage: `Join {leftSourceName}:{leftFieldName} with`, - values: { leftSourceName, leftFieldName }, - }) - ); - joinStatement.push(`${this._descriptor.indexPatternTitle}:${this._termField.getName()}`); - joinStatement.push( - i18n.translate('xpack.maps.source.esJoin.joinMetricsDescription', { - defaultMessage: `for metrics {metrics}`, - values: { metrics: metrics.join(',') }, - }) - ); - return i18n.translate('xpack.maps.source.esJoin.joinDescription', { - defaultMessage: `Elasticsearch terms aggregation request for {description}`, - values: { - description: joinStatement.join(' '), - }, - }); - } - async getDisplayName() { //no need to localize. this is never rendered. return `es_table ${this._descriptor.indexPatternId}`; 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 3952aacf03b33..8369ca562e14b 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 @@ -54,7 +54,7 @@ export class AbstractVectorSource extends AbstractSource { * factory function creating a new field-instance * @param fieldName * @param label - * @returns {ESAggMetricField} + * @returns {IField} */ createField() { throw new Error(`Should implemement ${this.constructor.type} ${this}`); @@ -64,7 +64,7 @@ export class AbstractVectorSource extends AbstractSource { * Retrieves a field. This may be an existing instance. * @param fieldName * @param label - * @returns {ESAggMetricField} + * @returns {IField} */ getFieldByName(name) { return this.createField({ fieldName: name }); diff --git a/x-pack/legacy/plugins/maps/public/layers/styles/vector/properties/dynamic_style_property.js b/x-pack/legacy/plugins/maps/public/layers/styles/vector/properties/dynamic_style_property.js index 19e80f330378b..7b94e58f0e7d4 100644 --- a/x-pack/legacy/plugins/maps/public/layers/styles/vector/properties/dynamic_style_property.js +++ b/x-pack/legacy/plugins/maps/public/layers/styles/vector/properties/dynamic_style_property.js @@ -152,10 +152,7 @@ export class DynamicStyleProperty extends AbstractStyleProperty { async getFieldMetaRequest() { if (this.isOrdinal()) { - const fieldMetaOptions = this.getFieldMetaOptions(); - return this._field.getOrdinalFieldMetaRequest({ - sigma: _.get(fieldMetaOptions, 'sigma', DEFAULT_SIGMA), - }); + return this._field.getOrdinalFieldMetaRequest(); } else if (this.isCategorical()) { return this._field.getCategoricalFieldMetaRequest(); } else { diff --git a/x-pack/legacy/plugins/maps/public/layers/util/es_agg_utils.test.ts b/x-pack/legacy/plugins/maps/public/layers/util/es_agg_utils.test.ts index 201d6907981a2..445a7621194b7 100644 --- a/x-pack/legacy/plugins/maps/public/layers/util/es_agg_utils.test.ts +++ b/x-pack/legacy/plugins/maps/public/layers/util/es_agg_utils.test.ts @@ -19,19 +19,34 @@ describe('extractPropertiesFromBucket', () => { }); }); - test('Should extract bucket aggregation values', () => { + test('Should extract top bucket aggregation value and percentage', () => { const properties = extractPropertiesFromBucket({ + doc_count: 3, 'terms_of_machine.os.keyword': { buckets: [ { key: 'win xp', - doc_count: 16, + doc_count: 1, }, ], }, }); expect(properties).toEqual({ + doc_count: 3, 'terms_of_machine.os.keyword': 'win xp', + 'terms_of_machine.os.keyword__percentage': 33, + }); + }); + + test('Should handle empty top bucket aggregation', () => { + const properties = extractPropertiesFromBucket({ + doc_count: 3, + 'terms_of_machine.os.keyword': { + buckets: [], + }, + }); + expect(properties).toEqual({ + doc_count: 3, }); }); }); diff --git a/x-pack/legacy/plugins/maps/public/layers/util/es_agg_utils.ts b/x-pack/legacy/plugins/maps/public/layers/util/es_agg_utils.ts index 7af176acfaf46..9d4f24f80d6cd 100644 --- a/x-pack/legacy/plugins/maps/public/layers/util/es_agg_utils.ts +++ b/x-pack/legacy/plugins/maps/public/layers/util/es_agg_utils.ts @@ -6,6 +6,7 @@ import { i18n } from '@kbn/i18n'; import _ from 'lodash'; import { IndexPattern, IFieldType } from '../../../../../../../src/plugins/data/public'; +import { TOP_TERM_PERCENTAGE_SUFFIX } from '../../../common/constants'; export function getField(indexPattern: IndexPattern, fieldName: string) { const field = indexPattern.fields.getByName(fieldName); @@ -42,7 +43,19 @@ export function extractPropertiesFromBucket(bucket: any, ignoreKeys: string[] = if (_.has(bucket[key], 'value')) { properties[key] = bucket[key].value; } else if (_.has(bucket[key], 'buckets')) { + if (bucket[key].buckets.length === 0) { + // No top term + continue; + } + properties[key] = _.get(bucket[key], 'buckets[0].key'); + const topBucketCount = bucket[key].buckets[0].doc_count; + const totalCount = bucket.doc_count; + if (totalCount && topBucketCount) { + properties[`${key}${TOP_TERM_PERCENTAGE_SUFFIX}`] = Math.round( + (topBucketCount / totalCount) * 100 + ); + } } else { properties[key] = bucket[key]; } diff --git a/x-pack/legacy/plugins/maps/public/layers/util/is_metric_countable.js b/x-pack/legacy/plugins/maps/public/layers/util/is_metric_countable.ts similarity index 85% rename from x-pack/legacy/plugins/maps/public/layers/util/is_metric_countable.js rename to x-pack/legacy/plugins/maps/public/layers/util/is_metric_countable.ts index 69ccb8890d10c..37916e53d6c45 100644 --- a/x-pack/legacy/plugins/maps/public/layers/util/is_metric_countable.js +++ b/x-pack/legacy/plugins/maps/public/layers/util/is_metric_countable.ts @@ -6,6 +6,6 @@ import { AGG_TYPE } from '../../../common/constants'; -export function isMetricCountable(aggType) { +export function isMetricCountable(aggType: AGG_TYPE): boolean { return [AGG_TYPE.COUNT, AGG_TYPE.SUM, AGG_TYPE.UNIQUE_COUNT].includes(aggType); } diff --git a/x-pack/plugins/translations/translations/ja-JP.json b/x-pack/plugins/translations/translations/ja-JP.json index 195b75f84a8c0..c57de95ecb82c 100644 --- a/x-pack/plugins/translations/translations/ja-JP.json +++ b/x-pack/plugins/translations/translations/ja-JP.json @@ -7257,9 +7257,6 @@ "xpack.maps.source.esGrid.showasFieldLabel": "表示形式", "xpack.maps.source.esGridDescription": "それぞれのグリッド付きセルのメトリックでグリッドにグループ分けされた地理空間データです。", "xpack.maps.source.esGridTitle": "グリッド集約", - "xpack.maps.source.esJoin.joinDescription": "{description} の Elasticsearch 用語集約リクエストです", - "xpack.maps.source.esJoin.joinLeftDescription": "{leftSourceName}:{leftFieldName} を次と結合:", - "xpack.maps.source.esJoin.joinMetricsDescription": "メトリック {metrics} の", "xpack.maps.source.esSearch.convertToGeoJsonErrorMsg": "検索への応答を geoJson 機能コレクションに変換できません。エラー: {errorMsg}", "xpack.maps.source.esSearch.disableFilterByMapBoundsExplainMsg": "インデックス「{indexPatternTitle}」はドキュメント数が少なく、ダイナミックフィルターが必要ありません。", "xpack.maps.source.esSearch.disableFilterByMapBoundsTitle": "ダイナミックデータフィルターは無効です", diff --git a/x-pack/plugins/translations/translations/zh-CN.json b/x-pack/plugins/translations/translations/zh-CN.json index 9add5c6bcdbc3..f17f2eb509cf0 100644 --- a/x-pack/plugins/translations/translations/zh-CN.json +++ b/x-pack/plugins/translations/translations/zh-CN.json @@ -7257,9 +7257,6 @@ "xpack.maps.source.esGrid.showasFieldLabel": "显示为", "xpack.maps.source.esGridDescription": "地理空间数据在网格中进行分组,每个网格单元格都具有指标", "xpack.maps.source.esGridTitle": "网格聚合", - "xpack.maps.source.esJoin.joinDescription": "{description} 的 Elasticsearch 词聚合请求", - "xpack.maps.source.esJoin.joinLeftDescription": "将 {leftSourceName}:{leftFieldName} 联接到", - "xpack.maps.source.esJoin.joinMetricsDescription": "以获取指标 {metrics}", "xpack.maps.source.esSearch.convertToGeoJsonErrorMsg": "无法将搜索响应转换成 geoJson 功能集合,错误:{errorMsg}", "xpack.maps.source.esSearch.disableFilterByMapBoundsExplainMsg": "索引“{indexPatternTitle}”具有很少数量的文档,不需要动态筛选。", "xpack.maps.source.esSearch.disableFilterByMapBoundsTitle": "动态数据筛选已禁用",