Skip to content

Commit

Permalink
[Maps] fix join metric field selection bugs (#56044) (#56088)
Browse files Browse the repository at this point in the history
* lint fixes

* move aggregation check to MEtricEditor

* fix functional test, handle case where fields are not loaded
  • Loading branch information
nreese authored Jan 28, 2020
1 parent 99de5ee commit 11b7ad8
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions x-pack/legacy/plugins/maps/public/components/metric_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,41 @@ 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 (!fields) {
return [];
}

if (aggType === METRIC_TYPE.UNIQUE_COUNT) {
return fields.filter(field => {
return field.aggregatable;
});
}

return fields.filter(field => {
return field.aggregatable && 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({
Expand All @@ -36,12 +65,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 = (
<EuiFormRow
label={i18n.translate('xpack.maps.metricsEditor.selectFieldLabel', {
Expand All @@ -55,8 +78,7 @@ export function MetricEditor({ fields, metricsFilter, metric, onChange, removeBu
})}
value={metric.field}
onChange={onFieldChange}
filterField={filterField}
fields={fields}
fields={filterFieldsForAgg(fields, metric.type)}
isClearable={false}
compressed
/>
Expand Down

0 comments on commit 11b7ad8

Please sign in to comment.