Skip to content

Commit

Permalink
[Security Solution] Failed getFieldsForIndexPattern calls can result …
Browse files Browse the repository at this point in the history
…in Exception Flyout getting stuck in loading state (#158371)

## Summary

Original ticket #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](#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>
(cherry picked from commit 1a3cad1)
  • Loading branch information
e40pud committed May 30, 2023
1 parent 0841742 commit ae2a153
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit ae2a153

Please sign in to comment.