Skip to content

Commit de5ca86

Browse files
committed
changes count source
1 parent 0767f8d commit de5ca86

File tree

7 files changed

+75
-48
lines changed

7 files changed

+75
-48
lines changed

x-pack/plugins/siem/public/alerts/components/signals/actions.tsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import {
2323
replaceTemplateFieldFromMatchFilters,
2424
replaceTemplateFieldFromDataProviders,
2525
} from './helpers';
26-
import { displaySuccessToast, displayErrorToast } from '../../../common/components/toasters';
27-
import * as i18n from './translations';
2826

2927
export const getUpdateSignalsQuery = (eventIds: Readonly<string[]>) => {
3028
return {
@@ -58,29 +56,21 @@ export const updateSignalStatusAction = async ({
5856
status,
5957
setEventsLoading,
6058
setEventsDeleted,
61-
dispatchToaster,
59+
onAlertStatusUpdateSuccess,
60+
onAlertStatusUpdateFailure,
6261
}: UpdateSignalStatusActionProps) => {
6362
try {
6463
setEventsLoading({ eventIds: signalIds, isLoading: true });
6564

6665
const queryObject = query ? { query: JSON.parse(query) } : getUpdateSignalsQuery(signalIds);
6766

68-
await updateSignalStatus({ query: queryObject, status });
67+
const response = await updateSignalStatus({ query: queryObject, status });
6968
// TODO: Only delete those that were successfully updated from updatedRules
7069
setEventsDeleted({ eventIds: signalIds, isDeleted: true });
7170

72-
const successTitle =
73-
status === 'closed'
74-
? i18n.CLOSED_ALERT_SUCCESS_TOAST(signalIds.length)
75-
: i18n.OPENED_ALERT_SUCCESS_TOAST(signalIds.length);
76-
77-
displaySuccessToast(successTitle, dispatchToaster);
78-
} catch (e) {
79-
const errorTitle =
80-
status === 'closed'
81-
? i18n.CLOSED_ALERT_FAILED_TOAST(signalIds.length)
82-
: i18n.OPENED_ALERT_FAILED_TOAST(signalIds.length);
83-
displayErrorToast(errorTitle, [e.message], dispatchToaster);
71+
onAlertStatusUpdateSuccess(response.updated, status);
72+
} catch (error) {
73+
onAlertStatusUpdateFailure(status, error);
8474
} finally {
8575
setEventsLoading({ eventIds: signalIds, isLoading: false });
8676
}

x-pack/plugins/siem/public/alerts/components/signals/default_config.test.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
import { mockEcsDataWithSignal } from '../../../common/mock/mock_ecs';
2020
import { sendSignalToTimelineAction, updateSignalStatusAction } from './actions';
2121
import * as i18n from './translations';
22-
import { ActionToaster } from '../../../common/components/toasters';
2322

2423
jest.mock('./actions');
2524

@@ -54,14 +53,17 @@ describe('signals default_config', () => {
5453
let setEventsDeleted: ({ eventIds, isDeleted }: SetEventsDeletedProps) => void;
5554
let createTimeline: CreateTimeline;
5655
let updateTimelineIsLoading: UpdateTimelineLoading;
57-
let dispatchToaster: React.Dispatch<ActionToaster>;
56+
57+
let onAlertStatusUpdateSuccess: (count: number, status: string) => void;
58+
let onAlertStatusUpdateFailure: (status: string, error: Error) => void;
5859

5960
beforeEach(() => {
6061
setEventsLoading = jest.fn();
6162
setEventsDeleted = jest.fn();
6263
createTimeline = jest.fn();
6364
updateTimelineIsLoading = jest.fn();
64-
dispatchToaster = jest.fn();
65+
onAlertStatusUpdateSuccess = jest.fn();
66+
onAlertStatusUpdateFailure = jest.fn();
6567
});
6668

6769
describe('timeline tooltip', () => {
@@ -74,7 +76,8 @@ describe('signals default_config', () => {
7476
createTimeline,
7577
status: 'open',
7678
updateTimelineIsLoading,
77-
dispatchToaster,
79+
onAlertStatusUpdateSuccess,
80+
onAlertStatusUpdateFailure,
7881
});
7982
const timelineAction = signalsActions[0].getAction({
8083
eventId: 'even-id',
@@ -101,7 +104,8 @@ describe('signals default_config', () => {
101104
createTimeline,
102105
status: 'open',
103106
updateTimelineIsLoading,
104-
dispatchToaster,
107+
onAlertStatusUpdateSuccess,
108+
onAlertStatusUpdateFailure,
105109
});
106110

107111
signalOpenAction = signalsActions[1].getAction({
@@ -124,7 +128,7 @@ describe('signals default_config', () => {
124128
status: 'open',
125129
setEventsLoading,
126130
setEventsDeleted,
127-
dispatchToaster,
131+
onAlertStatusUpdateSuccess,
128132
});
129133
});
130134

@@ -157,7 +161,8 @@ describe('signals default_config', () => {
157161
createTimeline,
158162
status: 'closed',
159163
updateTimelineIsLoading,
160-
dispatchToaster,
164+
onAlertStatusUpdateSuccess,
165+
onAlertStatusUpdateFailure,
161166
});
162167

163168
signalCloseAction = signalsActions[1].getAction({
@@ -180,7 +185,7 @@ describe('signals default_config', () => {
180185
status: 'closed',
181186
setEventsLoading,
182187
setEventsDeleted,
183-
dispatchToaster,
188+
onAlertStatusUpdateSuccess,
184189
});
185190
});
186191

x-pack/plugins/siem/public/alerts/components/signals/default_config.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
SetEventsLoadingProps,
3333
UpdateTimelineLoading,
3434
} from './types';
35-
import { ActionToaster } from '../../../common/components/toasters';
3635

3736
export const signalsOpenFilters: Filter[] = [
3837
{
@@ -199,7 +198,8 @@ export const getSignalsActions = ({
199198
createTimeline,
200199
status,
201200
updateTimelineIsLoading,
202-
dispatchToaster,
201+
onAlertStatusUpdateSuccess,
202+
onAlertStatusUpdateFailure,
203203
}: {
204204
apolloClient?: ApolloClient<{}>;
205205
canUserCRUD: boolean;
@@ -209,7 +209,8 @@ export const getSignalsActions = ({
209209
createTimeline: CreateTimeline;
210210
status: 'open' | 'closed';
211211
updateTimelineIsLoading: UpdateTimelineLoading;
212-
dispatchToaster: React.Dispatch<ActionToaster>;
212+
onAlertStatusUpdateSuccess: (count: number, status: string) => void;
213+
onAlertStatusUpdateFailure: (status: string, error: Error) => void;
213214
}): TimelineAction[] => [
214215
{
215216
getAction: ({ ecsData }: TimelineActionProps): JSX.Element => (
@@ -249,7 +250,8 @@ export const getSignalsActions = ({
249250
status,
250251
setEventsLoading,
251252
setEventsDeleted,
252-
dispatchToaster,
253+
onAlertStatusUpdateSuccess,
254+
onAlertStatusUpdateFailure,
253255
})
254256
}
255257
isDisabled={!canUserCRUD || !hasIndexWrite}

x-pack/plugins/siem/public/alerts/components/signals/index.tsx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ import {
4646
UpdateSignalsStatusProps,
4747
} from './types';
4848
import { dispatchUpdateTimeline } from '../../../timelines/components/open_timeline/helpers';
49-
import { useStateToaster } from '../../../common/components/toasters';
49+
import {
50+
useStateToaster,
51+
displaySuccessToast,
52+
displayErrorToast,
53+
} from '../../../common/components/toasters';
5054

5155
export const SIGNALS_PAGE_TIMELINE_ID = 'signals-page';
5256

@@ -148,6 +152,27 @@ export const SignalsTableComponent: React.FC<SignalsTableComponentProps> = ({
148152
[setEventsDeleted, SIGNALS_PAGE_TIMELINE_ID]
149153
);
150154

155+
const onAlertStatusUpdateSuccess = useCallback(
156+
(count: number, status: string) => {
157+
const title =
158+
status === 'closed'
159+
? i18n.CLOSED_ALERT_SUCCESS_TOAST(count)
160+
: i18n.OPENED_ALERT_SUCCESS_TOAST(count);
161+
162+
displaySuccessToast(title, dispatchToaster);
163+
},
164+
[dispatchToaster]
165+
);
166+
167+
const onAlertStatusUpdateFailure = useCallback(
168+
(status: string, error: Error) => {
169+
const title =
170+
status === 'closed' ? i18n.CLOSED_ALERT_FAILED_TOAST : i18n.OPENED_ALERT_FAILED_TOAST;
171+
displayErrorToast(title, [error.message], dispatchToaster);
172+
},
173+
[dispatchToaster]
174+
);
175+
151176
// Catches state change isSelectAllChecked->false upon user selection change to reset utility bar
152177
useEffect(() => {
153178
if (!isSelectAllChecked) {
@@ -191,7 +216,8 @@ export const SignalsTableComponent: React.FC<SignalsTableComponentProps> = ({
191216
status,
192217
setEventsDeleted: setEventsDeletedCallback,
193218
setEventsLoading: setEventsLoadingCallback,
194-
dispatchToaster,
219+
onAlertStatusUpdateSuccess,
220+
onAlertStatusUpdateFailure,
195221
});
196222
refetchQuery();
197223
},
@@ -201,7 +227,8 @@ export const SignalsTableComponent: React.FC<SignalsTableComponentProps> = ({
201227
setEventsDeletedCallback,
202228
setEventsLoadingCallback,
203229
showClearSelectionAction,
204-
dispatchToaster,
230+
onAlertStatusUpdateSuccess,
231+
onAlertStatusUpdateFailure,
205232
]
206233
);
207234

@@ -248,7 +275,8 @@ export const SignalsTableComponent: React.FC<SignalsTableComponentProps> = ({
248275
setEventsDeleted: setEventsDeletedCallback,
249276
status: filterGroup === FILTER_OPEN ? FILTER_CLOSED : FILTER_OPEN,
250277
updateTimelineIsLoading,
251-
dispatchToaster,
278+
onAlertStatusUpdateSuccess,
279+
onAlertStatusUpdateFailure,
252280
}),
253281
[
254282
apolloClient,
@@ -259,7 +287,8 @@ export const SignalsTableComponent: React.FC<SignalsTableComponentProps> = ({
259287
setEventsLoadingCallback,
260288
setEventsDeletedCallback,
261289
updateTimelineIsLoading,
262-
dispatchToaster,
290+
onAlertStatusUpdateSuccess,
291+
onAlertStatusUpdateFailure,
263292
]
264293
);
265294

x-pack/plugins/siem/public/alerts/components/signals/translations.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,16 @@ export const OPENED_ALERT_SUCCESS_TOAST = (totalAlerts: number) =>
116116
'Successfully opened {totalAlerts} {totalAlerts, plural, =1 {alert} other {alerts}}.',
117117
});
118118

119-
export const CLOSED_ALERT_FAILED_TOAST = (totalAlerts: number) =>
120-
i18n.translate('xpack.siem.detectionEngine.signals.closedAlertFailedToastMessage', {
121-
values: { totalAlerts },
122-
defaultMessage:
123-
'Failed to close {totalAlerts} {totalAlerts, plural, =1 {alert} other {alerts}}.',
124-
});
119+
export const CLOSED_ALERT_FAILED_TOAST = i18n.translate(
120+
'xpack.siem.detectionEngine.signals.closedAlertFailedToastMessage',
121+
{
122+
defaultMessage: 'Failed to close alert(s).',
123+
}
124+
);
125125

126-
export const OPENED_ALERT_FAILED_TOAST = (totalAlerts: number) =>
127-
i18n.translate('xpack.siem.detectionEngine.signals.openedAlertFailedToastMessage', {
128-
values: { totalAlerts },
129-
defaultMessage:
130-
'Failed to open {totalAlerts} {totalAlerts, plural, =1 {alert} other {alerts}}.',
131-
});
126+
export const OPENED_ALERT_FAILED_TOAST = i18n.translate(
127+
'xpack.siem.detectionEngine.signals.openedAlertFailedToastMessage',
128+
{
129+
defaultMessage: 'Failed to open alert(s)',
130+
}
131+
);

x-pack/plugins/siem/public/alerts/components/signals/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import ApolloClient from 'apollo-client';
99
import { Ecs } from '../../../graphql/types';
1010
import { TimelineModel } from '../../../timelines/store/timeline/model';
1111
import { inputsModel } from '../../../common/store';
12-
import { ActionToaster } from '../../../common/components/toasters';
1312

1413
export interface SetEventsLoadingProps {
1514
eventIds: string[];
@@ -38,7 +37,8 @@ export interface UpdateSignalStatusActionProps {
3837
status: 'open' | 'closed';
3938
setEventsLoading: ({ eventIds, isLoading }: SetEventsLoadingProps) => void;
4039
setEventsDeleted: ({ eventIds, isDeleted }: SetEventsDeletedProps) => void;
41-
dispatchToaster: React.Dispatch<ActionToaster>;
40+
onAlertStatusUpdateSuccess: (count: number, status: string) => void;
41+
onAlertStatusUpdateFailure: (status: string, error: Error) => void;
4242
}
4343

4444
export type SendSignalsToTimeline = () => void;

x-pack/plugins/siem/public/alerts/containers/detection_engine/signals/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { ReindexResponse } from 'elasticsearch';
78
import {
89
DETECTION_ENGINE_QUERY_SIGNALS_URL,
910
DETECTION_ENGINE_SIGNALS_STATUS_URL,
@@ -54,7 +55,7 @@ export const updateSignalStatus = async ({
5455
query,
5556
status,
5657
signal,
57-
}: UpdateSignalStatusProps): Promise<unknown> =>
58+
}: UpdateSignalStatusProps): Promise<ReindexResponse> =>
5859
KibanaServices.get().http.fetch(DETECTION_ENGINE_SIGNALS_STATUS_URL, {
5960
method: 'POST',
6061
body: JSON.stringify({ status, ...query }),

0 commit comments

Comments
 (0)