Skip to content

Commit

Permalink
uptime - migrate legacy alert apis
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiqueclarke committed Aug 9, 2021
1 parent b16d20b commit 4ba8ab9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 27 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/triggers_actions_ui/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type {
IErrorObject,
AlertFlyoutCloseReason,
AlertTypeParams,
AsApiContract,
} from './types';

export {
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/triggers_actions_ui/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
AlertHistoryDocumentTemplate,
ALERT_HISTORY_PREFIX,
AlertHistoryDefaultIndexName,
AsApiContract,
} from '../../actions/common';
import { TypeRegistry } from './application/type_registry';
import {
Expand Down Expand Up @@ -58,6 +59,7 @@ export {
AlertHistoryDocumentTemplate,
AlertHistoryDefaultIndexName,
ALERT_HISTORY_PREFIX,
AsApiContract,
};

export type ActionTypeIndex = Record<string, ActionType>;
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/uptime/common/constants/rest_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export enum API_URLS {
ML_CAPABILITIES = '/api/ml/ml_capabilities',
ML_ANOMALIES_RESULT = `/api/ml/results/anomalies_table_data`,

ALERT_ACTIONS = '/api/actions',
CREATE_ALERT = '/api/alerts/alert',
ALERT = '/api/alerts/alert/',
ALERTS_FIND = '/api/alerts/_find',
ACTION_TYPES = '/api/actions/list_action_types',
RULE_CONNECTORS = '/api/actions/connectors',
CREATE_RULE = '/api/alerting/rule',
DELETE_RULE = '/api/alerting/rule/',
RULES_FIND = '/api/alerting/rules/_find',
CONNECTOR_TYPES = '/api/actions/connector_types',
}
4 changes: 0 additions & 4 deletions x-pack/plugins/uptime/public/state/api/alert_actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ describe('Alert Actions factory', () => {
});
expect(resp).toEqual([
{
actionTypeId: '.pagerduty',
group: 'recovered',
id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9',
params: {
Expand All @@ -69,7 +68,6 @@ describe('Alert Actions factory', () => {
},
},
{
actionTypeId: '.pagerduty',
group: 'xpack.uptime.alerts.actionGroups.monitorStatus',
id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9',
params: {
Expand Down Expand Up @@ -103,7 +101,6 @@ describe('Alert Actions factory', () => {
});
expect(resp).toEqual([
{
actionTypeId: '.pagerduty',
group: 'recovered',
id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9',
params: {
Expand All @@ -114,7 +111,6 @@ describe('Alert Actions factory', () => {
},
},
{
actionTypeId: '.pagerduty',
group: 'xpack.uptime.alerts.actionGroups.monitorStatus',
id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9',
params: {
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/uptime/public/state/api/alert_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const WEBHOOK_ACTION_ID: ActionTypeId = '.webhook';

const { MONITOR_STATUS } = ACTION_GROUP_DEFINITIONS;

export type RuleAction = Omit<AlertAction, 'actionTypeId'>;

const getRecoveryMessage = (selectedMonitor: Ping) => {
return i18n.translate('xpack.uptime.alerts.monitorStatus.recoveryMessage', {
defaultMessage: 'Monitor {monitor} with url {url} has recovered with status Up',
Expand All @@ -44,18 +46,16 @@ const getRecoveryMessage = (selectedMonitor: Ping) => {
};

export function populateAlertActions({ defaultActions, selectedMonitor }: NewAlertParams) {
const actions: AlertAction[] = [];
const actions: RuleAction[] = [];
defaultActions.forEach((aId) => {
const action: AlertAction = {
const action: RuleAction = {
id: aId.id,
actionTypeId: aId.actionTypeId,
group: MONITOR_STATUS.id,
params: {},
};

const recoveredAction: AlertAction = {
const recoveredAction: RuleAction = {
id: aId.id,
actionTypeId: aId.actionTypeId,
group: 'recovered',
params: {
message: getRecoveryMessage(selectedMonitor),
Expand Down
63 changes: 50 additions & 13 deletions x-pack/plugins/uptime/public/state/api/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,31 @@ import { apiService } from './utils';
import { ActionConnector } from '../alerts/alerts';

import { AlertsResult, MonitorIdParam } from '../actions/types';
import { ActionType, AlertAction } from '../../../../triggers_actions_ui/public';
import { ActionType, AsApiContract } from '../../../../triggers_actions_ui/public';
import { API_URLS } from '../../../common/constants';
import { Alert, AlertTypeParams } from '../../../../alerting/common';
import { AtomicStatusCheckParams } from '../../../common/runtime_types/alerts';

import { populateAlertActions } from './alert_actions';
import { populateAlertActions, RuleAction } from './alert_actions';
import { Ping } from '../../../common/runtime_types/ping';

const UPTIME_AUTO_ALERT = 'UPTIME_AUTO';

export const fetchConnectors = async () => {
return await apiService.get(API_URLS.ALERT_ACTIONS);
export const fetchConnectors = async (): Promise<ActionConnector[]> => {
const response = (await apiService.get(API_URLS.RULE_CONNECTORS)) as Array<
AsApiContract<ActionConnector>
>;
/* eslint-disable @typescript-eslint/naming-convention */
return response.map(
({ connector_type_id, referenced_by_count, is_preconfigured, is_missing_secrets, ...res }) => ({
...res,
actionTypeId: connector_type_id,
referencedByCount: referenced_by_count,
isPreconfigured: is_preconfigured,
isMissingSecrets: is_missing_secrets,
})
);
/* eslint-enable @typescript-eslint/naming-convention */
};

export interface NewAlertParams extends AlertTypeParams {
Expand All @@ -41,14 +54,21 @@ type NewMonitorStatusAlert = Omit<
| 'muteAll'
| 'mutedInstanceIds'
| 'executionStatus'
>;
| 'alertTypeId'
| 'notifyWhen'
| 'actions'
> & {
rule_type_id: Alert<AtomicStatusCheckParams>['alertTypeId'];
notify_when: Alert<AtomicStatusCheckParams>['notifyWhen'];
actions: RuleAction[];
};

export const createAlert = async ({
defaultActions,
monitorId,
selectedMonitor,
}: NewAlertParams): Promise<Alert> => {
const actions: AlertAction[] = populateAlertActions({
const actions: RuleAction[] = populateAlertActions({
defaultActions,
selectedMonitor,
});
Expand All @@ -66,16 +86,16 @@ export const createAlert = async ({
filters: { 'url.port': [], 'observer.geo.name': [], 'monitor.type': [], tags: [] },
},
consumer: 'uptime',
alertTypeId: CLIENT_ALERT_TYPES.MONITOR_STATUS,
rule_type_id: CLIENT_ALERT_TYPES.MONITOR_STATUS,
schedule: { interval: '1m' },
notifyWhen: 'onActionGroupChange',
notify_when: 'onActionGroupChange',
tags: [UPTIME_AUTO_ALERT],
name: `${selectedMonitor?.monitor.name || selectedMonitor?.url?.full}(Simple status alert)`,
enabled: true,
throttle: null,
};

return await apiService.post(API_URLS.CREATE_ALERT, data);
return await apiService.post(API_URLS.CREATE_RULE, data);
};

export const fetchMonitorAlertRecords = async (): Promise<AlertsResult> => {
Expand All @@ -89,7 +109,7 @@ export const fetchMonitorAlertRecords = async (): Promise<AlertsResult> => {
search_fields: ['name', 'tags'],
search: 'UPTIME_AUTO',
};
return await apiService.get(API_URLS.ALERTS_FIND, data);
return await apiService.get(API_URLS.RULES_FIND, data);
};

export const fetchAlertRecords = async ({
Expand All @@ -103,14 +123,31 @@ export const fetchAlertRecords = async ({
sort_field: 'name.keyword',
sort_order: 'asc',
};
const alerts = await apiService.get(API_URLS.ALERTS_FIND, data);
const alerts = await apiService.get(API_URLS.RULES_FIND, data);
return alerts.data.find((alert: Alert<NewAlertParams>) => alert.params.monitorId === monitorId);
};

export const disableAlertById = async ({ alertId }: { alertId: string }) => {
return await apiService.delete(API_URLS.ALERT + alertId);
return await apiService.delete(API_URLS.DELETE_RULE + alertId);
};

export const fetchActionTypes = async (): Promise<ActionType[]> => {
return await apiService.get(API_URLS.ACTION_TYPES);
const response = (await apiService.get(API_URLS.CONNECTOR_TYPES)) as Array<
AsApiContract<ActionType>
>;
/* eslint-disable @typescript-eslint/naming-convention */
return response.map<ActionType>(
({
enabled_in_config,
enabled_in_license,
minimum_license_required,
...res
}: AsApiContract<ActionType>) => ({
...res,
enabledInConfig: enabled_in_config,
enabledInLicense: enabled_in_license,
minimumLicenseRequired: minimum_license_required,
})
);
/* eslint-enable @typescript-eslint/naming-convention */
};

0 comments on commit 4ba8ab9

Please sign in to comment.