Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const useTimelineLastEventTime = ({
const { data, notifications } = useKibana().services;
const refetch = useRef<inputsModel.Refetch>(noop);
const abortCtrl = useRef(new AbortController());
const didCancel = useRef(false);
const [loading, setLoading] = useState(false);
const [
TimelineLastEventTimeRequest,
Expand All @@ -71,7 +72,7 @@ export const useTimelineLastEventTime = ({

const timelineLastEventTimeSearch = useCallback(
(request: TimelineEventsLastEventTimeRequestOptions) => {
let didCancel = false;
didCancel.current = false;
const asyncSearch = async () => {
abortCtrl.current = new AbortController();
setLoading(true);
Expand All @@ -86,47 +87,46 @@ export const useTimelineLastEventTime = ({
})
.subscribe({
next: (response) => {
if (isCompleteResponse(response)) {
if (!didCancel) {
if (!didCancel.current) {
if (isCompleteResponse(response)) {
setLoading(false);
setTimelineLastEventTimeResponse((prevResponse) => ({
...prevResponse,
errorMessage: undefined,
lastSeen: response.lastSeen,
refetch: refetch.current,
}));
}
searchSubscription$.unsubscribe();
} else if (isErrorResponse(response)) {
if (!didCancel) {
searchSubscription$.unsubscribe();
} else if (isErrorResponse(response)) {
setLoading(false);
// TODO: Make response error status clearer
notifications.toasts.addWarning(i18n.ERROR_LAST_EVENT_TIME);
}
// TODO: Make response error status clearer
notifications.toasts.addWarning(i18n.ERROR_LAST_EVENT_TIME);
} else {
searchSubscription$.unsubscribe();
}
},
error: (msg) => {
if (!(msg instanceof AbortError)) {
notifications.toasts.addDanger({
title: i18n.FAIL_LAST_EVENT_TIME,
text: msg.message,
});
setTimelineLastEventTimeResponse((prevResponse) => ({
...prevResponse,
errorMessage: msg.message,
}));
if (!didCancel.current) {
if (!(msg instanceof AbortError)) {
setLoading(false);
notifications.toasts.addDanger({
title: i18n.FAIL_LAST_EVENT_TIME,
text: msg.message,
});
setTimelineLastEventTimeResponse((prevResponse) => ({
...prevResponse,
errorMessage: msg.message,
}));
}
}
searchSubscription$.unsubscribe();
},
});
};
abortCtrl.current.abort();
asyncSearch();
refetch.current = asyncSearch;
return () => {
didCancel = true;
abortCtrl.current.abort();
};
},
[data.search, notifications.toasts]
);
Expand All @@ -149,6 +149,10 @@ export const useTimelineLastEventTime = ({

useEffect(() => {
timelineLastEventTimeSearch(TimelineLastEventTimeRequest);
return () => {
didCancel.current = true;
abortCtrl.current.abort();
};
}, [TimelineLastEventTimeRequest, timelineLastEventTimeSearch]);

return [loading, timelineLastEventTimeResponse];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const useMatrixHistogram = ({
const { data, notifications } = useKibana().services;
const refetch = useRef<inputsModel.Refetch>(noop);
const abortCtrl = useRef(new AbortController());
const didCancel = useRef(false);
const [loading, setLoading] = useState(false);
const [
matrixHistogramRequest,
Expand Down Expand Up @@ -96,7 +97,7 @@ export const useMatrixHistogram = ({

const hostsSearch = useCallback(
(request: MatrixHistogramRequestOptions) => {
let didCancel = false;
didCancel.current = false;
const asyncSearch = async () => {
abortCtrl.current = new AbortController();
setLoading(true);
Expand All @@ -108,8 +109,8 @@ export const useMatrixHistogram = ({
})
.subscribe({
next: (response) => {
if (isCompleteResponse(response)) {
if (!didCancel) {
if (!didCancel.current) {
if (isCompleteResponse(response)) {
const histogramBuckets: Buckets = getOr(
bucketEmpty,
'rawResponse.aggregations.eventActionGroup.buckets',
Expand All @@ -124,36 +125,33 @@ export const useMatrixHistogram = ({
totalCount: response.totalCount,
buckets: histogramBuckets,
}));
}
searchSubscription$.unsubscribe();
} else if (isErrorResponse(response)) {
if (!didCancel) {
searchSubscription$.unsubscribe();
} else if (isErrorResponse(response)) {
setLoading(false);
// TODO: Make response error status clearer
notifications.toasts.addWarning(i18n.ERROR_MATRIX_HISTOGRAM);
searchSubscription$.unsubscribe();
}
// TODO: Make response error status clearer
notifications.toasts.addWarning(i18n.ERROR_MATRIX_HISTOGRAM);
} else {
searchSubscription$.unsubscribe();
}
},
error: (msg) => {
if (!didCancel) {
setLoading(false);
}
if (!(msg instanceof AbortError)) {
notifications.toasts.addError(msg, {
title: errorMessage ?? i18n.FAIL_MATRIX_HISTOGRAM,
});
if (!didCancel.current) {
if (!(msg instanceof AbortError)) {
setLoading(false);
notifications.toasts.addError(msg, {
title: errorMessage ?? i18n.FAIL_MATRIX_HISTOGRAM,
});
}
}
searchSubscription$.unsubscribe();
},
});
};
abortCtrl.current.abort();
asyncSearch();
refetch.current = asyncSearch;
return () => {
didCancel = true;
abortCtrl.current.abort();
};
},
[data.search, errorMessage, notifications.toasts]
);
Expand Down Expand Up @@ -196,6 +194,10 @@ export const useMatrixHistogram = ({
if (!skip) {
hostsSearch(matrixHistogramRequest);
}
return () => {
didCancel.current = true;
abortCtrl.current.abort();
};
}, [matrixHistogramRequest, hostsSearch, skip]);

const runMatrixHistogramSearch = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
BrowserField,
BrowserFields,
} from '../../../../common/search_strategy/index_fields';
import { isErrorResponse, isCompleteResponse } from '../../../../../../../src/plugins/data/common';
import { AbortError } from '../../../../../../../src/plugins/kibana_utils/common';
import { useDeepEqualSelector } from '../../../common/hooks/use_selector';
import * as i18n from './translations';
Expand Down Expand Up @@ -126,6 +127,7 @@ export const useFetchIndex = (
): [boolean, FetchIndexReturn] => {
const { data, notifications } = useKibana().services;
const abortCtrl = useRef(new AbortController());
const didCancel = useRef(false);
const previousIndexesName = useRef<string[]>([]);
const [isLoading, setLoading] = useState(false);

Expand All @@ -139,7 +141,7 @@ export const useFetchIndex = (

const indexFieldsSearch = useCallback(
(iNames) => {
let didCancel = false;
didCancel.current = false;
const asyncSearch = async () => {
abortCtrl.current = new AbortController();
setLoading(true);
Expand All @@ -153,8 +155,8 @@ export const useFetchIndex = (
)
.subscribe({
next: (response) => {
if (!response.isPartial && !response.isRunning) {
if (!didCancel) {
if (!didCancel.current) {
if (isCompleteResponse(response)) {
const stringifyIndices = response.indicesExist.sort().join();
previousIndexesName.current = response.indicesExist;
setLoading(false);
Expand All @@ -165,34 +167,33 @@ export const useFetchIndex = (
indexExists: response.indicesExist.length > 0,
indexPatterns: getIndexFields(stringifyIndices, response.indexFields),
});

searchSubscription$.unsubscribe();
} else if (isErrorResponse(response)) {
setLoading(false);
notifications.toasts.addWarning(i18n.ERROR_BEAT_FIELDS);
searchSubscription$.unsubscribe();
}
searchSubscription$.unsubscribe();
} else if (!didCancel && response.isPartial && !response.isRunning) {
setLoading(false);
notifications.toasts.addWarning(i18n.ERROR_BEAT_FIELDS);
} else {
searchSubscription$.unsubscribe();
}
},
error: (msg) => {
if (!didCancel) {
setLoading(false);
}

if (!(msg instanceof AbortError)) {
notifications.toasts.addDanger({
text: msg.message,
title: i18n.FAIL_BEAT_FIELDS,
});
if (!didCancel.current) {
if (!(msg instanceof AbortError)) {
setLoading(false);
notifications.toasts.addDanger({
text: msg.message,
title: i18n.FAIL_BEAT_FIELDS,
});
}
}
searchSubscription$.unsubscribe();
},
});
};
abortCtrl.current.abort();
asyncSearch();
return () => {
didCancel = true;
abortCtrl.current.abort();
};
},
[data.search, notifications.toasts, onlyCheckIfIndicesExist]
);
Expand All @@ -201,6 +202,10 @@ export const useFetchIndex = (
if (!isEmpty(indexNames) && !isEqual(previousIndexesName.current, indexNames)) {
indexFieldsSearch(indexNames);
}
return () => {
didCancel.current = true;
abortCtrl.current.abort();
};
}, [indexNames, indexFieldsSearch, previousIndexesName]);

return [isLoading, state];
Expand All @@ -209,6 +214,7 @@ export const useFetchIndex = (
export const useIndexFields = (sourcererScopeName: SourcererScopeName) => {
const { data, notifications } = useKibana().services;
const abortCtrl = useRef(new AbortController());
const didCancel = useRef(false);
const dispatch = useDispatch();
const indexNamesSelectedSelector = useMemo(
() => sourcererSelectors.getIndexNamesSelectedSelector(),
Expand All @@ -228,7 +234,7 @@ export const useIndexFields = (sourcererScopeName: SourcererScopeName) => {

const indexFieldsSearch = useCallback(
(indicesName) => {
let didCancel = false;
didCancel.current = false;
const asyncSearch = async () => {
abortCtrl.current = new AbortController();
setLoading(true);
Expand All @@ -242,8 +248,8 @@ export const useIndexFields = (sourcererScopeName: SourcererScopeName) => {
)
.subscribe({
next: (response) => {
if (!response.isPartial && !response.isRunning) {
if (!didCancel) {
if (!didCancel.current) {
if (isCompleteResponse(response)) {
const stringifyIndices = response.indicesExist.sort().join();
dispatch(
sourcererActions.setSource({
Expand All @@ -259,35 +265,32 @@ export const useIndexFields = (sourcererScopeName: SourcererScopeName) => {
},
})
);
searchSubscription$.unsubscribe();
} else if (isErrorResponse(response)) {
setLoading(false);
notifications.toasts.addWarning(i18n.ERROR_BEAT_FIELDS);
searchSubscription$.unsubscribe();
}
searchSubscription$.unsubscribe();
} else if (!didCancel && response.isPartial && !response.isRunning) {
// TODO: Make response error status clearer
setLoading(false);
notifications.toasts.addWarning(i18n.ERROR_BEAT_FIELDS);
} else {
searchSubscription$.unsubscribe();
}
},
error: (msg) => {
if (!didCancel) {
setLoading(false);
}

if (!(msg instanceof AbortError)) {
notifications.toasts.addDanger({
text: msg.message,
title: i18n.FAIL_BEAT_FIELDS,
});
if (!didCancel.current) {
if (!(msg instanceof AbortError)) {
setLoading(false);
notifications.toasts.addDanger({
text: msg.message,
title: i18n.FAIL_BEAT_FIELDS,
});
}
}
searchSubscription$.unsubscribe();
},
});
};
abortCtrl.current.abort();
asyncSearch();
return () => {
didCancel = true;
abortCtrl.current.abort();
};
},
[data.search, dispatch, notifications.toasts, setLoading, sourcererScopeName]
);
Expand All @@ -296,5 +299,9 @@ export const useIndexFields = (sourcererScopeName: SourcererScopeName) => {
if (!isEmpty(indexNames) && previousIndexNames !== indexNames.sort().join()) {
indexFieldsSearch(indexNames);
}
return () => {
didCancel.current = true;
abortCtrl.current.abort();
};
}, [indexNames, indexFieldsSearch, previousIndexNames]);
};
Loading