[Security Solution] Failed getFieldsForIndexPattern calls can result in Exception Flyout getting stuck in loading state (#158371)

## Summary

Original ticket https://github.com/elastic/kibana/issues/158110

These changes fixes the issue with the exception flyout which can be
stuck in loading state in case `getFieldsForIndexPattern` throws an
exception.

Fixed by putting the `getFieldsForIndexPattern` call in try/catch. We
use this call to fetch extended information about the fields [to show
warning to the user in case there are some index
issues](https://github.com/elastic/kibana/pull/149149). If
`getFieldsForIndexPattern` fails and throws an exception we will
continue using fields without conflicts/unmapped information.

I also, noticed that we do not fetch extended information for the case
where user uses index patterns instead of data views. Fixed this issue
in `useFetchIndex`.

cc @dhurley14 We will need to adjust either of our PRs depending whose
changes will go in first :-)

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Ievgen Sorokopud 2023-05-30 22:23:17 +02:00 committed by GitHub
parent 03d4fe7515
commit 1a3cad1b27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View file

@ -13,3 +13,10 @@ export const ERROR_INDEX_FIELDS_SEARCH = i18n.translate(
defaultMessage: `An error has occurred creating the ad-hoc data view`,
}
);
export const FETCH_FIELDS_WITH_UNMAPPED_DATA_ERROR = i18n.translate(
'xpack.securitySolution.dataView.fetchFields.warning',
{
defaultMessage: 'Failed to fetch detailed fields information',
}
);

View file

@ -8,11 +8,14 @@
import { useEffect, useState, useMemo } from 'react';
import type { DataViewBase } from '@kbn/es-query';
import { useAppToasts } from '../../../common/hooks/use_app_toasts';
import type { Rule } from '../../rule_management/logic/types';
import { useGetInstalledJob } from '../../../common/components/ml/hooks/use_get_jobs';
import { useKibana } from '../../../common/lib/kibana';
import { useFetchIndex } from '../../../common/containers/source';
import * as i18n from '../../../common/containers/source/translations';
export interface ReturnUseFetchExceptionFlyoutData {
isLoading: boolean;
indexPatterns: DataViewBase;
@ -25,6 +28,7 @@ export interface ReturnUseFetchExceptionFlyoutData {
*/
export const useFetchIndexPatterns = (rules: Rule[] | null): ReturnUseFetchExceptionFlyoutData => {
const { data, spaces } = useKibana().services;
const { addWarning } = useAppToasts();
const [dataViewLoading, setDataViewLoading] = useState(false);
const [activeSpaceId, setActiveSpaceId] = useState('');
const isSingleRule = useMemo(() => rules != null && rules.length === 1, [rules]);
@ -97,20 +101,25 @@ export const useFetchIndexPatterns = (rules: Rule[] | null): ReturnUseFetchExcep
if (activeSpaceId !== '' && memoDataViewId) {
setDataViewLoading(true);
const dv = await data.dataViews.get(memoDataViewId);
const fieldsWithUnmappedInfo = await data.dataViews.getFieldsForIndexPattern(dv, {
pattern: '',
includeUnmapped: true,
});
let fieldsWithUnmappedInfo = null;
try {
fieldsWithUnmappedInfo = await data.dataViews.getFieldsForIndexPattern(dv, {
pattern: '',
includeUnmapped: true,
});
} catch (error) {
addWarning(error, { title: i18n.FETCH_FIELDS_WITH_UNMAPPED_DATA_ERROR });
}
setDataViewLoading(false);
setDataViewIndexPatterns({
...dv,
fields: fieldsWithUnmappedInfo,
...(fieldsWithUnmappedInfo ? { fields: fieldsWithUnmappedInfo } : {}),
});
}
};
fetchSingleDataView();
}, [memoDataViewId, data.dataViews, setDataViewIndexPatterns, activeSpaceId]);
}, [memoDataViewId, data.dataViews, setDataViewIndexPatterns, activeSpaceId, addWarning]);
// Determine whether to use index patterns or data views
const indexPatternsToUse = useMemo(