Skip to content

Commit

Permalink
Moved setting of default alert type to the server api
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Feb 19, 2020
1 parent 18f19d3 commit 0e7f7b8
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 27 deletions.
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
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;
}
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 @@ -15,6 +15,7 @@ const alertType: AlertType = {
{ id: 'default', name: 'Default' },
{ id: 'other-group', name: 'Other Group' },
],
defaultActionGroup: 'default',
executor: jest.fn(),
};

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 @@ -101,6 +101,5 @@ export function getAlertType(): AlertTypeModel {
return validationResult;
},
defaultActionMessage: 'Alert [{{ctx.metadata.name}}] has exceeded the threshold',
defaultActionGroup: 'alert',
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +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<string>(
alertTypeModel?.defaultActionGroup ?? 'default'
);
const [defaultActionGroup, setDefaultActionGroup] = useState<string>('default');
const [activeActionItem, setActiveActionItem] = useState<ActiveActionConnectorState | undefined>(
undefined
);
Expand Down Expand Up @@ -167,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 (!alertTypeModel?.defaultActionGroup && alert.alertTypeId && index[alert.alertTypeId]) {
setDefaultActionGroup(index[alert.alertTypeId].actionGroups[0].id);
if (alert.alertTypeId && index[alert.alertTypeId]) {
setDefaultActionGroup(index[alert.alertTypeId].defaultActionGroup);
}
setAlertTypesIndex(index);
} catch (e) {
Expand Down Expand Up @@ -295,15 +294,8 @@ export const AlertForm = ({
onClick={() => {
setAlertProperty('alertTypeId', item.id);
setAlertTypeModel(item);
if (item.defaultActionGroup) {
setDefaultActionGroup(item.defaultActionGroup);
} else if (
!alertTypeModel?.defaultActionGroup &&
alertTypesIndex &&
alertTypesIndex[item.id] &&
alertTypesIndex[item.id].actionGroups.length > 0
) {
setDefaultActionGroup(alertTypesIndex[item.id].actionGroups[0].id);
if (alertTypesIndex && alertTypesIndex[item.id]) {
setDefaultActionGroup(alertTypesIndex[item.id].defaultActionGroup);
}
}}
>
Expand Down
10 changes: 4 additions & 6 deletions x-pack/plugins/triggers_actions_ui/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { ActionGroup } from '../../alerting/common';
import { ActionType } from '../../actions/common';
import { TypeRegistry } from './application/type_registry';
import {
Expand Down Expand Up @@ -70,18 +71,16 @@ export interface ActionConnectorTableItem extends ActionConnector {
actionType: ActionType['name'];
}

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

export interface AlertType {
id: string;
name: string;
actionGroups: ActionGroup[];
actionVariables: string[];
defaultActionGroup: ActionGroup['id'];
}

export type SanitizedAlertType = Omit<AlertType, 'apiKey'>;

export type AlertWithoutId = Omit<Alert, 'id'>;

export interface AlertTableItem extends Alert {
Expand All @@ -96,7 +95,6 @@ export interface AlertTypeModel {
validate: (alertParams: any) => ValidationResult;
alertParamsExpression: React.FunctionComponent<any>;
defaultActionMessage?: string;
defaultActionGroup?: string;
}

export interface IErrorObject {
Expand Down

0 comments on commit 0e7f7b8

Please sign in to comment.