Skip to content

Commit c50a3bf

Browse files
[Maps] fix unable to edit heatmap metric (#70606)
* [Maps] fix unable to edit heatmap metric * add comment Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent aa4d3f4 commit c50a3bf

File tree

6 files changed

+132
-7
lines changed

6 files changed

+132
-7
lines changed

x-pack/plugins/maps/public/classes/sources/es_agg_source/es_agg_source.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { esAggFieldsFactory } from '../../fields/es_agg_field';
1010
import { AGG_TYPE, COUNT_PROP_LABEL, FIELD_ORIGIN } from '../../../../common/constants';
1111
import { getSourceAggKey } from '../../../../common/get_agg_key';
1212

13+
export const DEFAULT_METRIC = { type: AGG_TYPE.COUNT };
14+
1315
export class AbstractESAggSource extends AbstractESSource {
1416
constructor(descriptor, inspectorAdapters) {
1517
super(descriptor, inspectorAdapters);
@@ -48,6 +50,7 @@ export class AbstractESAggSource extends AbstractESSource {
4850

4951
getMetricFields() {
5052
const metrics = this._metricFields.filter((esAggField) => esAggField.isValid());
53+
// Handle case where metrics is empty because older saved object state is empty array or there are no valid aggs.
5154
return metrics.length === 0
5255
? esAggFieldsFactory({ type: AGG_TYPE.COUNT }, this, this.getOriginForField())
5356
: metrics;

x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from '../../../../common/constants';
1919
import { i18n } from '@kbn/i18n';
2020
import { getDataSourceLabel } from '../../../../common/i18n_getters';
21-
import { AbstractESAggSource } from '../es_agg_source';
21+
import { AbstractESAggSource, DEFAULT_METRIC } from '../es_agg_source';
2222
import { DataRequestAbortError } from '../../util/data_request';
2323
import { registerSource } from '../source_registry';
2424
import { makeESBbox } from '../../../elasticsearch_geo_utils';
@@ -42,7 +42,7 @@ export class ESGeoGridSource extends AbstractESAggSource {
4242
id: uuid(),
4343
indexPatternId,
4444
geoField,
45-
metrics: metrics ? metrics : [],
45+
metrics: metrics ? metrics : [DEFAULT_METRIC],
4646
requestType,
4747
resolution: resolution ? resolution : GRID_RESOLUTION.COARSE,
4848
};

x-pack/plugins/maps/public/classes/sources/es_pew_pew_source/es_pew_pew_source.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { i18n } from '@kbn/i18n';
1212
import { SOURCE_TYPES, VECTOR_SHAPE_TYPE } from '../../../../common/constants';
1313
import { getDataSourceLabel } from '../../../../common/i18n_getters';
1414
import { convertToLines } from './convert_to_lines';
15-
import { AbstractESAggSource } from '../es_agg_source';
15+
import { AbstractESAggSource, DEFAULT_METRIC } from '../es_agg_source';
1616
import { indexPatterns } from '../../../../../../../src/plugins/data/public';
1717
import { registerSource } from '../source_registry';
1818

@@ -32,7 +32,7 @@ export class ESPewPewSource extends AbstractESAggSource {
3232
indexPatternId: indexPatternId,
3333
sourceGeoField,
3434
destGeoField,
35-
metrics: metrics ? metrics : [],
35+
metrics: metrics ? metrics : [DEFAULT_METRIC],
3636
};
3737
}
3838

x-pack/plugins/maps/public/components/__snapshots__/metrics_editor.test.js.snap

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugins/maps/public/components/metrics_editor.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import { i18n } from '@kbn/i18n';
1010
import { FormattedMessage } from '@kbn/i18n/react';
1111
import { EuiButtonEmpty, EuiSpacer, EuiTextAlign } from '@elastic/eui';
1212
import { MetricEditor } from './metric_editor';
13-
import { AGG_TYPE } from '../../common/constants';
13+
import { DEFAULT_METRIC } from '../classes/sources/es_agg_source';
1414

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

102105
MetricsEditor.defaultProps = {
103-
metrics: [{ type: AGG_TYPE.COUNT }],
106+
metrics: [DEFAULT_METRIC],
104107
allowMultipleMetrics: true,
105108
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import React from 'react';
8+
import { shallow } from 'enzyme';
9+
import { MetricsEditor } from './metrics_editor';
10+
import { AGG_TYPE } from '../../common/constants';
11+
12+
const defaultProps = {
13+
metrics: [
14+
{
15+
type: AGG_TYPE.SUM,
16+
field: 'myField',
17+
},
18+
],
19+
fields: [],
20+
onChange: () => {},
21+
allowMultipleMetrics: true,
22+
metricsFilter: () => {},
23+
};
24+
25+
test('should render metrics editor', async () => {
26+
const component = shallow(<MetricsEditor {...defaultProps} />);
27+
expect(component).toMatchSnapshot();
28+
});
29+
30+
test('should add default count metric when metrics is empty array', async () => {
31+
const component = shallow(<MetricsEditor {...defaultProps} metrics={[]} />);
32+
expect(component).toMatchSnapshot();
33+
});

0 commit comments

Comments
 (0)