Skip to content

Commit 66a5066

Browse files
committed
[Security Solution] Fix paradigm around our container for search strategy query (elastic#90982)
* fix paradigm around our serach strategy query * simplify logic to act with search strategy * miss to delete a declaration
1 parent e5f4360 commit 66a5066

File tree

32 files changed

+752
-858
lines changed

32 files changed

+752
-858
lines changed

x-pack/plugins/security_solution/public/common/containers/events/last_event_time/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const mockUseKibana = {
2626
next(mockData);
2727
/* eslint-disable no-empty */
2828
} catch (e) {}
29+
return {
30+
unsubscribe: jest.fn(),
31+
};
2932
}),
3033
}),
3134
},

x-pack/plugins/security_solution/public/common/containers/events/last_event_time/index.ts

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import deepEqual from 'fast-deep-equal';
99
import { noop } from 'lodash/fp';
1010
import { useCallback, useEffect, useRef, useState } from 'react';
11+
import { Subscription } from 'rxjs';
1112

1213
import { inputsModel } from '../../../../common/store';
1314
import { useKibana } from '../../../../common/lib/kibana';
@@ -22,7 +23,6 @@ import {
2223
isCompleteResponse,
2324
isErrorResponse,
2425
} from '../../../../../../../../src/plugins/data/common';
25-
import { AbortError } from '../../../../../../../../src/plugins/kibana_utils/common';
2626
import * as i18n from './translations';
2727
import { DocValueFields } from '../../../../../common/search_strategy';
2828

@@ -48,6 +48,7 @@ export const useTimelineLastEventTime = ({
4848
const { data, notifications } = useKibana().services;
4949
const refetch = useRef<inputsModel.Refetch>(noop);
5050
const abortCtrl = useRef(new AbortController());
51+
const searchSubscription$ = useRef(new Subscription());
5152
const [loading, setLoading] = useState(false);
5253
const [
5354
TimelineLastEventTimeRequest,
@@ -71,12 +72,11 @@ export const useTimelineLastEventTime = ({
7172

7273
const timelineLastEventTimeSearch = useCallback(
7374
(request: TimelineEventsLastEventTimeRequestOptions) => {
74-
let didCancel = false;
7575
const asyncSearch = async () => {
7676
abortCtrl.current = new AbortController();
7777
setLoading(true);
7878

79-
const searchSubscription$ = data.search
79+
searchSubscription$.current = data.search
8080
.search<
8181
TimelineEventsLastEventTimeRequestOptions,
8282
TimelineEventsLastEventTimeStrategyResponse
@@ -87,46 +87,36 @@ export const useTimelineLastEventTime = ({
8787
.subscribe({
8888
next: (response) => {
8989
if (isCompleteResponse(response)) {
90-
if (!didCancel) {
91-
setLoading(false);
92-
setTimelineLastEventTimeResponse((prevResponse) => ({
93-
...prevResponse,
94-
errorMessage: undefined,
95-
lastSeen: response.lastSeen,
96-
refetch: refetch.current,
97-
}));
98-
}
99-
searchSubscription$.unsubscribe();
90+
setLoading(false);
91+
setTimelineLastEventTimeResponse((prevResponse) => ({
92+
...prevResponse,
93+
errorMessage: undefined,
94+
lastSeen: response.lastSeen,
95+
refetch: refetch.current,
96+
}));
10097
} else if (isErrorResponse(response)) {
101-
if (!didCancel) {
102-
setLoading(false);
103-
}
98+
setLoading(false);
10499
// TODO: Make response error status clearer
105100
notifications.toasts.addWarning(i18n.ERROR_LAST_EVENT_TIME);
106-
searchSubscription$.unsubscribe();
107101
}
108102
},
109103
error: (msg) => {
110-
if (!(msg instanceof AbortError)) {
111-
notifications.toasts.addDanger({
112-
title: i18n.FAIL_LAST_EVENT_TIME,
113-
text: msg.message,
114-
});
115-
setTimelineLastEventTimeResponse((prevResponse) => ({
116-
...prevResponse,
117-
errorMessage: msg.message,
118-
}));
119-
}
104+
setLoading(false);
105+
notifications.toasts.addDanger({
106+
title: i18n.FAIL_LAST_EVENT_TIME,
107+
text: msg.message,
108+
});
109+
setTimelineLastEventTimeResponse((prevResponse) => ({
110+
...prevResponse,
111+
errorMessage: msg.message,
112+
}));
120113
},
121114
});
122115
};
116+
searchSubscription$.current.unsubscribe();
123117
abortCtrl.current.abort();
124118
asyncSearch();
125119
refetch.current = asyncSearch;
126-
return () => {
127-
didCancel = true;
128-
abortCtrl.current.abort();
129-
};
130120
},
131121
[data.search, notifications.toasts]
132122
);
@@ -149,6 +139,10 @@ export const useTimelineLastEventTime = ({
149139

150140
useEffect(() => {
151141
timelineLastEventTimeSearch(TimelineLastEventTimeRequest);
142+
return () => {
143+
searchSubscription$.current.unsubscribe();
144+
abortCtrl.current.abort();
145+
};
152146
}, [TimelineLastEventTimeRequest, timelineLastEventTimeSearch]);
153147

154148
return [loading, timelineLastEventTimeResponse];

x-pack/plugins/security_solution/public/common/containers/matrix_histogram/index.ts

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import deepEqual from 'fast-deep-equal';
99
import { getOr, isEmpty, noop } from 'lodash/fp';
1010
import { useCallback, useEffect, useRef, useState } from 'react';
11+
import { Subscription } from 'rxjs';
1112

1213
import { MatrixHistogramQueryProps } from '../../components/matrix_histogram/types';
1314
import { inputsModel } from '../../../common/store';
@@ -20,7 +21,6 @@ import {
2021
MatrixHistogramData,
2122
} from '../../../../common/search_strategy/security_solution';
2223
import { isErrorResponse, isCompleteResponse } from '../../../../../../../src/plugins/data/common';
23-
import { AbortError } from '../../../../../../../src/plugins/kibana_utils/common';
2424
import { getInspectResponse } from '../../../helpers';
2525
import { InspectResponse } from '../../../types';
2626
import * as i18n from './translations';
@@ -63,6 +63,7 @@ export const useMatrixHistogram = ({
6363
const { data, notifications } = useKibana().services;
6464
const refetch = useRef<inputsModel.Refetch>(noop);
6565
const abortCtrl = useRef(new AbortController());
66+
const searchSubscription$ = useRef(new Subscription());
6667
const [loading, setLoading] = useState(false);
6768
const [
6869
matrixHistogramRequest,
@@ -96,64 +97,53 @@ export const useMatrixHistogram = ({
9697

9798
const hostsSearch = useCallback(
9899
(request: MatrixHistogramRequestOptions) => {
99-
let didCancel = false;
100100
const asyncSearch = async () => {
101101
abortCtrl.current = new AbortController();
102102
setLoading(true);
103103

104-
const searchSubscription$ = data.search
104+
searchSubscription$.current = data.search
105105
.search<MatrixHistogramRequestOptions, MatrixHistogramStrategyResponse>(request, {
106106
strategy: 'securitySolutionSearchStrategy',
107107
abortSignal: abortCtrl.current.signal,
108108
})
109109
.subscribe({
110110
next: (response) => {
111111
if (isCompleteResponse(response)) {
112-
if (!didCancel) {
113-
const histogramBuckets: Buckets = getOr(
114-
bucketEmpty,
115-
'rawResponse.aggregations.eventActionGroup.buckets',
116-
response
117-
);
118-
setLoading(false);
119-
setMatrixHistogramResponse((prevResponse) => ({
120-
...prevResponse,
121-
data: response.matrixHistogramData,
122-
inspect: getInspectResponse(response, prevResponse.inspect),
123-
refetch: refetch.current,
124-
totalCount: response.totalCount,
125-
buckets: histogramBuckets,
126-
}));
127-
}
128-
searchSubscription$.unsubscribe();
112+
const histogramBuckets: Buckets = getOr(
113+
bucketEmpty,
114+
'rawResponse.aggregations.eventActionGroup.buckets',
115+
response
116+
);
117+
setLoading(false);
118+
setMatrixHistogramResponse((prevResponse) => ({
119+
...prevResponse,
120+
data: response.matrixHistogramData,
121+
inspect: getInspectResponse(response, prevResponse.inspect),
122+
refetch: refetch.current,
123+
totalCount: response.totalCount,
124+
buckets: histogramBuckets,
125+
}));
126+
searchSubscription$.current.unsubscribe();
129127
} else if (isErrorResponse(response)) {
130-
if (!didCancel) {
131-
setLoading(false);
132-
}
128+
setLoading(false);
133129
// TODO: Make response error status clearer
134130
notifications.toasts.addWarning(i18n.ERROR_MATRIX_HISTOGRAM);
135-
searchSubscription$.unsubscribe();
131+
searchSubscription$.current.unsubscribe();
136132
}
137133
},
138134
error: (msg) => {
139-
if (!didCancel) {
140-
setLoading(false);
141-
}
142-
if (!(msg instanceof AbortError)) {
143-
notifications.toasts.addError(msg, {
144-
title: errorMessage ?? i18n.FAIL_MATRIX_HISTOGRAM,
145-
});
146-
}
135+
setLoading(false);
136+
notifications.toasts.addError(msg, {
137+
title: errorMessage ?? i18n.FAIL_MATRIX_HISTOGRAM,
138+
});
139+
searchSubscription$.current.unsubscribe();
147140
},
148141
});
149142
};
143+
searchSubscription$.current.unsubscribe();
150144
abortCtrl.current.abort();
151145
asyncSearch();
152146
refetch.current = asyncSearch;
153-
return () => {
154-
didCancel = true;
155-
abortCtrl.current.abort();
156-
};
157147
},
158148
[data.search, errorMessage, notifications.toasts]
159149
);
@@ -196,6 +186,10 @@ export const useMatrixHistogram = ({
196186
if (!skip) {
197187
hostsSearch(matrixHistogramRequest);
198188
}
189+
return () => {
190+
searchSubscription$.current.unsubscribe();
191+
abortCtrl.current.abort();
192+
};
199193
}, [matrixHistogramRequest, hostsSearch, skip]);
200194

201195
const runMatrixHistogramSearch = useCallback(

0 commit comments

Comments
 (0)