Skip to content

Commit

Permalink
[Security Solution] Fix paradigm around our container for search stra…
Browse files Browse the repository at this point in the history
…tegy query (#90982) (#92335)

* fix paradigm around our serach strategy query

* simplify logic to act with search strategy

* miss to delete a declaration
  • Loading branch information
XavierM authored Feb 23, 2021
1 parent 41fa5f6 commit 588a97f
Show file tree
Hide file tree
Showing 32 changed files with 752 additions and 858 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const mockUseKibana = {
next(mockData);
/* eslint-disable no-empty */
} catch (e) {}
return {
unsubscribe: jest.fn(),
};
}),
}),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import deepEqual from 'fast-deep-equal';
import { noop } from 'lodash/fp';
import { useCallback, useEffect, useRef, useState } from 'react';
import { Subscription } from 'rxjs';

import { inputsModel } from '../../../../common/store';
import { useKibana } from '../../../../common/lib/kibana';
Expand All @@ -22,7 +23,6 @@ import {
isCompleteResponse,
isErrorResponse,
} from '../../../../../../../../src/plugins/data/common';
import { AbortError } from '../../../../../../../../src/plugins/kibana_utils/common';
import * as i18n from './translations';
import { DocValueFields } from '../../../../../common/search_strategy';

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

const timelineLastEventTimeSearch = useCallback(
(request: TimelineEventsLastEventTimeRequestOptions) => {
let didCancel = false;
const asyncSearch = async () => {
abortCtrl.current = new AbortController();
setLoading(true);

const searchSubscription$ = data.search
searchSubscription$.current = data.search
.search<
TimelineEventsLastEventTimeRequestOptions,
TimelineEventsLastEventTimeStrategyResponse
Expand All @@ -87,46 +87,36 @@ export const useTimelineLastEventTime = ({
.subscribe({
next: (response) => {
if (isCompleteResponse(response)) {
if (!didCancel) {
setLoading(false);
setTimelineLastEventTimeResponse((prevResponse) => ({
...prevResponse,
errorMessage: undefined,
lastSeen: response.lastSeen,
refetch: refetch.current,
}));
}
searchSubscription$.unsubscribe();
setLoading(false);
setTimelineLastEventTimeResponse((prevResponse) => ({
...prevResponse,
errorMessage: undefined,
lastSeen: response.lastSeen,
refetch: refetch.current,
}));
} else if (isErrorResponse(response)) {
if (!didCancel) {
setLoading(false);
}
setLoading(false);
// TODO: Make response error status clearer
notifications.toasts.addWarning(i18n.ERROR_LAST_EVENT_TIME);
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,
}));
}
setLoading(false);
notifications.toasts.addDanger({
title: i18n.FAIL_LAST_EVENT_TIME,
text: msg.message,
});
setTimelineLastEventTimeResponse((prevResponse) => ({
...prevResponse,
errorMessage: msg.message,
}));
},
});
};
searchSubscription$.current.unsubscribe();
abortCtrl.current.abort();
asyncSearch();
refetch.current = asyncSearch;
return () => {
didCancel = true;
abortCtrl.current.abort();
};
},
[data.search, notifications.toasts]
);
Expand All @@ -149,6 +139,10 @@ export const useTimelineLastEventTime = ({

useEffect(() => {
timelineLastEventTimeSearch(TimelineLastEventTimeRequest);
return () => {
searchSubscription$.current.unsubscribe();
abortCtrl.current.abort();
};
}, [TimelineLastEventTimeRequest, timelineLastEventTimeSearch]);

return [loading, timelineLastEventTimeResponse];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import deepEqual from 'fast-deep-equal';
import { getOr, isEmpty, noop } from 'lodash/fp';
import { useCallback, useEffect, useRef, useState } from 'react';
import { Subscription } from 'rxjs';

import { MatrixHistogramQueryProps } from '../../components/matrix_histogram/types';
import { inputsModel } from '../../../common/store';
Expand All @@ -20,7 +21,6 @@ import {
MatrixHistogramData,
} from '../../../../common/search_strategy/security_solution';
import { isErrorResponse, isCompleteResponse } from '../../../../../../../src/plugins/data/common';
import { AbortError } from '../../../../../../../src/plugins/kibana_utils/common';
import { getInspectResponse } from '../../../helpers';
import { InspectResponse } from '../../../types';
import * as i18n from './translations';
Expand Down 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 searchSubscription$ = useRef(new Subscription());
const [loading, setLoading] = useState(false);
const [
matrixHistogramRequest,
Expand Down Expand Up @@ -96,64 +97,53 @@ export const useMatrixHistogram = ({

const hostsSearch = useCallback(
(request: MatrixHistogramRequestOptions) => {
let didCancel = false;
const asyncSearch = async () => {
abortCtrl.current = new AbortController();
setLoading(true);

const searchSubscription$ = data.search
searchSubscription$.current = data.search
.search<MatrixHistogramRequestOptions, MatrixHistogramStrategyResponse>(request, {
strategy: 'securitySolutionSearchStrategy',
abortSignal: abortCtrl.current.signal,
})
.subscribe({
next: (response) => {
if (isCompleteResponse(response)) {
if (!didCancel) {
const histogramBuckets: Buckets = getOr(
bucketEmpty,
'rawResponse.aggregations.eventActionGroup.buckets',
response
);
setLoading(false);
setMatrixHistogramResponse((prevResponse) => ({
...prevResponse,
data: response.matrixHistogramData,
inspect: getInspectResponse(response, prevResponse.inspect),
refetch: refetch.current,
totalCount: response.totalCount,
buckets: histogramBuckets,
}));
}
searchSubscription$.unsubscribe();
const histogramBuckets: Buckets = getOr(
bucketEmpty,
'rawResponse.aggregations.eventActionGroup.buckets',
response
);
setLoading(false);
setMatrixHistogramResponse((prevResponse) => ({
...prevResponse,
data: response.matrixHistogramData,
inspect: getInspectResponse(response, prevResponse.inspect),
refetch: refetch.current,
totalCount: response.totalCount,
buckets: histogramBuckets,
}));
searchSubscription$.current.unsubscribe();
} else if (isErrorResponse(response)) {
if (!didCancel) {
setLoading(false);
}
setLoading(false);
// TODO: Make response error status clearer
notifications.toasts.addWarning(i18n.ERROR_MATRIX_HISTOGRAM);
searchSubscription$.unsubscribe();
searchSubscription$.current.unsubscribe();
}
},
error: (msg) => {
if (!didCancel) {
setLoading(false);
}
if (!(msg instanceof AbortError)) {
notifications.toasts.addError(msg, {
title: errorMessage ?? i18n.FAIL_MATRIX_HISTOGRAM,
});
}
setLoading(false);
notifications.toasts.addError(msg, {
title: errorMessage ?? i18n.FAIL_MATRIX_HISTOGRAM,
});
searchSubscription$.current.unsubscribe();
},
});
};
searchSubscription$.current.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 +186,10 @@ export const useMatrixHistogram = ({
if (!skip) {
hostsSearch(matrixHistogramRequest);
}
return () => {
searchSubscription$.current.unsubscribe();
abortCtrl.current.abort();
};
}, [matrixHistogramRequest, hostsSearch, skip]);

const runMatrixHistogramSearch = useCallback(
Expand Down
Loading

0 comments on commit 588a97f

Please sign in to comment.