-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[One Workflow] Support triggering workflows for recovered and ongoing alerts #256289
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3cd6dea
feat: support triggering workflows for recovered and ongoing alerts
talboren fc44f6a
Merge branch 'main' of https://github.com/elastic/kibana into feature…
talboren dad3e0e
Merge branch 'main' of https://github.com/elastic/kibana into feature…
talboren c37ebb2
fix: update connector type snapshot and fix type errors
talboren 9af5445
Merge branch 'main' of https://github.com/elastic/kibana into feature…
talboren 44f8bcb
fix: resolve alertStates to concrete booleans in error fallback path
talboren 13e1cb6
fix: always return alertStates in error fallback path
talboren 0c83c90
Merge branch 'main' into feature/alert-states-workflow
talboren 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
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
| /* | ||
| * 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'; | ||
| import type { AlertEventRule } from '../types/alert_types'; | ||
|
|
||
| const mockRule: AlertEventRule = { | ||
| id: 'rule-1', | ||
| name: 'Test Rule', | ||
| tags: ['test'], | ||
| consumer: 'alerts', | ||
| producer: 'infrastructure', | ||
| ruleTypeId: 'metrics.alert.threshold', | ||
| }; | ||
|
|
||
| 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 include only new alerts when ongoing and recovered are empty', () => { | ||
| const alerts = { | ||
| new: makeAlertGroup(['alert-new-1', 'alert-new-2']), | ||
| ongoing: makeAlertGroup([]), | ||
| recovered: makeAlertGroup([]), | ||
| all: makeAlertGroup(['alert-new-1', 'alert-new-2']), | ||
| } as unknown as CombinedSummarizedAlerts; | ||
|
|
||
| const result = buildAlertEvent({ alerts, rule: mockRule, spaceId: 'default' }); | ||
|
|
||
| expect(result.alerts).toHaveLength(2); | ||
| expect(result.alerts.map((a) => a._id)).toEqual(['alert-new-1', 'alert-new-2']); | ||
| }); | ||
|
|
||
| it('should merge alerts from all three states', () => { | ||
| const alerts = { | ||
| new: makeAlertGroup(['alert-new-1']), | ||
| ongoing: makeAlertGroup(['alert-ongoing-1']), | ||
| recovered: makeAlertGroup(['alert-recovered-1']), | ||
| all: makeAlertGroup(['alert-new-1', 'alert-ongoing-1', 'alert-recovered-1']), | ||
| } 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([ | ||
| 'alert-new-1', | ||
| 'alert-ongoing-1', | ||
| 'alert-recovered-1', | ||
| ]); | ||
| }); | ||
|
|
||
| it('should handle undefined state data gracefully', () => { | ||
| const alerts = { | ||
| new: { count: 0, data: undefined }, | ||
| ongoing: { count: 0, data: undefined }, | ||
| recovered: { count: 0, data: undefined }, | ||
| all: makeAlertGroup([]), | ||
| } as unknown as CombinedSummarizedAlerts; | ||
|
|
||
| const result = buildAlertEvent({ alerts, rule: mockRule, spaceId: 'default' }); | ||
|
|
||
| expect(result.alerts).toEqual([]); | ||
| }); | ||
|
|
||
| it('should populate rule information correctly', () => { | ||
| const alerts = { | ||
| new: makeAlertGroup(['alert-1']), | ||
| ongoing: makeAlertGroup([]), | ||
| recovered: makeAlertGroup([]), | ||
| all: makeAlertGroup(['alert-1']), | ||
| } as unknown as CombinedSummarizedAlerts; | ||
|
|
||
| const result = buildAlertEvent({ | ||
| alerts, | ||
| rule: mockRule, | ||
| ruleUrl: 'https://kibana/rule/1', | ||
| spaceId: 'my-space', | ||
| }); | ||
|
|
||
| expect(result.rule).toEqual(mockRule); | ||
| expect(result.ruleUrl).toBe('https://kibana/rule/1'); | ||
| expect(result.spaceId).toBe('my-space'); | ||
| }); | ||
| }); |
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.
The index signature (
[key: string]: ...) makesAlertStatesaccept arbitrary keys, which weakens type-safety (e.g., typos like{ recoevered: true }compile) and diverges from the server’s fixed-key shape. Consider removing the index signature and using explicit keys only (optionally paired with atype AlertStateId = 'new' | 'ongoing' | 'recovered'for the checkbox option IDs) to keep public/server contracts aligned and prevent silent misconfiguration.