Skip to content

Commit

Permalink
Encapsulating rule information into RuleConfig type
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed May 12, 2021
1 parent 7c98191 commit 3395506
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 107 deletions.
16 changes: 16 additions & 0 deletions x-pack/plugins/alerting/common/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ export interface Alert<Params extends AlertTypeParams = never> {

export type SanitizedAlert<Params extends AlertTypeParams = never> = Omit<Alert<Params>, 'apiKey'>;

export type SanitizedRuleConfig = Pick<
SanitizedAlert,
| 'name'
| 'tags'
| 'consumer'
| 'enabled'
| 'schedule'
| 'actions'
| 'createdBy'
| 'updatedBy'
| 'createdAt'
| 'updatedAt'
| 'throttle'
| 'notifyWhen'
>;

export enum HealthStatus {
OK = 'ok',
Warning = 'warn',
Expand Down
21 changes: 13 additions & 8 deletions x-pack/plugins/alerting/server/task_runner/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,23 @@ describe('Task Runner', () => {
expect(call.tags).toEqual(['alert-', '-tags']);
expect(call.createdBy).toBe('alert-creator');
expect(call.updatedBy).toBe('alert-updater');
expect(call.consumer).toBe('bar');
expect(call.schedule).toMatchInlineSnapshot(`
expect(call.rule).not.toBe(null);
expect(call.rule.name).toBe('alert-name');
expect(call.rule.tags).toEqual(['alert-', '-tags']);
expect(call.rule.consumer).toBe('bar');
expect(call.rule.enabled).toBe(true);
expect(call.rule.schedule).toMatchInlineSnapshot(`
Object {
"interval": "10s",
}
`);
expect(call.createdAt).toBe(mockDate);
expect(call.updatedAt).toBe(mockDate);
expect(call.notifyWhen).toBe('onActiveAlert');
expect(call.throttle).toBe(null);
expect(call.enabled).toBe(true);
expect(call.actions).toMatchInlineSnapshot(`
expect(call.rule.createdBy).toBe('alert-creator');
expect(call.rule.updatedBy).toBe('alert-updater');
expect(call.rule.createdAt).toBe(mockDate);
expect(call.rule.updatedAt).toBe(mockDate);
expect(call.rule.notifyWhen).toBe('onActiveAlert');
expect(call.rule.throttle).toBe(null);
expect(call.rule.actions).toMatchInlineSnapshot(`
Array [
Object {
"actionTypeId": "action",
Expand Down
22 changes: 14 additions & 8 deletions x-pack/plugins/alerting/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,20 @@ export class TaskRunner<
tags,
createdBy,
updatedBy,
consumer,
schedule,
createdAt,
updatedAt,
throttle,
notifyWhen,
enabled,
actions,
rule: {
name,
tags,
consumer,
enabled,
schedule,
actions,
createdBy,
updatedBy,
createdAt,
updatedAt,
throttle,
notifyWhen,
},
});
} catch (err) {
event.message = `alert execution failure: ${alertLabel}`;
Expand Down
12 changes: 2 additions & 10 deletions x-pack/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import {
AlertNotifyWhenType,
WithoutReservedActionGroups,
ActionVariable,
IntervalSchedule,
AlertAction,
SanitizedRuleConfig,
} from '../common';
import { LicenseType } from '../../licensing/server';

Expand Down Expand Up @@ -91,20 +90,13 @@ export interface AlertExecutorOptions<
services: AlertServices<InstanceState, InstanceContext, ActionGroupIds>;
params: Params;
state: State;
rule: SanitizedRuleConfig;
spaceId: string;
namespace?: string;
consumer: string;
name: string;
tags: string[];
createdBy: string | null;
updatedBy: string | null;
createdAt: Date;
updatedAt: Date;
throttle: string | null;
notifyWhen: AlertNotifyWhenType | null;
schedule: IntervalSchedule;
enabled: boolean;
actions: AlertAction[];
}

export type ExecutorType<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@ const mockOptions = {
tags: [],
createdBy: null,
updatedBy: null,
createdAt: new Date(),
updatedAt: new Date(),
consumer: '',
throttle: null,
notifyWhen: null,
schedule: {
interval: '1h',
rule: {
name: '',
tags: [],
consumer: '',
enabled: true,
schedule: {
interval: '1h',
},
actions: [],
createdBy: null,
updatedBy: null,
createdAt: new Date(),
updatedAt: new Date(),
throttle: null,
notifyWhen: null,
},
enabled: true,
actions: [],
};

describe('The metric threshold alert type', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,22 @@ describe('rules_notification_alert_type', () => {
previousStartedAt: new Date('2019-12-13T16:40:33.400Z'),
createdBy: 'elastic',
updatedBy: 'elastic',
createdAt: new Date('2019-12-14T16:40:33.400Z'),
updatedAt: new Date('2019-12-14T16:40:33.400Z'),
consumer: 'foo',
throttle: null,
notifyWhen: null,
schedule: {
interval: '1h',
rule: {
name: 'name',
tags: [],
consumer: 'foo',
enabled: true,
schedule: {
interval: '1h',
},
actions: [],
createdBy: 'elastic',
updatedBy: 'elastic',
createdAt: new Date('2019-12-14T16:40:33.400Z'),
updatedAt: new Date('2019-12-14T16:40:33.400Z'),
throttle: null,
notifyWhen: null,
},
enabled: true,
actions: [],
};

alert = rulesNotificationAlertType({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,22 @@ const getPayload = (
previousStartedAt: new Date('2019-12-13T16:40:33.400Z'),
createdBy: 'elastic',
updatedBy: 'elastic',
createdAt: new Date('2019-12-13T16:50:33.400Z'),
updatedAt: new Date('2019-12-13T16:50:33.400Z'),
consumer: 'foo',
throttle: null,
notifyWhen: null,
schedule: {
interval: '1h',
rule: {
name: ruleAlert.name,
tags: ruleAlert.tags,
consumer: 'foo',
enabled: true,
schedule: {
interval: '1h',
},
actions: [],
createdBy: 'elastic',
updatedBy: 'elastic',
createdAt: new Date('2019-12-13T16:50:33.400Z'),
updatedAt: new Date('2019-12-13T16:50:33.400Z'),
throttle: null,
notifyWhen: null,
},
enabled: true,
actions: [],
});

describe('signal_rule_alert_type', () => {
Expand Down
Loading

0 comments on commit 3395506

Please sign in to comment.