-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Response Ops][Alerting] Refactor
ExecutionHandler
stage 1 (#186666)
Resolves #186533 ## Summary Stage 1 of `ExecutionHandler` refactor: * Rename `ExecutionHandler` to `ActionScheduler`. * Create schedulers to handle the 3 different action types (`SummaryActionScheduler`, `SystemActionScheduler`, `PerAlertActionScheduler`) * Splits `ExecutionHandler.generateExecutables` function into the appropriate action type class and combine the returned executables from each scheduler class. GH is not recognizing the rename from `ExecutionHandler` to `ActionScheduler` so I've called out the primary difference between the two files (other than the rename) which is to get the executables from each scheduler class instead of from a `generateExecutables` function. Removed the `generateExecutables` fn from the `ActionScheduler` and any associated private helper functions. --------- Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
0077b0e
commit f19af22
Showing
21 changed files
with
3,473 additions
and
1,423 deletions.
There are no files selected for viewing
663 changes: 256 additions & 407 deletions
663
...ver/task_runner/execution_handler.test.ts → ...action_scheduler/action_scheduler.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
605 changes: 605 additions & 0 deletions
605
x-pack/plugins/alerting/server/task_runner/action_scheduler/action_scheduler.ts
Large diffs are not rendered by default.
Oops, something went wrong.
127 changes: 127 additions & 0 deletions
127
x-pack/plugins/alerting/server/task_runner/action_scheduler/get_summarized_alerts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { getSummarizedAlerts } from './get_summarized_alerts'; | ||
import { alertsClientMock } from '../../alerts_client/alerts_client.mock'; | ||
import { mockAAD } from '../fixtures'; | ||
import { ALERT_UUID } from '@kbn/rule-data-utils'; | ||
import { generateAlert } from './test_fixtures'; | ||
import { getErrorSource } from '@kbn/task-manager-plugin/server/task_running'; | ||
|
||
const alertsClient = alertsClientMock.create(); | ||
|
||
describe('getSummarizedAlerts', () => { | ||
const newAlert1 = generateAlert({ id: 1 }); | ||
const newAlert2 = generateAlert({ id: 2 }); | ||
const alerts = { ...newAlert1, ...newAlert2 }; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('should call alertsClient.getSummarizedAlerts with the correct params', async () => { | ||
const summarizedAlerts = { | ||
new: { | ||
count: 2, | ||
data: [ | ||
{ ...mockAAD, [ALERT_UUID]: alerts[1].getUuid() }, | ||
{ ...mockAAD, [ALERT_UUID]: alerts[2].getUuid() }, | ||
], | ||
}, | ||
ongoing: { count: 0, data: [] }, | ||
recovered: { count: 0, data: [] }, | ||
}; | ||
alertsClient.getSummarizedAlerts.mockResolvedValue(summarizedAlerts); | ||
alertsClient.getProcessedAlerts.mockReturnValue(alerts); | ||
const result = await getSummarizedAlerts({ | ||
alertsClient, | ||
queryOptions: { | ||
excludedAlertInstanceIds: [], | ||
executionUuid: '123xyz', | ||
ruleId: '1', | ||
spaceId: 'test1', | ||
}, | ||
}); | ||
|
||
expect(alertsClient.getSummarizedAlerts).toHaveBeenCalledWith({ | ||
excludedAlertInstanceIds: [], | ||
executionUuid: '123xyz', | ||
ruleId: '1', | ||
spaceId: 'test1', | ||
}); | ||
|
||
expect(result).toEqual({ | ||
...summarizedAlerts, | ||
all: summarizedAlerts.new, | ||
}); | ||
}); | ||
|
||
test('should throw error if alertsClient.getSummarizedAlerts throws error', async () => { | ||
alertsClient.getSummarizedAlerts.mockImplementation(() => { | ||
throw new Error('cannot get summarized alerts'); | ||
}); | ||
|
||
try { | ||
await getSummarizedAlerts({ | ||
alertsClient, | ||
queryOptions: { | ||
excludedAlertInstanceIds: [], | ||
executionUuid: '123xyz', | ||
ruleId: '1', | ||
spaceId: 'test1', | ||
}, | ||
}); | ||
} catch (err) { | ||
expect(getErrorSource(err)).toBe('framework'); | ||
expect(err.message).toBe('cannot get summarized alerts'); | ||
} | ||
}); | ||
|
||
test('should remove alert from summarized alerts if it is new and has a maintenance window', async () => { | ||
const newAlertWithMaintenanceWindow = generateAlert({ | ||
id: 1, | ||
maintenanceWindowIds: ['mw-1'], | ||
}); | ||
const alertsWithMaintenanceWindow = { ...newAlertWithMaintenanceWindow, ...newAlert2 }; | ||
|
||
const newAADAlerts = [ | ||
{ ...mockAAD, [ALERT_UUID]: newAlertWithMaintenanceWindow[1].getUuid() }, | ||
{ ...mockAAD, [ALERT_UUID]: alerts[2].getUuid() }, | ||
]; | ||
const summarizedAlerts = { | ||
new: { count: 2, data: newAADAlerts }, | ||
ongoing: { count: 0, data: [] }, | ||
recovered: { count: 0, data: [] }, | ||
}; | ||
alertsClient.getSummarizedAlerts.mockResolvedValue(summarizedAlerts); | ||
alertsClient.getProcessedAlerts.mockReturnValue(alertsWithMaintenanceWindow); | ||
|
||
const result = await getSummarizedAlerts({ | ||
alertsClient, | ||
queryOptions: { | ||
excludedAlertInstanceIds: [], | ||
executionUuid: '123xyz', | ||
ruleId: '1', | ||
spaceId: 'test1', | ||
}, | ||
}); | ||
|
||
expect(alertsClient.getSummarizedAlerts).toHaveBeenCalledWith({ | ||
excludedAlertInstanceIds: [], | ||
executionUuid: '123xyz', | ||
ruleId: '1', | ||
spaceId: 'test1', | ||
}); | ||
|
||
expect(result).toEqual({ | ||
new: { count: 1, data: [newAADAlerts[1]] }, | ||
ongoing: { count: 0, data: [] }, | ||
recovered: { count: 0, data: [] }, | ||
all: { count: 1, data: [newAADAlerts[1]] }, | ||
}); | ||
}); | ||
}); |
78 changes: 78 additions & 0 deletions
78
x-pack/plugins/alerting/server/task_runner/action_scheduler/get_summarized_alerts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { ALERT_UUID } from '@kbn/rule-data-utils'; | ||
import { createTaskRunError, TaskErrorSource } from '@kbn/task-manager-plugin/server'; | ||
import { GetSummarizedAlertsParams, IAlertsClient } from '../../alerts_client/types'; | ||
import { | ||
AlertInstanceContext, | ||
AlertInstanceState, | ||
CombinedSummarizedAlerts, | ||
RuleAlertData, | ||
} from '../../types'; | ||
|
||
interface GetSummarizedAlertsOpts< | ||
State extends AlertInstanceState, | ||
Context extends AlertInstanceContext, | ||
ActionGroupIds extends string, | ||
RecoveryActionGroupId extends string, | ||
AlertData extends RuleAlertData | ||
> { | ||
alertsClient: IAlertsClient<AlertData, State, Context, ActionGroupIds, RecoveryActionGroupId>; | ||
queryOptions: GetSummarizedAlertsParams; | ||
} | ||
|
||
export const getSummarizedAlerts = async < | ||
State extends AlertInstanceState, | ||
Context extends AlertInstanceContext, | ||
ActionGroupIds extends string, | ||
RecoveryActionGroupId extends string, | ||
AlertData extends RuleAlertData | ||
>({ | ||
alertsClient, | ||
queryOptions, | ||
}: GetSummarizedAlertsOpts< | ||
State, | ||
Context, | ||
ActionGroupIds, | ||
RecoveryActionGroupId, | ||
AlertData | ||
>): Promise<CombinedSummarizedAlerts> => { | ||
let alerts; | ||
try { | ||
alerts = await alertsClient.getSummarizedAlerts!(queryOptions); | ||
} catch (e) { | ||
throw createTaskRunError(e, TaskErrorSource.FRAMEWORK); | ||
} | ||
|
||
/** | ||
* We need to remove all new alerts with maintenance windows retrieved from | ||
* getSummarizedAlerts because they might not have maintenance window IDs | ||
* associated with them from maintenance windows with scoped query updated | ||
* yet (the update call uses refresh: false). So we need to rely on the in | ||
* memory alerts to do this. | ||
*/ | ||
const newAlertsInMemory = Object.values(alertsClient.getProcessedAlerts('new') || {}) || []; | ||
|
||
const newAlertsWithMaintenanceWindowIds = newAlertsInMemory.reduce<string[]>((result, alert) => { | ||
if (alert.getMaintenanceWindowIds().length > 0) { | ||
result.push(alert.getUuid()); | ||
} | ||
return result; | ||
}, []); | ||
|
||
const newAlerts = alerts.new.data.filter((alert) => { | ||
return !newAlertsWithMaintenanceWindowIds.includes(alert[ALERT_UUID]); | ||
}); | ||
|
||
const total = newAlerts.length + alerts.ongoing.count + alerts.recovered.count; | ||
return { | ||
...alerts, | ||
new: { count: newAlerts.length, data: newAlerts }, | ||
all: { count: total, data: [...newAlerts, ...alerts.ongoing.data, ...alerts.recovered.data] }, | ||
}; | ||
}; |
10 changes: 10 additions & 0 deletions
10
x-pack/plugins/alerting/server/task_runner/action_scheduler/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { ActionScheduler } from './action_scheduler'; | ||
export type { RunResult } from './action_scheduler'; | ||
export type { RuleUrl } from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { SystemActionScheduler } from './system_action_scheduler'; | ||
export { SummaryActionScheduler } from './summary_action_scheduler'; | ||
export { PerAlertActionScheduler } from './per_alert_action_scheduler'; |
Oops, something went wrong.