Skip to content
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 Adding isTrackedAlert function to alerts client #179253

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});
}
});
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