Skip to content

Commit

Permalink
[Response Ops][Alerting Adding isTrackedAlert function to alerts cl…
Browse files Browse the repository at this point in the history
…ient (#179253)

## Summary

Allows rule executors to determine whether an alert is tracked from a
previous execution.

Co-authored-by: Maryam Saeidi <maryam.saeidi@elastic.co>
  • Loading branch information
ymao1 and maryam-saeidi authored Mar 25, 2024
1 parent 50c6d13 commit 084af44
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
17 changes: 17 additions & 0 deletions x-pack/plugins/alerting/server/alerts_client/alerts_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2702,6 +2702,7 @@ describe('Alerts Client', () => {

expect(keys(publicAlertsClient)).toEqual([
'report',
'isTrackedAlert',
'setAlertData',
'getAlertLimitValue',
'setAlertLimitReached',
Expand Down Expand Up @@ -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);
});
});
});
}
});
5 changes: 5 additions & 0 deletions x-pack/plugins/alerting/server/alerts_client/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ export class AlertsClient<
}
}

public isTrackedAlert(id: string) {
return this.legacyAlertsClient.isTrackedAlert(id);
}

public hasReachedAlertLimit(): boolean {
return this.legacyAlertsClient.hasReachedAlertLimit();
}
Expand Down Expand Up @@ -745,6 +749,7 @@ export class AlertsClient<
WithoutReservedActionGroups<ActionGroupIds, RecoveryActionGroupId>
>
) => this.report(alert),
isTrackedAlert: (id: string) => this.isTrackedAlert(id),
setAlertData: (
alert: UpdateableAlert<AlertData, LegacyState, LegacyContext, RecoveryActionGroupId>
) => this.setAlertData(alert),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,16 @@ describe('Legacy Alerts Client', () => {
'2': new Alert<AlertInstanceContext, AlertInstanceContext>('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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/alerting/server/alerts_client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface IAlertsClient<
alertIds: string[];
maintenanceWindowIds: string[];
} | null>;
isTrackedAlert(id: string): boolean;
getSummarizedAlerts?(params: GetSummarizedAlertsParams): Promise<SummarizedAlerts>;
getAlertsToSerialize(): {
alertsToReturn: Record<string, RawAlertInstance>;
Expand Down Expand Up @@ -152,6 +153,7 @@ export interface PublicAlertsClient<
report(
alert: ReportedAlert<AlertData, State, Context, ActionGroupIds>
): ReportedAlertData<AlertData>;
isTrackedAlert(id: string): boolean;
setAlertData(alert: UpdateableAlert<AlertData, State, Context, ActionGroupIds>): void;
getAlertLimitValue: () => number;
setAlertLimitReached: (reached: boolean) => void;
Expand Down

0 comments on commit 084af44

Please sign in to comment.