Skip to content

Commit d97f19d

Browse files
committed
Merge branch 'master' of github.com:elastic/kibana into security/authc-telemetry
2 parents f0fa4e9 + 42a6934 commit d97f19d

File tree

29 files changed

+724
-580
lines changed

29 files changed

+724
-580
lines changed

src/core/server/saved_objects/migrations/core/index_migrator.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,30 @@ describe('IndexMigrator', () => {
369369
],
370370
});
371371
});
372+
373+
test('rejects when the migration function throws an error', async () => {
374+
const { client } = testOpts;
375+
const migrateDoc = jest.fn((doc: SavedObjectUnsanitizedDoc) => {
376+
throw new Error('error migrating document');
377+
});
378+
379+
testOpts.documentMigrator = {
380+
migrationVersion: { foo: '1.2.3' },
381+
migrate: migrateDoc,
382+
};
383+
384+
withIndex(client, {
385+
numOutOfDate: 1,
386+
docs: [
387+
[{ _id: 'foo:1', _source: { type: 'foo', foo: { name: 'Bar' } } }],
388+
[{ _id: 'foo:2', _source: { type: 'foo', foo: { name: 'Baz' } } }],
389+
],
390+
});
391+
392+
await expect(new IndexMigrator(testOpts).migrate()).rejects.toThrowErrorMatchingInlineSnapshot(
393+
`"error migrating document"`
394+
);
395+
});
372396
});
373397

374398
function withIndex(

src/core/server/saved_objects/migrations/core/migrate_raw_docs.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,18 @@ describe('migrateRawDocs', () => {
9090

9191
expect(logger.error).toBeCalledTimes(1);
9292
});
93+
94+
test('rejects when the transform function throws an error', async () => {
95+
const transform = jest.fn<any, any>((doc: any) => {
96+
throw new Error('error during transform');
97+
});
98+
await expect(
99+
migrateRawDocs(
100+
new SavedObjectsSerializer(new SavedObjectTypeRegistry()),
101+
transform,
102+
[{ _id: 'a:b', _source: { type: 'a', a: { name: 'AAA' } } }],
103+
createSavedObjectsMigrationLoggerMock()
104+
)
105+
).rejects.toThrowErrorMatchingInlineSnapshot(`"error during transform"`);
106+
});
93107
});

src/core/server/saved_objects/migrations/core/migrate_raw_docs.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ function transformNonBlocking(
7878
): (doc: SavedObjectUnsanitizedDoc) => Promise<SavedObjectUnsanitizedDoc> {
7979
// promises aren't enough to unblock the event loop
8080
return (doc: SavedObjectUnsanitizedDoc) =>
81-
new Promise((resolve) => {
81+
new Promise((resolve, reject) => {
8282
// set immediate is though
8383
setImmediate(() => {
84-
resolve(transform(doc));
84+
try {
85+
resolve(transform(doc));
86+
} catch (e) {
87+
reject(e);
88+
}
8589
});
8690
});
8791
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@import 'action_select';
2-
@import 'metric_editors';
2+
@import 'metrics_editor/metric_editors';
33
@import './geometry_filter';
44
@import 'tooltip_selector/tooltip_selector';

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

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
export { MetricsEditor } from './metrics_editor';

x-pack/plugins/maps/public/components/metric_editor.js renamed to x-pack/plugins/maps/public/components/metrics_editor/metric_editor.tsx

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import React, { Fragment } from 'react';
8-
import PropTypes from 'prop-types';
7+
import React, { ChangeEvent, Fragment } from 'react';
98
import { i18n } from '@kbn/i18n';
109

11-
import { EuiFieldText, EuiFormRow } from '@elastic/eui';
10+
import { EuiButtonEmpty, EuiComboBoxOptionOption, EuiFieldText, EuiFormRow } from '@elastic/eui';
1211

13-
import { MetricSelect, METRIC_AGGREGATION_VALUES } from './metric_select';
14-
import { SingleFieldSelect } from './single_field_select';
15-
import { AGG_TYPE } from '../../common/constants';
16-
import { getTermsFields } from '../index_pattern_util';
12+
import { FormattedMessage } from '@kbn/i18n/react';
13+
import { MetricSelect } from './metric_select';
14+
import { SingleFieldSelect } from '../single_field_select';
15+
import { AggDescriptor } from '../../../common/descriptor_types';
16+
import { AGG_TYPE } from '../../../common/constants';
17+
import { getTermsFields } from '../../index_pattern_util';
18+
import { IFieldType } from '../../../../../../src/plugins/data/public';
1719

18-
function filterFieldsForAgg(fields, aggType) {
20+
function filterFieldsForAgg(fields: IFieldType[], aggType: AGG_TYPE) {
1921
if (!fields) {
2022
return [];
2123
}
@@ -34,8 +36,27 @@ function filterFieldsForAgg(fields, aggType) {
3436
});
3537
}
3638

37-
export function MetricEditor({ fields, metricsFilter, metric, onChange, removeButton }) {
38-
const onAggChange = (metricAggregationType) => {
39+
interface Props {
40+
metric: AggDescriptor;
41+
fields: IFieldType[];
42+
onChange: (metric: AggDescriptor) => void;
43+
onRemove: () => void;
44+
metricsFilter?: (metricOption: EuiComboBoxOptionOption<AGG_TYPE>) => boolean;
45+
showRemoveButton: boolean;
46+
}
47+
48+
export function MetricEditor({
49+
fields,
50+
metricsFilter,
51+
metric,
52+
onChange,
53+
showRemoveButton,
54+
onRemove,
55+
}: Props) {
56+
const onAggChange = (metricAggregationType?: AGG_TYPE) => {
57+
if (!metricAggregationType) {
58+
return;
59+
}
3960
const newMetricProps = {
4061
...metric,
4162
type: metricAggregationType,
@@ -54,13 +75,16 @@ export function MetricEditor({ fields, metricsFilter, metric, onChange, removeBu
5475

5576
onChange(newMetricProps);
5677
};
57-
const onFieldChange = (fieldName) => {
78+
const onFieldChange = (fieldName?: string) => {
79+
if (!fieldName) {
80+
return;
81+
}
5882
onChange({
5983
...metric,
6084
field: fieldName,
6185
});
6286
};
63-
const onLabelChange = (e) => {
87+
const onLabelChange = (e: ChangeEvent<HTMLInputElement>) => {
6488
onChange({
6589
...metric,
6690
label: e.target.value,
@@ -80,7 +104,7 @@ export function MetricEditor({ fields, metricsFilter, metric, onChange, removeBu
80104
placeholder={i18n.translate('xpack.maps.metricsEditor.selectFieldPlaceholder', {
81105
defaultMessage: 'Select field',
82106
})}
83-
value={metric.field}
107+
value={metric.field ? metric.field : null}
84108
onChange={onFieldChange}
85109
fields={filterFieldsForAgg(fields, metric.type)}
86110
isClearable={false}
@@ -108,6 +132,28 @@ export function MetricEditor({ fields, metricsFilter, metric, onChange, removeBu
108132
);
109133
}
110134

135+
let removeButton;
136+
if (showRemoveButton) {
137+
removeButton = (
138+
<div className="mapMetricEditorPanel__metricRemoveButton">
139+
<EuiButtonEmpty
140+
iconType="trash"
141+
size="xs"
142+
color="danger"
143+
onClick={onRemove}
144+
aria-label={i18n.translate('xpack.maps.metricsEditor.deleteMetricAriaLabel', {
145+
defaultMessage: 'Delete metric',
146+
})}
147+
>
148+
<FormattedMessage
149+
id="xpack.maps.metricsEditor.deleteMetricButtonLabel"
150+
defaultMessage="Delete metric"
151+
/>
152+
</EuiButtonEmpty>
153+
</div>
154+
);
155+
}
156+
111157
return (
112158
<Fragment>
113159
<EuiFormRow
@@ -130,14 +176,3 @@ export function MetricEditor({ fields, metricsFilter, metric, onChange, removeBu
130176
</Fragment>
131177
);
132178
}
133-
134-
MetricEditor.propTypes = {
135-
metric: PropTypes.shape({
136-
type: PropTypes.oneOf(METRIC_AGGREGATION_VALUES),
137-
field: PropTypes.string,
138-
label: PropTypes.string,
139-
}),
140-
fields: PropTypes.array,
141-
onChange: PropTypes.func.isRequired,
142-
metricsFilter: PropTypes.func,
143-
};

x-pack/plugins/maps/public/components/metric_select.js renamed to x-pack/plugins/maps/public/components/metrics_editor/metric_select.tsx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
*/
66

77
import React from 'react';
8-
import PropTypes from 'prop-types';
98
import { i18n } from '@kbn/i18n';
10-
import { EuiComboBox } from '@elastic/eui';
11-
import { AGG_TYPE } from '../../common/constants';
9+
import { EuiComboBox, EuiComboBoxOptionOption, EuiComboBoxProps } from '@elastic/eui';
10+
import { AGG_TYPE } from '../../../common/constants';
1211

1312
const AGG_OPTIONS = [
1413
{
@@ -55,17 +54,19 @@ const AGG_OPTIONS = [
5554
},
5655
];
5756

58-
export const METRIC_AGGREGATION_VALUES = AGG_OPTIONS.map(({ value }) => {
59-
return value;
60-
});
57+
type Props = Omit<EuiComboBoxProps<AGG_TYPE>, 'onChange'> & {
58+
value: AGG_TYPE;
59+
onChange: (aggType: AGG_TYPE) => void;
60+
metricsFilter?: (metricOption: EuiComboBoxOptionOption<AGG_TYPE>) => boolean;
61+
};
6162

62-
export function MetricSelect({ value, onChange, metricsFilter, ...rest }) {
63-
function onAggChange(selectedOptions) {
63+
export function MetricSelect({ value, onChange, metricsFilter, ...rest }: Props) {
64+
function onAggChange(selectedOptions: Array<EuiComboBoxOptionOption<AGG_TYPE>>) {
6465
if (selectedOptions.length === 0) {
6566
return;
6667
}
6768

68-
const aggType = selectedOptions[0].value;
69+
const aggType = selectedOptions[0].value!;
6970
onChange(aggType);
7071
}
7172

@@ -87,9 +88,3 @@ export function MetricSelect({ value, onChange, metricsFilter, ...rest }) {
8788
/>
8889
);
8990
}
90-
91-
MetricSelect.propTypes = {
92-
metricsFilter: PropTypes.func,
93-
value: PropTypes.oneOf(METRIC_AGGREGATION_VALUES),
94-
onChange: PropTypes.func.isRequired,
95-
};

x-pack/plugins/maps/public/components/metrics_editor.test.js renamed to x-pack/plugins/maps/public/components/metrics_editor/metrics_editor.test.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import React from 'react';
88
import { shallow } from 'enzyme';
99
import { MetricsEditor } from './metrics_editor';
10-
import { AGG_TYPE } from '../../common/constants';
10+
import { AGG_TYPE } from '../../../common/constants';
1111

1212
const defaultProps = {
1313
metrics: [
@@ -19,15 +19,14 @@ const defaultProps = {
1919
fields: [],
2020
onChange: () => {},
2121
allowMultipleMetrics: true,
22-
metricsFilter: () => {},
2322
};
2423

25-
test('should render metrics editor', async () => {
24+
test('should render metrics editor', () => {
2625
const component = shallow(<MetricsEditor {...defaultProps} />);
2726
expect(component).toMatchSnapshot();
2827
});
2928

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

0 commit comments

Comments
 (0)