Skip to content

Commit

Permalink
[Maps] fix unable to edit heatmap metric (#70606)
Browse files Browse the repository at this point in the history
* [Maps] fix unable to edit heatmap metric

* add comment

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
nreese and elasticmachine authored Jul 2, 2020
1 parent fd307a0 commit e774921
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { esAggFieldsFactory } from '../../fields/es_agg_field';
import { AGG_TYPE, COUNT_PROP_LABEL, FIELD_ORIGIN } from '../../../../common/constants';
import { getSourceAggKey } from '../../../../common/get_agg_key';

export const DEFAULT_METRIC = { type: AGG_TYPE.COUNT };

export class AbstractESAggSource extends AbstractESSource {
constructor(descriptor, inspectorAdapters) {
super(descriptor, inspectorAdapters);
Expand Down Expand Up @@ -48,6 +50,7 @@ export class AbstractESAggSource extends AbstractESSource {

getMetricFields() {
const metrics = this._metricFields.filter((esAggField) => esAggField.isValid());
// Handle case where metrics is empty because older saved object state is empty array or there are no valid aggs.
return metrics.length === 0
? esAggFieldsFactory({ type: AGG_TYPE.COUNT }, this, this.getOriginForField())
: metrics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '../../../../common/constants';
import { i18n } from '@kbn/i18n';
import { getDataSourceLabel } from '../../../../common/i18n_getters';
import { AbstractESAggSource } from '../es_agg_source';
import { AbstractESAggSource, DEFAULT_METRIC } from '../es_agg_source';
import { DataRequestAbortError } from '../../util/data_request';
import { registerSource } from '../source_registry';
import { makeESBbox } from '../../../elasticsearch_geo_utils';
Expand All @@ -42,7 +42,7 @@ export class ESGeoGridSource extends AbstractESAggSource {
id: uuid(),
indexPatternId,
geoField,
metrics: metrics ? metrics : [],
metrics: metrics ? metrics : [DEFAULT_METRIC],
requestType,
resolution: resolution ? resolution : GRID_RESOLUTION.COARSE,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { i18n } from '@kbn/i18n';
import { SOURCE_TYPES, VECTOR_SHAPE_TYPE } from '../../../../common/constants';
import { getDataSourceLabel } from '../../../../common/i18n_getters';
import { convertToLines } from './convert_to_lines';
import { AbstractESAggSource } from '../es_agg_source';
import { AbstractESAggSource, DEFAULT_METRIC } from '../es_agg_source';
import { indexPatterns } from '../../../../../../../src/plugins/data/public';
import { registerSource } from '../source_registry';

Expand All @@ -32,7 +32,7 @@ export class ESPewPewSource extends AbstractESAggSource {
indexPatternId: indexPatternId,
sourceGeoField,
destGeoField,
metrics: metrics ? metrics : [],
metrics: metrics ? metrics : [DEFAULT_METRIC],
};
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions x-pack/plugins/maps/public/components/metrics_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiButtonEmpty, EuiSpacer, EuiTextAlign } from '@elastic/eui';
import { MetricEditor } from './metric_editor';
import { AGG_TYPE } from '../../common/constants';
import { DEFAULT_METRIC } from '../classes/sources/es_agg_source';

export function MetricsEditor({ fields, metrics, onChange, allowMultipleMetrics, metricsFilter }) {
function renderMetrics() {
return metrics.map((metric, index) => {
// There was a bug in 7.8 that initialized metrics to [].
// This check is needed to handle any saved objects created before the bug was patched.
const nonEmptyMetrics = metrics.length === 0 ? [DEFAULT_METRIC] : metrics;
return nonEmptyMetrics.map((metric, index) => {
const onMetricChange = (metric) => {
onChange([...metrics.slice(0, index), metric, ...metrics.slice(index + 1)]);
};
Expand Down Expand Up @@ -100,6 +103,6 @@ MetricsEditor.propTypes = {
};

MetricsEditor.defaultProps = {
metrics: [{ type: AGG_TYPE.COUNT }],
metrics: [DEFAULT_METRIC],
allowMultipleMetrics: true,
};
33 changes: 33 additions & 0 deletions x-pack/plugins/maps/public/components/metrics_editor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 React from 'react';
import { shallow } from 'enzyme';
import { MetricsEditor } from './metrics_editor';
import { AGG_TYPE } from '../../common/constants';

const defaultProps = {
metrics: [
{
type: AGG_TYPE.SUM,
field: 'myField',
},
],
fields: [],
onChange: () => {},
allowMultipleMetrics: true,
metricsFilter: () => {},
};

test('should render metrics editor', async () => {
const component = shallow(<MetricsEditor {...defaultProps} />);
expect(component).toMatchSnapshot();
});

test('should add default count metric when metrics is empty array', async () => {
const component = shallow(<MetricsEditor {...defaultProps} metrics={[]} />);
expect(component).toMatchSnapshot();
});

0 comments on commit e774921

Please sign in to comment.