Skip to content

Commit

Permalink
fixed due to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Apr 5, 2021
1 parent 0d209cf commit 4cb8283
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* 2.0.
*/
import { AlertExecutionStatus } from '../../../../../alerting/common';
import { RewriteRequestCase } from '../../../../../actions/common';
import { AsApiContract, RewriteRequestCase } from '../../../../../actions/common';
import { Alert, AlertAction } from '../../../types';

export const transformAction: RewriteRequestCase<AlertAction> = ({
const transformAction: RewriteRequestCase<AlertAction> = ({
group,
id,
connector_type_id: actionTypeId,
Expand All @@ -20,7 +20,7 @@ export const transformAction: RewriteRequestCase<AlertAction> = ({
actionTypeId,
});

export const transformExecutionStatus: RewriteRequestCase<AlertExecutionStatus> = ({
const transformExecutionStatus: RewriteRequestCase<AlertExecutionStatus> = ({
last_execution_date: lastExecutionDate,
...rest
}) => ({
Expand Down Expand Up @@ -52,8 +52,10 @@ export const transformAlert: RewriteRequestCase<Alert> = ({
notifyWhen,
muteAll,
mutedInstanceIds,
executionStatus,
actions,
executionStatus: executionStatus ? transformExecutionStatus(executionStatus) : undefined,
actions: actions
? actions.map((action: AsApiContract<AlertAction>) => transformAction(action))
: [],
scheduledTaskId,
...rest,
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ import { HttpSetup } from 'kibana/public';
import { RewriteResponseCase } from '../../../../../actions/common';
import { Alert, AlertUpdates } from '../../../types';
import { BASE_ALERTING_API_PATH } from '../../constants';
import {
transformAction,
transformAlert,
transformExecutionStatus,
} from './common_transformations';
import { transformAlert } from './common_transformations';

const rewriteBodyRequest: RewriteResponseCase<
Omit<AlertUpdates, 'createdBy' | 'updatedBy' | 'muteAll' | 'mutedInstanceIds' | 'executionStatus'>
> = ({ alertTypeId, notifyWhen, actions, ...res }): any => ({
type AlertCreateBody = Omit<
AlertUpdates,
'createdBy' | 'updatedBy' | 'muteAll' | 'mutedInstanceIds' | 'executionStatus'
>;
const rewriteBodyRequest: RewriteResponseCase<AlertCreateBody> = ({
alertTypeId,
notifyWhen,
actions,
...res
}): any => ({
...res,
rule_type_id: alertTypeId,
notify_when: notifyWhen,
actions: actions.map(({ group, id, actionTypeId, params }) => ({
actions: actions.map(({ group, id, params }) => ({
group,
id,
params,
connector_type_id: actionTypeId,
})),
});

Expand All @@ -33,17 +35,10 @@ export async function createAlert({
alert,
}: {
http: HttpSetup;
alert: Omit<
AlertUpdates,
'createdBy' | 'updatedBy' | 'muteAll' | 'mutedInstanceIds' | 'executionStatus'
>;
alert: AlertCreateBody;
}): Promise<Alert> {
const res = await http.post(`${BASE_ALERTING_API_PATH}/rule`, {
body: JSON.stringify(rewriteBodyRequest(alert)),
});
const actions = res.actions.map((action: any) => transformAction(action));
const executionStatus = transformExecutionStatus
? transformExecutionStatus(res.execution_status)
: undefined;
return transformAlert({ ...res, actions, execution_status: executionStatus });
return transformAlert(res);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
import { HttpSetup } from 'kibana/public';
import { Alert } from '../../../types';
import { BASE_ALERTING_API_PATH } from '../../constants';
import {
transformAction,
transformAlert,
transformExecutionStatus,
} from './common_transformations';
import { transformAlert } from './common_transformations';

export async function loadAlert({
http,
Expand All @@ -21,7 +17,5 @@ export async function loadAlert({
alertId: string;
}): Promise<Alert> {
const res = await http.get(`${BASE_ALERTING_API_PATH}/rule/${alertId}`);
const actions = res.actions.map((action: any) => transformAction(action));
const executionStatus = transformExecutionStatus(res.execution_status as any);
return transformAlert({ ...res, actions, execution_status: executionStatus });
return transformAlert(res);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const rewriteAlertingFrameworkHeath: RewriteRequestCase<AlertsHealth> = ({
...res,
});

const rewriteBodyReq: RewriteRequestCase<AlertingFrameworkHealth> = ({
const rewriteBodyRes: RewriteRequestCase<AlertingFrameworkHealth> = ({
is_sufficiently_secure: isSufficientlySecure,
has_permanent_encryption_key: hasPermanentEncryptionKey,
alerting_framework_heath: alertingFrameworkHeath,
Expand All @@ -40,5 +40,5 @@ export async function alertingFrameworkHealth({
}): Promise<AlertingFrameworkHealth> {
const res = await http.get(`${BASE_ALERTING_API_PATH}/_health`);
const alertingFrameworkHeath = rewriteAlertingFrameworkHeath(res.alerting_framework_heath);
return { ...rewriteBodyReq(res), alertingFrameworkHeath };
return { ...rewriteBodyRes(res), alertingFrameworkHeath };
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ import { BASE_ALERTING_API_PATH } from '../../constants';
import { Alert, Pagination, Sorting } from '../../../types';
import { AsApiContract } from '../../../../../actions/common';
import { mapFiltersToKql } from './map_filters_to_kql';
import {
transformAction,
transformExecutionStatus,
transformAlert,
} from './common_transformations';
import { transformAlert } from './common_transformations';

const rewriteResponseRes = (results: Array<AsApiContract<Alert>>): Alert[] => {
return results.map((item) => {
const actions = item.actions.map((action) => transformAction(action as any));
const executionStatus = transformExecutionStatus(item.execution_status as any);
return transformAlert({ ...item, actions, execution_status: executionStatus });
});
return results.map((item) => transformAlert(item));
};

export async function loadAlerts({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('loadAlertState', () => {
});

expect(await loadAlertState({ http, alertId })).toEqual(resolvedValue);
expect(http.get).toHaveBeenCalledWith(`/api/alerting/rule/${alertId}/state`);
expect(http.get).toHaveBeenCalledWith(`/internal/alerting/rule/${alertId}/state`);
});

test('should parse AlertInstances', async () => {
Expand Down Expand Up @@ -88,14 +88,14 @@ describe('loadAlertState', () => {
},
},
});
expect(http.get).toHaveBeenCalledWith(`/api/alerting/rule/${alertId}/state`);
expect(http.get).toHaveBeenCalledWith(`/internal/alerting/rule/${alertId}/state`);
});

test('should handle empty response from api', async () => {
const alertId = uuid.v4();
http.get.mockResolvedValueOnce('');

expect(await loadAlertState({ http, alertId })).toEqual({});
expect(http.get).toHaveBeenCalledWith(`/api/alerting/rule/${alertId}/state`);
expect(http.get).toHaveBeenCalledWith(`/internal/alerting/rule/${alertId}/state`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { pipe } from 'fp-ts/lib/pipeable';
import { fold } from 'fp-ts/lib/Either';
import { Errors, identity } from 'io-ts';
import { AlertTaskState } from '../../../types';
import { BASE_ALERTING_API_PATH } from '../../constants';
import { INTERNAL_BASE_ALERTING_API_PATH } from '../../constants';
import { alertStateSchema } from '../../../../../alerting/common';
import { AsApiContract, RewriteRequestCase } from '../../../../../actions/common';

Expand All @@ -34,7 +34,7 @@ export async function loadAlertState({
alertId: string;
}): Promise<AlertTaskState> {
return await http
.get(`${BASE_ALERTING_API_PATH}/rule/${alertId}/state`)
.get(`${INTERNAL_BASE_ALERTING_API_PATH}/rule/${alertId}/state`)
.then((state: AsApiContract<AlertTaskState> | EmptyHttpResponse) =>
state ? rewriteBodyRes(state) : {}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ import { pick } from 'lodash';
import { BASE_ALERTING_API_PATH } from '../../constants';
import { Alert, AlertUpdates } from '../../../types';
import { RewriteResponseCase } from '../../../../../actions/common';
import { transformAlert } from './common_transformations';

const rewriteBodyRequest: RewriteResponseCase<
Pick<
AlertUpdates,
'name' | 'tags' | 'schedule' | 'actions' | 'params' | 'throttle' | 'notifyWhen'
>
> = ({ notifyWhen, actions, ...res }): any => ({
type AlertUpdatesBody = Pick<
AlertUpdates,
'name' | 'tags' | 'schedule' | 'actions' | 'params' | 'throttle' | 'notifyWhen'
>;
const rewriteBodyRequest: RewriteResponseCase<AlertUpdatesBody> = ({
notifyWhen,
actions,
...res
}): any => ({
...res,
notify_when: notifyWhen,
actions: actions.map(({ group, id, actionTypeId, params }) => ({
actions: actions.map(({ group, id, params }) => ({
group,
id,
params,
connector_type_id: actionTypeId,
})),
});

Expand All @@ -38,11 +41,12 @@ export async function updateAlert({
>;
id: string;
}): Promise<Alert> {
return await http.put(`${BASE_ALERTING_API_PATH}/rule/${id}`, {
const res = await http.put(`${BASE_ALERTING_API_PATH}/rule/${id}`, {
body: JSON.stringify(
rewriteBodyRequest(
pick(alert, ['throttle', 'name', 'tags', 'schedule', 'params', 'actions', 'notifyWhen'])
)
),
});
return transformAlert(res);
}

0 comments on commit 4cb8283

Please sign in to comment.