-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[One Workflow] Add alertStates backend support for workflow connector #257363
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
102 changes: 102 additions & 0 deletions
102
src/platform/plugins/shared/workflows_management/common/utils/build_alert_event.test.ts
This file contains hidden or 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,102 @@ | ||
| /* | ||
| * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import type { CombinedSummarizedAlerts } from '@kbn/alerting-plugin/server/types'; | ||
| import { buildAlertEvent } from './build_alert_event'; | ||
|
|
||
| const mockRule = { | ||
| id: 'rule-id', | ||
| name: 'test rule', | ||
| tags: ['test-tag'], | ||
| consumer: 'test-consumer', | ||
| producer: 'test-producer', | ||
| ruleTypeId: 'test-rule-type', | ||
| }; | ||
|
|
||
| const makeAlertGroup = (ids: string[]) => ({ | ||
| count: ids.length, | ||
| data: ids.map((id) => ({ _id: id, _index: 'test-index' })), | ||
| alert_count: { active: ids.length, recovered: 0, ignored: 0 }, | ||
| }); | ||
|
|
||
| describe('buildAlertEvent', () => { | ||
| it('should merge new, ongoing, and recovered alerts into a flat array', () => { | ||
| const alerts = { | ||
| all: makeAlertGroup(['a1', 'a2', 'a3']), | ||
| new: makeAlertGroup(['a1']), | ||
| ongoing: makeAlertGroup(['a2']), | ||
| recovered: makeAlertGroup(['a3']), | ||
| } as unknown as CombinedSummarizedAlerts; | ||
|
|
||
| const result = buildAlertEvent({ alerts, rule: mockRule, spaceId: 'default' }); | ||
|
|
||
| expect(result.alerts).toHaveLength(3); | ||
| expect(result.alerts.map((a) => a._id)).toEqual(['a1', 'a2', 'a3']); | ||
| }); | ||
|
|
||
| it('should return only new alerts when ongoing and recovered are empty', () => { | ||
| const alerts = { | ||
| all: makeAlertGroup(['a1']), | ||
| new: makeAlertGroup(['a1']), | ||
| ongoing: makeAlertGroup([]), | ||
| recovered: makeAlertGroup([]), | ||
| } as unknown as CombinedSummarizedAlerts; | ||
|
|
||
| const result = buildAlertEvent({ alerts, rule: mockRule, spaceId: 'default' }); | ||
|
|
||
| expect(result.alerts).toHaveLength(1); | ||
| expect(result.alerts[0]._id).toBe('a1'); | ||
| }); | ||
|
|
||
| it('should handle undefined data gracefully', () => { | ||
| const alerts = { | ||
| all: { count: 0, data: [] }, | ||
| new: { count: 0, data: undefined }, | ||
| ongoing: { count: 0, data: undefined }, | ||
| recovered: { count: 0, data: undefined }, | ||
| } as unknown as CombinedSummarizedAlerts; | ||
|
|
||
| const result = buildAlertEvent({ alerts, rule: mockRule, spaceId: 'default' }); | ||
|
|
||
| expect(result.alerts).toEqual([]); | ||
| }); | ||
|
|
||
| it('should include rule information', () => { | ||
| const alerts = { | ||
| all: makeAlertGroup([]), | ||
| new: makeAlertGroup([]), | ||
| ongoing: makeAlertGroup([]), | ||
| recovered: makeAlertGroup([]), | ||
| } as unknown as CombinedSummarizedAlerts; | ||
|
|
||
| const result = buildAlertEvent({ | ||
| alerts, | ||
| rule: mockRule, | ||
| ruleUrl: 'https://example.com/rule', | ||
| spaceId: 'my-space', | ||
| }); | ||
|
|
||
| expect(result.rule).toEqual(mockRule); | ||
| expect(result.ruleUrl).toBe('https://example.com/rule'); | ||
| expect(result.spaceId).toBe('my-space'); | ||
| }); | ||
|
|
||
| it('should handle missing ruleUrl', () => { | ||
| const alerts = { | ||
| all: makeAlertGroup([]), | ||
| new: makeAlertGroup([]), | ||
| ongoing: makeAlertGroup([]), | ||
| recovered: makeAlertGroup([]), | ||
| } as unknown as CombinedSummarizedAlerts; | ||
|
|
||
| const result = buildAlertEvent({ alerts, rule: mockRule, spaceId: 'default' }); | ||
|
|
||
| expect(result.ruleUrl).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
I'm surprised these are being collapsed into a single collection
alerts, instead of keeping them in different collections. These are passed to the workflow? I guess there's likely enough info in the alert data itself to determine if it's new/ongoing/recovered, but since we already have them partitioned that way, not clear why we are sending them that way.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.
@pmuellr this is the pattern workflows work by as of today (events.alerts) - without a clear distinguish between the state (we didn't have anything else besides the new alerts up until now :)).
We don't want to make people migrate the workflows (and we don't have anything automatic for that now), and as you said, the pattern currently works for people and there's a great way they can get the state of the alert from the alert itself.