Skip to content

Commit 06da0e7

Browse files
TSVB field list performance issue on using annotations (#84407)
* TSVB field list performance issue on using annotations * Add AbortController to fetchFields and change translation id in annotations_editor * Rename fetchFields to debouncedFetchFields Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent 4546352 commit 06da0e7

File tree

6 files changed

+54
-22
lines changed

6 files changed

+54
-22
lines changed

src/plugins/vis_type_timeseries/common/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ export const INDEXES_SEPARATOR = ',';
2222
export const AUTO_INTERVAL = 'auto';
2323
export const ROUTES = {
2424
VIS_DATA: '/api/metrics/vis/data',
25+
FIELDS: '/api/metrics/fields',
2526
};

src/plugins/vis_type_timeseries/public/application/components/aggs/field_select.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function FieldSelectUi({
9191
}
9292

9393
FieldSelectUi.defaultProps = {
94-
indexPattern: '*',
94+
indexPattern: '',
9595
disabled: false,
9696
restrict: [],
9797
placeholder: i18n.translate('visTypeTimeseries.fieldSelect.selectFieldPlaceholder', {

src/plugins/vis_type_timeseries/public/application/components/annotations_editor.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ import {
4343
EuiCode,
4444
EuiText,
4545
} from '@elastic/eui';
46+
import { i18n } from '@kbn/i18n';
4647
import { FormattedMessage } from '@kbn/i18n/react';
4748

4849
function newAnnotation() {
4950
return {
5051
id: uuid.v1(),
5152
color: '#F00',
52-
index_pattern: '*',
53+
index_pattern: '',
5354
time_field: '@timestamp',
5455
icon: 'fa-tag',
5556
ignore_global_filters: 1,
@@ -84,7 +85,7 @@ export class AnnotationsEditor extends Component {
8485
const defaults = {
8586
fields: '',
8687
template: '',
87-
index_pattern: '*',
88+
index_pattern: '',
8889
query_string: { query: '', language: getDefaultQueryLanguage() },
8990
};
9091
const model = { ...defaults, ...row };
@@ -100,6 +101,8 @@ export class AnnotationsEditor extends Component {
100101
const htmlId = htmlIdGenerator(model.id);
101102
const handleAdd = collectionActions.handleAdd.bind(null, this.props, newAnnotation);
102103
const handleDelete = collectionActions.handleDelete.bind(null, this.props, model);
104+
const defaultIndexPattern = this.props.model.default_index_pattern;
105+
103106
return (
104107
<div className="tvbAnnotationsEditor" key={model.id}>
105108
<EuiFlexGroup responsive={false}>
@@ -120,14 +123,22 @@ export class AnnotationsEditor extends Component {
120123
label={
121124
<FormattedMessage
122125
id="visTypeTimeseries.annotationsEditor.indexPatternLabel"
123-
defaultMessage="Index pattern (required)"
126+
defaultMessage="Index pattern"
124127
/>
125128
}
129+
helpText={
130+
defaultIndexPattern &&
131+
!model.index_pattern &&
132+
i18n.translate('visTypeTimeseries.annotationsEditor.searchByDefaultIndex', {
133+
defaultMessage: 'Default index pattern is used. To query all indexes use *',
134+
})
135+
}
126136
fullWidth
127137
>
128138
<EuiFieldText
129139
onChange={this.handleChange(model, 'index_pattern')}
130140
value={model.index_pattern}
141+
placeholder={defaultIndexPattern}
131142
fullWidth
132143
/>
133144
</EuiFormRow>

src/plugins/vis_type_timeseries/public/application/components/vis_editor.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ export class VisEditor extends Component {
7676
});
7777
}, VIS_STATE_DEBOUNCE_DELAY);
7878

79+
debouncedFetchFields = debounce(
80+
(extractedIndexPatterns) => {
81+
if (this.abortControllerFetchFields) {
82+
this.abortControllerFetchFields.abort();
83+
}
84+
this.abortControllerFetchFields = new AbortController();
85+
86+
return fetchFields(extractedIndexPatterns, this.abortControllerFetchFields.signal);
87+
},
88+
VIS_STATE_DEBOUNCE_DELAY,
89+
{ leading: true }
90+
);
91+
7992
handleChange = (partialModel) => {
8093
if (isEmpty(partialModel)) {
8194
return;
@@ -94,7 +107,7 @@ export class VisEditor extends Component {
94107

95108
const extractedIndexPatterns = extractIndexPatterns(nextModel);
96109
if (!isEqual(this.state.extractedIndexPatterns, extractedIndexPatterns)) {
97-
fetchFields(extractedIndexPatterns).then((visFields) =>
110+
this.debouncedFetchFields(extractedIndexPatterns).then((visFields) =>
98111
this.setState({
99112
visFields,
100113
extractedIndexPatterns,

src/plugins/vis_type_timeseries/public/application/lib/fetch_fields.js

+22-16
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,40 @@
1919
import { i18n } from '@kbn/i18n';
2020
import { extractIndexPatterns } from '../../../common/extract_index_patterns';
2121
import { getCoreStart } from '../../services';
22+
import { ROUTES } from '../../../common/constants';
2223

23-
export async function fetchFields(indexPatterns = ['*']) {
24+
export async function fetchFields(indexPatterns = [], signal) {
2425
const patterns = Array.isArray(indexPatterns) ? indexPatterns : [indexPatterns];
2526
try {
2627
const indexFields = await Promise.all(
27-
patterns.map((pattern) => {
28-
return getCoreStart().http.get('/api/metrics/fields', {
28+
patterns.map((pattern) =>
29+
getCoreStart().http.get(ROUTES.FIELDS, {
2930
query: {
3031
index: pattern,
3132
},
32-
});
33-
})
33+
signal,
34+
})
35+
)
3436
);
35-
const fields = patterns.reduce((cumulatedFields, currentPattern, index) => {
36-
return {
37+
38+
return patterns.reduce(
39+
(cumulatedFields, currentPattern, index) => ({
3740
...cumulatedFields,
3841
[currentPattern]: indexFields[index],
39-
};
40-
}, {});
41-
return fields;
42-
} catch (error) {
43-
getCoreStart().notifications.toasts.addDanger({
44-
title: i18n.translate('visTypeTimeseries.fetchFields.loadIndexPatternFieldsErrorMessage', {
45-
defaultMessage: 'Unable to load index_pattern fields',
4642
}),
47-
text: error.message,
48-
});
43+
{}
44+
);
45+
} catch (error) {
46+
if (error.name !== 'AbortError') {
47+
getCoreStart().notifications.toasts.addDanger({
48+
title: i18n.translate('visTypeTimeseries.fetchFields.loadIndexPatternFieldsErrorMessage', {
49+
defaultMessage: 'Unable to load index_pattern fields',
50+
}),
51+
text: error.message,
52+
});
53+
}
4954
}
55+
return [];
5056
}
5157

5258
export async function fetchIndexPatternFields({ params, fields = {} }) {

src/plugins/vis_type_timeseries/server/routes/fields.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import { isBoom } from '@hapi/boom';
2121
import { schema } from '@kbn/config-schema';
2222
import { getFields } from '../lib/get_fields';
2323
import { Framework } from '../plugin';
24+
import { ROUTES } from '../../common/constants';
2425

2526
export const fieldsRoutes = (framework: Framework) => {
2627
framework.router.get(
2728
{
28-
path: '/api/metrics/fields',
29+
path: ROUTES.FIELDS,
2930
validate: {
3031
query: schema.object({ index: schema.string() }),
3132
},

0 commit comments

Comments
 (0)