-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Response Ops][Alerting] Refactor ExecutionHandler
stage 1
#186666
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
110d9e2
Refactoring generateExecutables fn in ExecutionHandler
ymao1 1c24496
Merge branch 'main' into refactor-execution-handler-stage-1
elasticmachine 86a9024
PR feedback
ymao1 90850ad
Merge branch 'main' of github.com:elastic/kibana into refactor-execut…
ymao1 69a673d
Merge branch 'main' of github.com:elastic/kibana into refactor-execut…
ymao1 4688cbb
Merge branch 'main' into refactor-execution-handler-stage-1
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was a private function in
ExecutionHandler
class. Moved to a reusable helper function