diff --git a/x-pack/plugins/alerting/server/alerts_client/alerts_client.test.ts b/x-pack/plugins/alerting/server/alerts_client/alerts_client.test.ts index ac2e45839f62e..60e0889d9aec6 100644 --- a/x-pack/plugins/alerting/server/alerts_client/alerts_client.test.ts +++ b/x-pack/plugins/alerting/server/alerts_client/alerts_client.test.ts @@ -2702,6 +2702,7 @@ describe('Alerts Client', () => { expect(keys(publicAlertsClient)).toEqual([ 'report', + 'isTrackedAlert', 'setAlertData', 'getAlertLimitValue', 'setAlertLimitReached', @@ -2781,6 +2782,22 @@ describe('Alerts Client', () => { expect(recoveredAlert.hit).toBeUndefined(); }); }); + + describe('isTrackedAlert()', () => { + test('should return true if alert was active in a previous execution, false otherwise', async () => { + const alertsClient = new AlertsClient<{}, {}, {}, 'default', 'recovered'>( + alertsClientParams + ); + + await alertsClient.initializeExecution({ + ...defaultExecutionOpts, + activeAlertsFromState: { '1': trackedAlert1Raw, '2': trackedAlert2Raw }, + }); + expect(alertsClient.isTrackedAlert('1')).toBe(true); + expect(alertsClient.isTrackedAlert('2')).toBe(true); + expect(alertsClient.isTrackedAlert('3')).toBe(false); + }); + }); }); } }); diff --git a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts index 5c2e9ed03569b..4fab0825354b7 100644 --- a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts +++ b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts @@ -277,6 +277,10 @@ export class AlertsClient< } } + public isTrackedAlert(id: string) { + return this.legacyAlertsClient.isTrackedAlert(id); + } + public hasReachedAlertLimit(): boolean { return this.legacyAlertsClient.hasReachedAlertLimit(); } @@ -745,6 +749,7 @@ export class AlertsClient< WithoutReservedActionGroups > ) => this.report(alert), + isTrackedAlert: (id: string) => this.isTrackedAlert(id), setAlertData: ( alert: UpdateableAlert ) => this.setAlertData(alert), diff --git a/x-pack/plugins/alerting/server/alerts_client/legacy_alerts_client.test.ts b/x-pack/plugins/alerting/server/alerts_client/legacy_alerts_client.test.ts index d01060820ae52..0aa0d59d6db89 100644 --- a/x-pack/plugins/alerting/server/alerts_client/legacy_alerts_client.test.ts +++ b/x-pack/plugins/alerting/server/alerts_client/legacy_alerts_client.test.ts @@ -307,4 +307,16 @@ describe('Legacy Alerts Client', () => { '2': new Alert('2', testAlert2), }); }); + + test('isTrackedAlert() should return true if alert was active in a previous execution, false otherwise', async () => { + const alertsClient = new LegacyAlertsClient({ + logger, + ruleType, + }); + + await alertsClient.initializeExecution(defaultExecutionOpts); + expect(alertsClient.isTrackedAlert('1')).toBe(true); + expect(alertsClient.isTrackedAlert('2')).toBe(true); + expect(alertsClient.isTrackedAlert('3')).toBe(false); + }); }); diff --git a/x-pack/plugins/alerting/server/alerts_client/legacy_alerts_client.ts b/x-pack/plugins/alerting/server/alerts_client/legacy_alerts_client.ts index ecbd49c0d248f..5e7bc30cb26b7 100644 --- a/x-pack/plugins/alerting/server/alerts_client/legacy_alerts_client.ts +++ b/x-pack/plugins/alerting/server/alerts_client/legacy_alerts_client.ts @@ -137,6 +137,10 @@ export class LegacyAlertsClient< return this.alertFactory?.get(id); } + public isTrackedAlert(id: string) { + return !!this.trackedAlerts.active[id]; + } + public processAlerts({ notifyOnActionGroupChange, flappingSettings, diff --git a/x-pack/plugins/alerting/server/alerts_client/types.ts b/x-pack/plugins/alerting/server/alerts_client/types.ts index 763b293363a26..7f4f9ddf4d991 100644 --- a/x-pack/plugins/alerting/server/alerts_client/types.ts +++ b/x-pack/plugins/alerting/server/alerts_client/types.ts @@ -84,6 +84,7 @@ export interface IAlertsClient< alertIds: string[]; maintenanceWindowIds: string[]; } | null>; + isTrackedAlert(id: string): boolean; getSummarizedAlerts?(params: GetSummarizedAlertsParams): Promise; getAlertsToSerialize(): { alertsToReturn: Record; @@ -152,6 +153,7 @@ export interface PublicAlertsClient< report( alert: ReportedAlert ): ReportedAlertData; + isTrackedAlert(id: string): boolean; setAlertData(alert: UpdateableAlert): void; getAlertLimitValue: () => number; setAlertLimitReached: (reached: boolean) => void;