Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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 '../layers/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();
});
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';

Expand All @@ -41,7 +41,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