Skip to content

Commit

Permalink
[Response Ops][Rule Form V2] Move dependencies from triggers actions …
Browse files Browse the repository at this point in the history
…UI to shared package (#184977)

## Summary

Issue: #179105
Related PR: #180539

Part 2.5/3 PRs of the new rule form. This PR acts as the foundation PR
for the main rule form PR by moving a lot of the dependencies needed by
the rules form to a shared package. So no new features added, just
moving stuff around.

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
JiaweiWu and kibanamachine authored Jun 12, 2024
1 parent 4afcc0a commit a25ccec
Show file tree
Hide file tree
Showing 97 changed files with 1,914 additions and 631 deletions.
34 changes: 34 additions & 0 deletions packages/kbn-alerting-types/alerting_framework_health_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

export enum HealthStatus {
OK = 'ok',
Warning = 'warn',
Error = 'error',
}

export interface AlertsHealth {
decryptionHealth: {
status: HealthStatus;
timestamp: string;
};
executionHealth: {
status: HealthStatus;
timestamp: string;
};
readHealth: {
status: HealthStatus;
timestamp: string;
};
}

export interface AlertingFrameworkHealth {
isSufficientlySecure: boolean;
hasPermanentEncryptionKey: boolean;
alertingFrameworkHealth: AlertsHealth;
}
1 change: 1 addition & 0 deletions packages/kbn-alerting-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export * from './alert_type';
export * from './rule_notify_when_type';
export * from './r_rule_types';
export * from './rule_types';
export * from './alerting_framework_health_types';
export * from './action_variable';
13 changes: 12 additions & 1 deletion packages/kbn-alerting-types/rule_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
* Side Public License, v 1.
*/

import type { SavedObjectAttributes } from '@kbn/core/server';
import type {
SavedObjectAttribute,
SavedObjectAttributes,
SavedObjectsResolveResponse,
} from '@kbn/core/server';
import type { Filter } from '@kbn/es-query';
import type { RuleNotifyWhenType, RRuleParams } from '.';

export type RuleTypeParams = Record<string, unknown>;
export type RuleActionParams = SavedObjectAttributes;
export type RuleActionParam = SavedObjectAttribute;

export const ISO_WEEKDAYS = [1, 2, 3, 4, 5, 6, 7] as const;
export type IsoWeekday = typeof ISO_WEEKDAYS[number];
Expand Down Expand Up @@ -239,3 +244,9 @@ export type SanitizedRule<Params extends RuleTypeParams = never> = Omit<
Rule<Params>,
'apiKey' | 'actions'
> & { actions: SanitizedRuleAction[] };

export type ResolvedSanitizedRule<Params extends RuleTypeParams = never> = SanitizedRule<Params> &
Omit<SavedObjectsResolveResponse, 'saved_object'> & {
outcome: string;
alias_target_id?: string;
};
2 changes: 1 addition & 1 deletion packages/kbn-alerts-ui-shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export type { AlertsSearchBarProps } from './src/alerts_search_bar/types';

export * from './src/alert_fields_table';
export * from './src/alert_filter_controls/types';
export * from './src/common/hooks';
export * from './src/common/types';
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 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 or the Server
* Side Public License, v 1.
*/

import { httpServiceMock } from '@kbn/core/public/mocks';
import { RuleUpdates } from '../../../types';
import { createRule } from './create';
import { RuleTypeParams } from '../../types';
import { createRule } from './create_rule';
import { CreateRuleBody } from './types';

const http = httpServiceMock.createStartContract();

Expand Down Expand Up @@ -62,10 +64,7 @@ describe('createRule', () => {
},
};

const ruleToCreate: Omit<
RuleUpdates,
'createdBy' | 'updatedBy' | 'muteAll' | 'mutedInstanceIds' | 'executionStatus'
> = {
const ruleToCreate: CreateRuleBody<RuleTypeParams> = {
params: {
aggType: 'count',
termSize: 5,
Expand Down Expand Up @@ -106,17 +105,14 @@ describe('createRule', () => {
actionTypeId: '.system-action',
},
],
createdAt: new Date('2021-04-01T21:33:13.247Z'),
updatedAt: new Date('2021-04-01T21:33:13.247Z'),
apiKeyOwner: '',
revision: 0,
notifyWhen: 'onActionGroupChange',
alertDelay: {
active: 10,
},
};
http.post.mockResolvedValueOnce(resolvedValue);

const result = await createRule({ http, rule: ruleToCreate });
const result = await createRule({ http, rule: ruleToCreate as CreateRuleBody });
expect(result).toEqual({
actions: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import type { HttpSetup } from '@kbn/core/public';
import type { AsApiContract } from '@kbn/actions-types';
import type { Rule } from '../../types';
import { CreateRuleBody, transformCreateRuleBody } from '.';
import { BASE_ALERTING_API_PATH } from '../../constants';
import { transformRule } from '../../transformations';

export async function createRule({
http,
rule,
}: {
http: HttpSetup;
rule: CreateRuleBody;
}): Promise<Rule> {
const res = await http.post<AsApiContract<Rule>>(`${BASE_ALERTING_API_PATH}/rule`, {
body: JSON.stringify(transformCreateRuleBody(rule)),
});
return transformRule(res);
}
11 changes: 11 additions & 0 deletions packages/kbn-alerts-ui-shared/src/common/apis/create_rule/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

export * from './types';
export * from './create_rule';
export * from './transform_create_rule_body';
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import { transformCreateRuleBody } from './transform_create_rule_body';
import type { RuleTypeParams } from '../../types';
import type { CreateRuleBody } from './types';

const ruleToCreate: CreateRuleBody<RuleTypeParams> = {
params: {
aggType: 'count',
termSize: 5,
thresholdComparator: '>',
timeWindowSize: 5,
timeWindowUnit: 'm',
groupBy: 'all',
threshold: [1000],
index: ['.kibana'],
timeField: 'alert.executionStatus.lastExecutionDate',
},
consumer: 'alerts',
schedule: { interval: '1m' },
tags: [],
name: 'test',
enabled: true,
throttle: null,
ruleTypeId: '.index-threshold',
actions: [
{
group: 'threshold met',
id: '83d4d860-9316-11eb-a145-93ab369a4461',
params: {
level: 'info',
message: 'test-message',
},
actionTypeId: '.server-log',
frequency: {
notifyWhen: 'onActionGroupChange',
throttle: null,
summary: false,
},
useAlertDataForTemplate: true,
},
{
id: '.test-system-action',
params: {},
actionTypeId: '.system-action',
},
],
notifyWhen: 'onActionGroupChange',
alertDelay: {
active: 10,
},
};

describe('transformCreateRuleBody', () => {
test('should transform create rule body', () => {
expect(transformCreateRuleBody(ruleToCreate)).toEqual({
params: {
aggType: 'count',
termSize: 5,
thresholdComparator: '>',
timeWindowSize: 5,
timeWindowUnit: 'm',
groupBy: 'all',
threshold: [1000],
index: ['.kibana'],
timeField: 'alert.executionStatus.lastExecutionDate',
},
consumer: 'alerts',
schedule: { interval: '1m' },
tags: [],
name: 'test',
enabled: true,
throttle: null,
notifyWhen: 'onActionGroupChange',
rule_type_id: '.index-threshold',
actions: [
{
group: 'threshold met',
id: '83d4d860-9316-11eb-a145-93ab369a4461',
params: {
level: 'info',
message: 'test-message',
},
frequency: {
notify_when: 'onActionGroupChange',
summary: false,
throttle: null,
},
use_alert_data_for_template: true,
},
{ id: '.test-system-action', params: {} },
],

alert_delay: { active: 10 },
});
});
});
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 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 or the Server
* Side Public License, v 1.
*/
import { HttpSetup } from '@kbn/core/public';
import { AsApiContract, RewriteResponseCase } from '@kbn/actions-plugin/common';
import { Rule, RuleUpdates } from '../../../types';
import { BASE_ALERTING_API_PATH } from '../../constants';
import { transformRule } from './common_transformations';

type RuleCreateBody = Omit<
RuleUpdates,
| 'createdBy'
| 'updatedBy'
| 'muteAll'
| 'mutedInstanceIds'
| 'executionStatus'
| 'lastRun'
| 'nextRun'
>;
export const rewriteBodyRequest: RewriteResponseCase<RuleCreateBody> = ({
import { RewriteResponseCase } from '@kbn/actions-types';
import { CreateRuleBody } from './types';

export const transformCreateRuleBody: RewriteResponseCase<CreateRuleBody> = ({
ruleTypeId,
actions,
alertDelay,
Expand Down Expand Up @@ -56,16 +45,3 @@ export const rewriteBodyRequest: RewriteResponseCase<RuleCreateBody> = ({
}),
...(alertDelay ? { alert_delay: alertDelay } : {}),
});

export async function createRule({
http,
rule,
}: {
http: HttpSetup;
rule: RuleCreateBody;
}): Promise<Rule> {
const res = await http.post<AsApiContract<Rule>>(`${BASE_ALERTING_API_PATH}/rule`, {
body: JSON.stringify(rewriteBodyRequest(rule)),
});
return transformRule(res);
}
23 changes: 23 additions & 0 deletions packages/kbn-alerts-ui-shared/src/common/apis/create_rule/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import { Rule, RuleTypeParams } from '../../types';

export interface CreateRuleBody<Params extends RuleTypeParams = RuleTypeParams> {
name: Rule<Params>['name'];
ruleTypeId: Rule<Params>['ruleTypeId'];
enabled: Rule<Params>['enabled'];
consumer: Rule<Params>['consumer'];
tags: Rule<Params>['tags'];
throttle?: Rule<Params>['throttle'];
params: Rule<Params>['params'];
schedule: Rule<Params>['schedule'];
actions: Rule<Params>['actions'];
notifyWhen?: Rule<Params>['notifyWhen'];
alertDelay?: Rule<Params>['alertDelay'];
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 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 or the Server
* Side Public License, v 1.
*/

import { httpServiceMock } from '@kbn/core/public/mocks';
import { alertingFrameworkHealth } from './health';
import { fetchAlertingFrameworkHealth } from '.';

describe('alertingFrameworkHealth', () => {
describe('fetchAlertingFrameworkHealth', () => {
const http = httpServiceMock.createStartContract();
test('should call alertingFrameworkHealth API', async () => {
test('should call fetchAlertingFrameworkHealth API', async () => {
http.get.mockResolvedValueOnce({
is_sufficiently_secure: true,
has_permanent_encryption_key: true,
Expand All @@ -20,7 +21,7 @@ describe('alertingFrameworkHealth', () => {
read_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
},
});
const result = await alertingFrameworkHealth({ http });
const result = await fetchAlertingFrameworkHealth({ http });
expect(result).toEqual({
alertingFrameworkHealth: {
decryptionHealth: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
Expand Down
Loading

0 comments on commit a25ccec

Please sign in to comment.