From 750ffdb1eb2f6f8143ca023a06976e1c29581271 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Mon, 27 Jan 2020 13:21:27 -0500 Subject: [PATCH 1/3] lint fixes --- .../maps/public/components/metric_editor.js | 36 +++++++++++++------ .../layer_panel/join_editor/resources/join.js | 2 +- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/x-pack/legacy/plugins/maps/public/components/metric_editor.js b/x-pack/legacy/plugins/maps/public/components/metric_editor.js index f6f5b23f14596..830c33d0b7030 100644 --- a/x-pack/legacy/plugins/maps/public/components/metric_editor.js +++ b/x-pack/legacy/plugins/maps/public/components/metric_editor.js @@ -14,12 +14,35 @@ import { MetricSelect, METRIC_AGGREGATION_VALUES } from './metric_select'; import { SingleFieldSelect } from './single_field_select'; import { METRIC_TYPE } from '../../common/constants'; +function filterFieldsForAgg(fields, aggType) { + if (aggType === METRIC_TYPE.UNIQUE_COUNT) { + return fields; + } + + return fields.filter(field => { + return field.type === 'number'; + }); +} + export function MetricEditor({ fields, metricsFilter, metric, onChange, removeButton }) { const onAggChange = metricAggregationType => { - onChange({ + const newMetricProps = { ...metric, type: metricAggregationType, - }); + }; + + // unset field when new agg type does not support currently selected field. + if (metric.field && metricAggregationType !== METRIC_TYPE.COUNT) { + const fieldsForNewAggType = filterFieldsForAgg(fields, metricAggregationType); + const found = fieldsForNewAggType.find(field => { + return field.name === metric.field; + }); + if (!found) { + newMetricProps.field = undefined; + } + } + + onChange(newMetricProps); }; const onFieldChange = fieldName => { onChange({ @@ -36,12 +59,6 @@ export function MetricEditor({ fields, metricsFilter, metric, onChange, removeBu let fieldSelect; if (metric.type && metric.type !== METRIC_TYPE.COUNT) { - const filterField = - metric.type !== METRIC_TYPE.UNIQUE_COUNT - ? field => { - return field.type === 'number'; - } - : undefined; fieldSelect = ( diff --git a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js index 554164bf0e8c4..e83f06c1e6b81 100644 --- a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js +++ b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js @@ -89,7 +89,7 @@ export class Join extends Component { } this.setState({ - rightFields: indexPattern.fields.filter(field => !isNestedField(field)), + rightFields: indexPattern.fields.filter(field => field.aggregatable && !isNestedField(field)), indexPattern, }); } From 85ab70d2d1bb9b2b1ffc2157efa83ce138df93df Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Mon, 27 Jan 2020 13:37:32 -0500 Subject: [PATCH 2/3] move aggregation check to MEtricEditor --- .../legacy/plugins/maps/public/components/metric_editor.js | 6 ++++-- .../layer_panel/join_editor/resources/join.js | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/x-pack/legacy/plugins/maps/public/components/metric_editor.js b/x-pack/legacy/plugins/maps/public/components/metric_editor.js index 830c33d0b7030..fc66ecc3b36f2 100644 --- a/x-pack/legacy/plugins/maps/public/components/metric_editor.js +++ b/x-pack/legacy/plugins/maps/public/components/metric_editor.js @@ -16,11 +16,13 @@ import { METRIC_TYPE } from '../../common/constants'; function filterFieldsForAgg(fields, aggType) { if (aggType === METRIC_TYPE.UNIQUE_COUNT) { - return fields; + return fields.filter(field => { + return field.aggregatable; + }); } return fields.filter(field => { - return field.type === 'number'; + return field.aggregatable && field.type === 'number'; }); } diff --git a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js index e83f06c1e6b81..554164bf0e8c4 100644 --- a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js +++ b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js @@ -89,7 +89,7 @@ export class Join extends Component { } this.setState({ - rightFields: indexPattern.fields.filter(field => field.aggregatable && !isNestedField(field)), + rightFields: indexPattern.fields.filter(field => !isNestedField(field)), indexPattern, }); } From adda399177da7cb4474ef1e143a3de5e4da59767 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Mon, 27 Jan 2020 17:57:43 -0500 Subject: [PATCH 3/3] fix functional test, handle case where fields are not loaded --- x-pack/legacy/plugins/maps/public/components/metric_editor.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x-pack/legacy/plugins/maps/public/components/metric_editor.js b/x-pack/legacy/plugins/maps/public/components/metric_editor.js index fc66ecc3b36f2..e60c2ac0dd7ab 100644 --- a/x-pack/legacy/plugins/maps/public/components/metric_editor.js +++ b/x-pack/legacy/plugins/maps/public/components/metric_editor.js @@ -15,6 +15,10 @@ import { SingleFieldSelect } from './single_field_select'; import { METRIC_TYPE } from '../../common/constants'; function filterFieldsForAgg(fields, aggType) { + if (!fields) { + return []; + } + if (aggType === METRIC_TYPE.UNIQUE_COUNT) { return fields.filter(field => { return field.aggregatable;