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

Added UI support for the default action group for Alert Type Model #57603

Merged
Show file tree
Hide file tree
Changes from 8 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 @@ -54,6 +54,7 @@ export const getLicenseExpiration = (
}),
},
],
defaultActionGroup: 'default',
async executor({
services,
params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const signalRulesAlertType = ({
}),
},
],
defaultActionGroup: 'default',
validate: {
params: schema.object({
description: schema.string(),
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/alerting/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
export * from './alert';
export * from './alert_instance';
export * from './alert_task_instance';

export interface ActionGroup {
id: string;
name: string;
}
15 changes: 14 additions & 1 deletion x-pack/plugins/alerting/server/alert_type_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('has()', () => {
id: 'foo',
name: 'Foo',
actionGroups: [],
defaultActionGroup: '',
executor: jest.fn(),
});
expect(registry.has('foo')).toEqual(true);
Expand All @@ -40,6 +41,7 @@ describe('register()', () => {
id: 'test',
name: 'Test',
actionGroups: [],
defaultActionGroup: '',
executor: jest.fn(),
};
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand All @@ -65,13 +67,15 @@ describe('register()', () => {
id: 'test',
name: 'Test',
actionGroups: [],
defaultActionGroup: '',
executor: jest.fn(),
});
expect(() =>
registry.register({
id: 'test',
name: 'Test',
actionGroups: [],
defaultActionGroup: '',
executor: jest.fn(),
})
).toThrowErrorMatchingInlineSnapshot(`"Alert type \\"test\\" is already registered."`);
Expand All @@ -85,12 +89,19 @@ describe('get()', () => {
id: 'test',
name: 'Test',
actionGroups: [],
defaultActionGroup: '',
executor: jest.fn(),
});
const alertType = registry.get('test');
expect(alertType).toMatchInlineSnapshot(`
Object {
"actionGroups": Array [],
"actionGroups": Array [
Object {
"id": "default",
"name": "Default",
},
],
"defaultActionGroup": "default",
"executor": [MockFunction],
"id": "test",
"name": "Test",
Expand Down Expand Up @@ -124,6 +135,7 @@ describe('list()', () => {
name: 'Test Action Group',
},
],
defaultActionGroup: 'testActionGroup',
executor: jest.fn(),
});
const result = registry.list();
Expand All @@ -136,6 +148,7 @@ describe('list()', () => {
"name": "Test Action Group",
},
],
"defaultActionGroup": "testActionGroup",
"id": "test",
"name": "Test",
},
Expand Down
12 changes: 11 additions & 1 deletion x-pack/plugins/alerting/server/alert_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ export class AlertTypeRegistry {
})
);
}
this.alertTypes.set(alertType.id, alertType);
// set default value for actionGroups if it is empty
if (alertType.actionGroups.length === 0) {
YulNaumenko marked this conversation as resolved.
Show resolved Hide resolved
this.alertTypes.set(alertType.id, {
...alertType,
actionGroups: [{ id: 'default', name: 'Default' }],
YulNaumenko marked this conversation as resolved.
Show resolved Hide resolved
defaultActionGroup: 'default',
});
} else {
this.alertTypes.set(alertType.id, alertType);
}
this.taskManager.registerTaskDefinitions({
[`alerting:${alertType.id}`]: {
title: alertType.name,
Expand Down Expand Up @@ -70,6 +79,7 @@ export class AlertTypeRegistry {
id: alertTypeId,
name: alertType.name,
actionGroups: alertType.actionGroups,
defaultActionGroup: alertType.defaultActionGroup,
}));
}
}
5 changes: 5 additions & 0 deletions x-pack/plugins/alerting/server/alerts_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('create()', () => {
id: '123',
name: 'Test',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroup: 'default',
async executor() {},
});
});
Expand Down Expand Up @@ -523,6 +524,7 @@ describe('create()', () => {
id: '123',
name: 'Test',
actionGroups: [],
defaultActionGroup: '',
validate: {
params: schema.object({
param1: schema.string(),
Expand Down Expand Up @@ -1885,6 +1887,7 @@ describe('update()', () => {
id: '123',
name: 'Test',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroup: 'default',
async executor() {},
});
});
Expand Down Expand Up @@ -2415,6 +2418,7 @@ describe('update()', () => {
id: '123',
name: 'Test',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroup: 'default',
validate: {
params: schema.object({
param1: schema.string(),
Expand Down Expand Up @@ -2647,6 +2651,7 @@ describe('update()', () => {
id: '123',
name: 'Test',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroup: 'default',
async executor() {},
});
savedObjectsClient.bulkGet.mockResolvedValueOnce({
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type AlertsClient = PublicMethodsOf<AlertsClientClass>;

export {
AlertType,
ActionGroup,
AlertingPlugin,
AlertExecutorOptions,
AlertActionParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test('should return passed in params when validation not defined', () => {
id: 'my-alert-type',
name: 'My description',
actionGroups: [],
defaultActionGroup: '',
async executor() {},
},
{
Expand All @@ -28,6 +29,7 @@ test('should validate and apply defaults when params is valid', () => {
id: 'my-alert-type',
name: 'My description',
actionGroups: [],
defaultActionGroup: '',
validate: {
params: schema.object({
param1: schema.string(),
Expand All @@ -51,6 +53,7 @@ test('should validate and throw error when params is invalid', () => {
id: 'my-alert-type',
name: 'My description',
actionGroups: [],
defaultActionGroup: '',
validate: {
params: schema.object({
param1: schema.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const alertType: AlertType = {
{ id: 'default', name: 'Default' },
{ id: 'other-group', name: 'Other Group' },
],
defaultActionGroup: 'default',
executor: jest.fn(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const alertType = {
id: 'test',
name: 'My test alert',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroup: 'default',
executor: jest.fn(),
};
let fakeTimer: sinon.SinonFakeTimers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const alertType = {
id: 'test',
name: 'My test alert',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroup: 'default',
executor: jest.fn(),
};
let fakeTimer: sinon.SinonFakeTimers;
Expand Down
8 changes: 2 additions & 6 deletions x-pack/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AlertInstance } from './alert_instance';
import { AlertTypeRegistry as OrigAlertTypeRegistry } from './alert_type_registry';
import { PluginSetupContract, PluginStartContract } from './plugin';
import { SavedObjectAttributes, SavedObjectsClientContract } from '../../../../src/core/server';
import { Alert, AlertActionParams } from '../common';
import { Alert, AlertActionParams, ActionGroup } from '../common';
import { AlertsClient } from './alerts_client';
export * from '../common';

Expand Down Expand Up @@ -52,18 +52,14 @@ export interface AlertExecutorOptions {
updatedBy: string | null;
}

export interface ActionGroup {
id: string;
name: string;
}

export interface AlertType {
id: string;
name: string;
validate?: {
params?: { validate: (object: any) => any };
};
actionGroups: ActionGroup[];
defaultActionGroup: ActionGroup['id'];
executor: ({ services, params, state }: AlertExecutorOptions) => Promise<State | void>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('loadAlertTypes', () => {
name: 'Test',
actionVariables: ['var1'],
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroup: 'default',
},
];
http.get.mockResolvedValueOnce(resolvedValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe('alert_form', () => {
alertTypeRegistry.has.mockReturnValue(true);
actionTypeRegistry.list.mockReturnValue([actionType]);
actionTypeRegistry.has.mockReturnValue(true);
actionTypeRegistry.get.mockReturnValue(actionType);

const initialAlert = ({
name: 'test',
Expand Down Expand Up @@ -221,6 +222,10 @@ describe('alert_form', () => {
'[data-test-subj="my-action-type-ActionTypeSelectOption"]'
);
expect(actionTypeSelectOptions.exists()).toBeTruthy();

actionTypeSelectOptions.first().simulate('click');
const actionTypeForm = wrapper.find('[data-test-subj="alertActionAccordion-default"]');
expect(actionTypeForm.exists()).toBeTruthy();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
ActionTypeIndex,
ActionConnector,
AlertTypeIndex,
ActionGroup,
} from '../../../types';
import { SectionLoading } from '../../components/section_loading';
import { ConnectorAddModal } from '../action_connector_form/connector_add_modal';
Expand Down Expand Up @@ -119,7 +118,7 @@ export const AlertForm = ({
const [alertThrottleUnit, setAlertThrottleUnit] = useState<string>('m');
const [isAddActionPanelOpen, setIsAddActionPanelOpen] = useState<boolean>(true);
const [connectors, setConnectors] = useState<ActionConnector[]>([]);
const [defaultActionGroup, setDefaultActionGroup] = useState<ActionGroup | undefined>(undefined);
const [defaultActionGroup, setDefaultActionGroup] = useState<string>('default');
YulNaumenko marked this conversation as resolved.
Show resolved Hide resolved
const [activeActionItem, setActiveActionItem] = useState<ActiveActionConnectorState | undefined>(
undefined
);
Expand Down Expand Up @@ -166,13 +165,14 @@ export const AlertForm = ({
],
name: 'threshold',
actionVariables: ['ctx.metadata.name', 'ctx.metadata.test'],
defaultActionGroup: 'alert',
});
const index: AlertTypeIndex = {};
for (const alertTypeItem of alertTypes) {
index[alertTypeItem.id] = alertTypeItem;
}
if (alert.alertTypeId) {
setDefaultActionGroup(index[alert.alertTypeId].actionGroups[0]);
if (alert.alertTypeId && index[alert.alertTypeId]) {
setDefaultActionGroup(index[alert.alertTypeId].defaultActionGroup);
}
setAlertTypesIndex(index);
} catch (e) {
Expand Down Expand Up @@ -266,7 +266,7 @@ export const AlertForm = ({
alert.actions.push({
id: '',
actionTypeId: actionTypeModel.id,
group: defaultActionGroup?.id ?? 'Alert',
group: defaultActionGroup,
params: {},
});
setActionProperty('id', freeConnectors[0].id, alert.actions.length - 1);
Expand All @@ -278,7 +278,7 @@ export const AlertForm = ({
alert.actions.push({
id: '',
actionTypeId: actionTypeModel.id,
group: defaultActionGroup?.id ?? 'Alert',
group: defaultActionGroup,
params: {},
});
setActionProperty('id', alert.actions.length, alert.actions.length - 1);
Expand All @@ -294,12 +294,8 @@ export const AlertForm = ({
onClick={() => {
setAlertProperty('alertTypeId', item.id);
setAlertTypeModel(item);
if (
alertTypesIndex &&
alertTypesIndex[item.id] &&
alertTypesIndex[item.id].actionGroups.length > 0
) {
setDefaultActionGroup(alertTypesIndex[item.id].actionGroups[0]);
if (alertTypesIndex && alertTypesIndex[item.id]) {
setDefaultActionGroup(alertTypesIndex[item.id].defaultActionGroup);
}
}}
>
Expand Down Expand Up @@ -356,7 +352,7 @@ export const AlertForm = ({
id,
}));
const actionTypeRegisterd = actionTypeRegistry.get(actionConnector.actionTypeId);
if (actionTypeRegisterd === null || actionItem.group !== defaultActionGroup?.id) return null;
if (!actionTypeRegisterd || actionItem.group !== defaultActionGroup) return null;
const ParamsFieldsComponent = actionTypeRegisterd.actionParamsFields;
const actionParamsErrors: { errors: IErrorObject } =
Object.keys(actionsErrors).length > 0 ? actionsErrors[actionItem.id] : { errors: {} };
Expand All @@ -368,7 +364,7 @@ export const AlertForm = ({
id={index.toString()}
className="euiAccordionForm"
buttonContentClassName="euiAccordionForm__button"
data-test-subj="alertActionAccordion"
data-test-subj={`alertActionAccordion-${defaultActionGroup}`}
buttonContent={
<EuiFlexGroup gutterSize="s" alignItems="center">
<EuiFlexItem grow={false}>
Expand Down Expand Up @@ -465,7 +461,9 @@ export const AlertForm = ({
errors={actionParamsErrors.errors}
editAction={setActionParamsProperty}
messageVariables={
alertTypesIndex ? alertTypesIndex[alert.alertTypeId].actionVariables : undefined
alertTypesIndex && alertTypesIndex[alert.alertTypeId]
? alertTypesIndex[alert.alertTypeId].actionVariables
: undefined
}
defaultMessage={alertTypeModel?.defaultActionMessage ?? undefined}
/>
Expand All @@ -479,15 +477,15 @@ export const AlertForm = ({
? actionTypesIndex[actionItem.actionTypeId].name
: actionItem.actionTypeId;
const actionTypeRegisterd = actionTypeRegistry.get(actionItem.actionTypeId);
if (actionTypeRegisterd === null || actionItem.group !== defaultActionGroup?.id) return null;
if (!actionTypeRegisterd || actionItem.group !== defaultActionGroup) return null;
return (
<EuiAccordion
initialIsOpen={true}
key={index}
id={index.toString()}
className="euiAccordionForm"
buttonContentClassName="euiAccordionForm__button"
data-test-subj="alertActionAccordion"
data-test-subj={`alertActionAccordion-${defaultActionGroup}`}
buttonContent={
<EuiFlexGroup gutterSize="s" alignItems="center">
<EuiFlexItem grow={false}>
Expand Down
Loading