From 891cbfac07cbda0f3df27c88386544cc12f6c080 Mon Sep 17 00:00:00 2001 From: Jiawei Wu <74562234+JiaweiWu@users.noreply.github.com> Date: Wed, 24 Jan 2024 09:07:44 -0800 Subject: [PATCH] [RAM][Maintenance Window] Fix maintenance window FE types and transforms (#173888) ## Summary Resolves: https://github.com/elastic/kibana/issues/172529 Fix up some odd types for the maintenance window frontend. Consolidates some response transforms. ### 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: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../use_archive_maintenance_window.test.tsx | 3 +- .../use_create_maintenance_window.test.tsx | 3 +- .../hooks/use_create_maintenance_window.ts | 7 +-- ...sh_and_archive_maintenance_window.test.tsx | 3 +- .../use_finish_maintenance_window.test.tsx | 3 +- .../use_update_maintenance_window.test.tsx | 9 ++- .../hooks/use_update_maintenance_window.ts | 10 +-- .../create_maintenance_windows_form.tsx | 5 +- .../maintenance_windows_list.test.tsx | 9 +-- .../components/maintenance_windows_list.tsx | 18 +++--- .../upcoming_events_popover.test.tsx | 1 - .../components/upcoming_events_popover.tsx | 5 +- ...rt_from_maintenance_window_to_form.test.ts | 23 +++---- ...convert_from_maintenance_window_to_form.ts | 5 +- .../public/pages/maintenance_windows/types.ts | 14 ----- .../maintenance_windows_api/archive.test.ts | 3 +- .../maintenance_windows_api/archive.ts | 22 ++----- .../maintenance_windows_api/create.test.ts | 7 +-- .../maintenance_windows_api/create.ts | 63 ++++++++++--------- .../maintenance_windows_api/find.test.ts | 5 +- .../services/maintenance_windows_api/find.ts | 47 +++----------- .../maintenance_windows_api/finish.test.ts | 3 +- .../maintenance_windows_api/finish.ts | 21 ++----- .../maintenance_windows_api/get.test.ts | 3 +- .../services/maintenance_windows_api/get.ts | 23 ++----- .../transform_maintenance_window_response.ts | 32 ++++++++++ .../maintenance_windows_api/update.test.ts | 7 +-- .../maintenance_windows_api/update.ts | 63 ++++++++++--------- 28 files changed, 184 insertions(+), 233 deletions(-) create mode 100644 x-pack/plugins/alerting/public/services/maintenance_windows_api/transform_maintenance_window_response.ts diff --git a/x-pack/plugins/alerting/public/hooks/use_archive_maintenance_window.test.tsx b/x-pack/plugins/alerting/public/hooks/use_archive_maintenance_window.test.tsx index d72f0bda9dc933..e6f58df8d3a7f3 100644 --- a/x-pack/plugins/alerting/public/hooks/use_archive_maintenance_window.test.tsx +++ b/x-pack/plugins/alerting/public/hooks/use_archive_maintenance_window.test.tsx @@ -7,7 +7,6 @@ import { act, renderHook } from '@testing-library/react-hooks/dom'; import { waitFor } from '@testing-library/react'; -import { MaintenanceWindow } from '../pages/maintenance_windows/types'; import { AppMockRenderer, createAppMockRenderer } from '../lib/test_utils'; import { useArchiveMaintenanceWindow } from './use_archive_maintenance_window'; @@ -37,7 +36,7 @@ const { archiveMaintenanceWindow } = jest.requireMock( '../services/maintenance_windows_api/archive' ); -const maintenanceWindow: MaintenanceWindow = { +const maintenanceWindow = { title: 'archive', duration: 1, rRule: { diff --git a/x-pack/plugins/alerting/public/hooks/use_create_maintenance_window.test.tsx b/x-pack/plugins/alerting/public/hooks/use_create_maintenance_window.test.tsx index f827287532445c..c7c1ce427f02b7 100644 --- a/x-pack/plugins/alerting/public/hooks/use_create_maintenance_window.test.tsx +++ b/x-pack/plugins/alerting/public/hooks/use_create_maintenance_window.test.tsx @@ -7,7 +7,6 @@ import { act, renderHook } from '@testing-library/react-hooks/dom'; import { waitFor } from '@testing-library/react'; -import { MaintenanceWindow } from '../pages/maintenance_windows/types'; import { AppMockRenderer, createAppMockRenderer } from '../lib/test_utils'; import { useCreateMaintenanceWindow } from './use_create_maintenance_window'; @@ -35,7 +34,7 @@ jest.mock('../services/maintenance_windows_api/create', () => ({ const { createMaintenanceWindow } = jest.requireMock('../services/maintenance_windows_api/create'); -const maintenanceWindow: MaintenanceWindow = { +const maintenanceWindow = { title: 'test', duration: 1, rRule: { diff --git a/x-pack/plugins/alerting/public/hooks/use_create_maintenance_window.ts b/x-pack/plugins/alerting/public/hooks/use_create_maintenance_window.ts index ef76e8f0f5ee42..985140fddc22b0 100644 --- a/x-pack/plugins/alerting/public/hooks/use_create_maintenance_window.ts +++ b/x-pack/plugins/alerting/public/hooks/use_create_maintenance_window.ts @@ -11,8 +11,7 @@ import type { IHttpFetchError } from '@kbn/core-http-browser'; import type { KibanaServerError } from '@kbn/kibana-utils-plugin/public'; import { useKibana } from '../utils/kibana_react'; -import { MaintenanceWindow } from '../pages/maintenance_windows/types'; -import { createMaintenanceWindow } from '../services/maintenance_windows_api/create'; +import { createMaintenanceWindow, CreateParams } from '../services/maintenance_windows_api/create'; interface UseCreateMaintenanceWindowProps { onError?: (error: IHttpFetchError) => void; @@ -26,8 +25,8 @@ export function useCreateMaintenanceWindow(props?: UseCreateMaintenanceWindowPro notifications: { toasts }, } = useKibana().services; - const mutationFn = (maintenanceWindow: MaintenanceWindow) => { - return createMaintenanceWindow({ http, maintenanceWindow }); + const mutationFn = (createParams: CreateParams) => { + return createMaintenanceWindow({ http, createParams }); }; return useMutation(mutationFn, { diff --git a/x-pack/plugins/alerting/public/hooks/use_finish_and_archive_maintenance_window.test.tsx b/x-pack/plugins/alerting/public/hooks/use_finish_and_archive_maintenance_window.test.tsx index 453a3b88cef8fb..8b55812bd0301f 100644 --- a/x-pack/plugins/alerting/public/hooks/use_finish_and_archive_maintenance_window.test.tsx +++ b/x-pack/plugins/alerting/public/hooks/use_finish_and_archive_maintenance_window.test.tsx @@ -7,7 +7,6 @@ import { act, renderHook } from '@testing-library/react-hooks/dom'; import { waitFor } from '@testing-library/react'; -import { MaintenanceWindow } from '../pages/maintenance_windows/types'; import { AppMockRenderer, createAppMockRenderer } from '../lib/test_utils'; import { useFinishAndArchiveMaintenanceWindow } from './use_finish_and_archive_maintenance_window'; @@ -41,7 +40,7 @@ const { archiveMaintenanceWindow } = jest.requireMock( '../services/maintenance_windows_api/archive' ); -const maintenanceWindow: MaintenanceWindow = { +const maintenanceWindow = { title: 'test', duration: 1, rRule: { diff --git a/x-pack/plugins/alerting/public/hooks/use_finish_maintenance_window.test.tsx b/x-pack/plugins/alerting/public/hooks/use_finish_maintenance_window.test.tsx index 06608125fd8367..6041796fcc00cf 100644 --- a/x-pack/plugins/alerting/public/hooks/use_finish_maintenance_window.test.tsx +++ b/x-pack/plugins/alerting/public/hooks/use_finish_maintenance_window.test.tsx @@ -7,7 +7,6 @@ import { act, renderHook } from '@testing-library/react-hooks/dom'; import { waitFor } from '@testing-library/react'; -import { MaintenanceWindow } from '../pages/maintenance_windows/types'; import { AppMockRenderer, createAppMockRenderer } from '../lib/test_utils'; import { useFinishMaintenanceWindow } from './use_finish_maintenance_window'; @@ -35,7 +34,7 @@ jest.mock('../services/maintenance_windows_api/finish', () => ({ const { finishMaintenanceWindow } = jest.requireMock('../services/maintenance_windows_api/finish'); -const maintenanceWindow: MaintenanceWindow = { +const maintenanceWindow = { title: 'cancel', duration: 1, rRule: { diff --git a/x-pack/plugins/alerting/public/hooks/use_update_maintenance_window.test.tsx b/x-pack/plugins/alerting/public/hooks/use_update_maintenance_window.test.tsx index b29161f0e006da..6ba19c27c362e8 100644 --- a/x-pack/plugins/alerting/public/hooks/use_update_maintenance_window.test.tsx +++ b/x-pack/plugins/alerting/public/hooks/use_update_maintenance_window.test.tsx @@ -7,7 +7,6 @@ import { act, renderHook } from '@testing-library/react-hooks/dom'; import { waitFor } from '@testing-library/react'; -import { MaintenanceWindow } from '../pages/maintenance_windows/types'; import { AppMockRenderer, createAppMockRenderer } from '../lib/test_utils'; import { useUpdateMaintenanceWindow } from './use_update_maintenance_window'; @@ -35,7 +34,7 @@ jest.mock('../services/maintenance_windows_api/update', () => ({ const { updateMaintenanceWindow } = jest.requireMock('../services/maintenance_windows_api/update'); -const maintenanceWindow: MaintenanceWindow = { +const updateParams = { title: 'updated', duration: 1, rRule: { @@ -51,7 +50,7 @@ describe('useUpdateMaintenanceWindow', () => { jest.clearAllMocks(); appMockRenderer = createAppMockRenderer(); - updateMaintenanceWindow.mockResolvedValue(maintenanceWindow); + updateMaintenanceWindow.mockResolvedValue(updateParams); }); it('should call onSuccess if api succeeds', async () => { @@ -60,7 +59,7 @@ describe('useUpdateMaintenanceWindow', () => { }); await act(async () => { - await result.current.mutate({ maintenanceWindowId: '123', maintenanceWindow }); + await result.current.mutate({ maintenanceWindowId: '123', updateParams }); }); await waitFor(() => expect(mockAddSuccess).toBeCalledWith("Updated maintenance window 'updated'") @@ -75,7 +74,7 @@ describe('useUpdateMaintenanceWindow', () => { }); await act(async () => { - await result.current.mutate({ maintenanceWindowId: '123', maintenanceWindow }); + await result.current.mutate({ maintenanceWindowId: '123', updateParams }); }); await waitFor(() => diff --git a/x-pack/plugins/alerting/public/hooks/use_update_maintenance_window.ts b/x-pack/plugins/alerting/public/hooks/use_update_maintenance_window.ts index 14e67fa644385d..403fdb3ce989d6 100644 --- a/x-pack/plugins/alerting/public/hooks/use_update_maintenance_window.ts +++ b/x-pack/plugins/alerting/public/hooks/use_update_maintenance_window.ts @@ -9,10 +9,10 @@ import { i18n } from '@kbn/i18n'; import { useMutation } from '@tanstack/react-query'; import type { IHttpFetchError } from '@kbn/core-http-browser'; import type { KibanaServerError } from '@kbn/kibana-utils-plugin/public'; +import type { MaintenanceWindow } from '../../common'; import { useKibana } from '../utils/kibana_react'; -import { MaintenanceWindow } from '../pages/maintenance_windows/types'; -import { updateMaintenanceWindow } from '../services/maintenance_windows_api/update'; +import { updateMaintenanceWindow, UpdateParams } from '../services/maintenance_windows_api/update'; interface UseUpdateMaintenanceWindowProps { onError?: (error: IHttpFetchError) => void; @@ -28,12 +28,12 @@ export function useUpdateMaintenanceWindow(props?: UseUpdateMaintenanceWindowPro const mutationFn = ({ maintenanceWindowId, - maintenanceWindow, + updateParams, }: { maintenanceWindowId: string; - maintenanceWindow: MaintenanceWindow; + updateParams: UpdateParams; }) => { - return updateMaintenanceWindow({ http, maintenanceWindowId, maintenanceWindow }); + return updateMaintenanceWindow({ http, maintenanceWindowId, updateParams }); }; return useMutation(mutationFn, { diff --git a/x-pack/plugins/alerting/public/pages/maintenance_windows/components/create_maintenance_windows_form.tsx b/x-pack/plugins/alerting/public/pages/maintenance_windows/components/create_maintenance_windows_form.tsx index fe899c371c2164..cd69f972c675aa 100644 --- a/x-pack/plugins/alerting/public/pages/maintenance_windows/components/create_maintenance_windows_form.tsx +++ b/x-pack/plugins/alerting/public/pages/maintenance_windows/components/create_maintenance_windows_form.tsx @@ -162,7 +162,10 @@ export const CreateMaintenanceWindowForm = React.memo { const date = moment('2023-04-05').toISOString(); const endDate = moment('2023-04-05').add(1, 'month').toISOString(); - const items: MaintenanceWindowFindResponse[] = [ + const items: MaintenanceWindow[] = [ { id: '1', - total: 100, title: 'Host maintenance', enabled: true, duration: 1, @@ -36,7 +34,6 @@ describe('MaintenanceWindowsList', () => { }, { id: '2', - total: 0, title: 'Server outage west coast', enabled: true, duration: 1, @@ -53,7 +50,6 @@ describe('MaintenanceWindowsList', () => { }, { id: '4', - total: 1000, title: 'Monthly maintenance window', enabled: true, duration: 1, @@ -70,7 +66,6 @@ describe('MaintenanceWindowsList', () => { }, { id: '5', - total: 200, title: 'Monthly maintenance window', enabled: true, duration: 1, diff --git a/x-pack/plugins/alerting/public/pages/maintenance_windows/components/maintenance_windows_list.tsx b/x-pack/plugins/alerting/public/pages/maintenance_windows/components/maintenance_windows_list.tsx index 5c361eee011778..0abd1c4e1863cc 100644 --- a/x-pack/plugins/alerting/public/pages/maintenance_windows/components/maintenance_windows_list.tsx +++ b/x-pack/plugins/alerting/public/pages/maintenance_windows/components/maintenance_windows_list.tsx @@ -18,12 +18,16 @@ import { EuiButton, } from '@elastic/eui'; import { css } from '@emotion/react'; -import { MaintenanceWindowFindResponse, SortDirection } from '../types'; +import { SortDirection } from '../types'; import * as i18n from '../translations'; import { useEditMaintenanceWindowsNavigation } from '../../../hooks/use_navigation'; import { STATUS_DISPLAY, STATUS_SORT } from '../constants'; import { UpcomingEventsPopover } from './upcoming_events_popover'; -import { MaintenanceWindowStatus, MAINTENANCE_WINDOW_DATE_FORMAT } from '../../../../common'; +import { + MaintenanceWindowStatus, + MAINTENANCE_WINDOW_DATE_FORMAT, + MaintenanceWindow, +} from '../../../../common'; import { StatusFilter } from './status_filter'; import { TableActionsPopover } from './table_actions_popover'; import { useFinishMaintenanceWindow } from '../../../hooks/use_finish_maintenance_window'; @@ -32,12 +36,12 @@ import { useFinishAndArchiveMaintenanceWindow } from '../../../hooks/use_finish_ interface MaintenanceWindowsListProps { loading: boolean; - items: MaintenanceWindowFindResponse[]; + items: MaintenanceWindow[]; readOnly: boolean; refreshData: () => void; } -const COLUMNS: Array> = [ +const COLUMNS: Array> = [ { field: 'title', name: i18n.NAME, @@ -58,7 +62,7 @@ const COLUMNS: Array> = [ field: 'eventStartTime', name: i18n.TABLE_START_TIME, dataType: 'date', - render: (startDate: string, item: MaintenanceWindowFindResponse) => { + render: (startDate: string, item: MaintenanceWindow) => { return ( @@ -89,7 +93,7 @@ const sorting = { }, }; -const rowProps = (item: MaintenanceWindowFindResponse) => ({ +const rowProps = (item: MaintenanceWindow) => ({ className: item.status, 'data-test-subj': 'list-item', }); @@ -148,7 +152,7 @@ export const MaintenanceWindowsList = React.memo( `; }, [euiTheme.colors.highlight]); - const actions: Array> = useMemo( + const actions: Array> = useMemo( () => [ { name: '', diff --git a/x-pack/plugins/alerting/public/pages/maintenance_windows/components/upcoming_events_popover.test.tsx b/x-pack/plugins/alerting/public/pages/maintenance_windows/components/upcoming_events_popover.test.tsx index 574bb7d1f7549c..2f4d2b9a1e9053 100644 --- a/x-pack/plugins/alerting/public/pages/maintenance_windows/components/upcoming_events_popover.test.tsx +++ b/x-pack/plugins/alerting/public/pages/maintenance_windows/components/upcoming_events_popover.test.tsx @@ -49,7 +49,6 @@ describe('rule_actions_popover', () => { updatedAt: '2023-04-14T14:58:58.997Z', eventStartTime: '2023-04-21T14:58:40.243Z', eventEndTime: '2023-04-21T14:58:40.243Z', - total: 1000, }} /> ); diff --git a/x-pack/plugins/alerting/public/pages/maintenance_windows/components/upcoming_events_popover.tsx b/x-pack/plugins/alerting/public/pages/maintenance_windows/components/upcoming_events_popover.tsx index 70895cb67586f6..b5c3d898b8ee9a 100644 --- a/x-pack/plugins/alerting/public/pages/maintenance_windows/components/upcoming_events_popover.tsx +++ b/x-pack/plugins/alerting/public/pages/maintenance_windows/components/upcoming_events_popover.tsx @@ -21,14 +21,13 @@ import { formatDate, } from '@elastic/eui'; import * as i18n from '../translations'; -import { MAINTENANCE_WINDOW_DATE_FORMAT } from '../../../../common'; +import { MAINTENANCE_WINDOW_DATE_FORMAT, MaintenanceWindow } from '../../../../common'; import { recurringSummary } from '../helpers/recurring_summary'; import { getPresets } from '../helpers/get_presets'; -import { MaintenanceWindowFindResponse } from '../types'; import { convertFromMaintenanceWindowToForm } from '../helpers/convert_from_maintenance_window_to_form'; interface UpcomingEventsPopoverProps { - maintenanceWindowFindResponse: MaintenanceWindowFindResponse; + maintenanceWindowFindResponse: MaintenanceWindow; } export const UpcomingEventsPopover: React.FC = React.memo( diff --git a/x-pack/plugins/alerting/public/pages/maintenance_windows/helpers/convert_from_maintenance_window_to_form.test.ts b/x-pack/plugins/alerting/public/pages/maintenance_windows/helpers/convert_from_maintenance_window_to_form.test.ts index 7b40b028ab2102..b11fadcb97340c 100644 --- a/x-pack/plugins/alerting/public/pages/maintenance_windows/helpers/convert_from_maintenance_window_to_form.test.ts +++ b/x-pack/plugins/alerting/public/pages/maintenance_windows/helpers/convert_from_maintenance_window_to_form.test.ts @@ -6,6 +6,7 @@ */ import moment from 'moment'; +import type { MaintenanceWindow } from '../../../../common'; import { Frequency } from '@kbn/rrule'; import { convertFromMaintenanceWindowToForm } from './convert_from_maintenance_window_to_form'; @@ -27,7 +28,7 @@ describe('convertFromMaintenanceWindowToForm', () => { freq: Frequency.YEARLY, count: 1, }, - }); + } as MaintenanceWindow); expect(maintenanceWindow).toEqual({ title, @@ -50,7 +51,7 @@ describe('convertFromMaintenanceWindowToForm', () => { interval: 1, byweekday: ['WE'], }, - }); + } as MaintenanceWindow); expect(maintenanceWindow).toEqual({ title, @@ -82,7 +83,7 @@ describe('convertFromMaintenanceWindowToForm', () => { byweekday: ['WE'], until, }, - }); + } as MaintenanceWindow); expect(maintenanceWindow).toEqual({ title, @@ -113,7 +114,7 @@ describe('convertFromMaintenanceWindowToForm', () => { byweekday: ['WE'], count: 3, }, - }); + } as MaintenanceWindow); expect(maintenanceWindow).toEqual({ title, @@ -143,7 +144,7 @@ describe('convertFromMaintenanceWindowToForm', () => { interval: 1, byweekday: ['WE'], }, - }); + } as MaintenanceWindow); expect(maintenanceWindow).toEqual({ title, @@ -172,7 +173,7 @@ describe('convertFromMaintenanceWindowToForm', () => { interval: 1, byweekday: ['+4WE'], }, - }); + } as MaintenanceWindow); expect(maintenanceWindow).toEqual({ title, @@ -202,7 +203,7 @@ describe('convertFromMaintenanceWindowToForm', () => { bymonth: [3], bymonthday: [22], }, - }); + } as MaintenanceWindow); expect(maintenanceWindow).toEqual({ title, @@ -229,7 +230,7 @@ describe('convertFromMaintenanceWindowToForm', () => { freq: Frequency.DAILY, interval: 1, }, - }); + } as MaintenanceWindow); expect(maintenanceWindow).toEqual({ title, @@ -258,7 +259,7 @@ describe('convertFromMaintenanceWindowToForm', () => { interval: 1, byweekday: ['WE', 'TH'], }, - }); + } as MaintenanceWindow); expect(maintenanceWindow).toEqual({ title, @@ -288,7 +289,7 @@ describe('convertFromMaintenanceWindowToForm', () => { interval: 1, bymonthday: [22], }, - }); + } as MaintenanceWindow); expect(maintenanceWindow).toEqual({ title, @@ -319,7 +320,7 @@ describe('convertFromMaintenanceWindowToForm', () => { bymonth: [3], bymonthday: [22], }, - }); + } as MaintenanceWindow); expect(maintenanceWindow).toEqual({ title, diff --git a/x-pack/plugins/alerting/public/pages/maintenance_windows/helpers/convert_from_maintenance_window_to_form.ts b/x-pack/plugins/alerting/public/pages/maintenance_windows/helpers/convert_from_maintenance_window_to_form.ts index 9296038a405ab4..6adc7310b25b75 100644 --- a/x-pack/plugins/alerting/public/pages/maintenance_windows/helpers/convert_from_maintenance_window_to_form.ts +++ b/x-pack/plugins/alerting/public/pages/maintenance_windows/helpers/convert_from_maintenance_window_to_form.ts @@ -8,11 +8,10 @@ import moment from 'moment'; import { Frequency } from '@kbn/rrule'; import { has } from 'lodash'; -import { MaintenanceWindow } from '../types'; +import type { FormProps, RecurringScheduleFormProps } from '../components/schema'; +import type { RRuleParams, MaintenanceWindow } from '../../../../common'; import { EndsOptions, MaintenanceWindowFrequency } from '../constants'; -import { FormProps, RecurringScheduleFormProps } from '../components/schema'; import { getInitialByWeekday } from './get_initial_by_weekday'; -import { RRuleParams } from '../../../../common'; export const convertFromMaintenanceWindowToForm = ( maintenanceWindow: MaintenanceWindow diff --git a/x-pack/plugins/alerting/public/pages/maintenance_windows/types.ts b/x-pack/plugins/alerting/public/pages/maintenance_windows/types.ts index acc3cd5420bb49..da19abfb7f5bc4 100644 --- a/x-pack/plugins/alerting/public/pages/maintenance_windows/types.ts +++ b/x-pack/plugins/alerting/public/pages/maintenance_windows/types.ts @@ -6,10 +6,6 @@ */ import { Frequency } from '@kbn/rrule'; -import { - MaintenanceWindow as MaintenanceWindowServerSide, - MaintenanceWindowModificationMetadata, -} from '../../../common'; export const RRuleFrequencyMap = { '0': Frequency.YEARLY, @@ -18,16 +14,6 @@ export const RRuleFrequencyMap = { '3': Frequency.DAILY, }; -export type MaintenanceWindow = Pick< - MaintenanceWindowServerSide, - 'title' | 'duration' | 'rRule' | 'categoryIds' | 'scopedQuery' ->; - -export type MaintenanceWindowFindResponse = MaintenanceWindowServerSide & - MaintenanceWindowModificationMetadata & { - total: number; - }; - export enum SortDirection { asc = 'asc', desc = 'desc', diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/archive.test.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/archive.test.ts index 8f0e44eaf2eb99..6143805f824c7f 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/archive.test.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/archive.test.ts @@ -6,7 +6,6 @@ */ import { httpServiceMock } from '@kbn/core/public/mocks'; -import { MaintenanceWindow } from '../../pages/maintenance_windows/types'; import { archiveMaintenanceWindow } from './archive'; const http = httpServiceMock.createStartContract(); @@ -28,7 +27,7 @@ describe('archiveMaintenanceWindow', () => { }; http.post.mockResolvedValueOnce(apiResponse); - const maintenanceWindow: MaintenanceWindow = { + const maintenanceWindow = { title: 'test', duration: 1, rRule: { diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/archive.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/archive.ts index 45c90dcc2f6499..3b8dd3b459f9d5 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/archive.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/archive.ts @@ -4,21 +4,11 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { HttpSetup } from '@kbn/core/public'; -import { AsApiContract, RewriteRequestCase } from '@kbn/actions-plugin/common'; - -import { MaintenanceWindow } from '../../pages/maintenance_windows/types'; +import type { HttpSetup } from '@kbn/core/public'; +import type { MaintenanceWindowResponse } from '../../../common/routes/maintenance_window/response'; +import type { MaintenanceWindow } from '../../../common'; import { INTERNAL_BASE_ALERTING_API_PATH } from '../../../common'; - -const rewriteBodyRes: RewriteRequestCase = ({ - r_rule: rRule, - category_ids: categoryIds, - ...rest -}) => ({ - ...rest, - rRule, - categoryIds, -}); +import { transformMaintenanceWindowResponse } from './transform_maintenance_window_response'; export async function archiveMaintenanceWindow({ http, @@ -29,12 +19,12 @@ export async function archiveMaintenanceWindow({ maintenanceWindowId: string; archive: boolean; }): Promise { - const res = await http.post>( + const res = await http.post( `${INTERNAL_BASE_ALERTING_API_PATH}/rules/maintenance_window/${encodeURIComponent( maintenanceWindowId )}/_archive`, { body: JSON.stringify({ archive }) } ); - return rewriteBodyRes(res); + return transformMaintenanceWindowResponse(res); } diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/create.test.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/create.test.ts index 5a7bbf8be2bc4a..baf2737c4114b8 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/create.test.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/create.test.ts @@ -7,7 +7,6 @@ import { httpServiceMock } from '@kbn/core/public/mocks'; import { createMaintenanceWindow } from './create'; -import { MaintenanceWindow } from '../../pages/maintenance_windows/types'; const http = httpServiceMock.createStartContract(); @@ -28,7 +27,7 @@ describe('createMaintenanceWindow', () => { }; http.post.mockResolvedValueOnce(apiResponse); - const maintenanceWindow: MaintenanceWindow = { + const createParams = { title: 'test', duration: 1, rRule: { @@ -40,8 +39,8 @@ describe('createMaintenanceWindow', () => { }, }; - const result = await createMaintenanceWindow({ http, maintenanceWindow }); - expect(result).toEqual(maintenanceWindow); + const result = await createMaintenanceWindow({ http, createParams }); + expect(result).toEqual(createParams); expect(http.post.mock.calls[0]).toMatchInlineSnapshot(` Array [ "/internal/alerting/rules/maintenance_window", diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/create.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/create.ts index 4584d839cb6b54..c196c8120620c8 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/create.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/create.ts @@ -4,47 +4,50 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { HttpSetup } from '@kbn/core/public'; -import { AsApiContract, RewriteRequestCase, RewriteResponseCase } from '@kbn/actions-plugin/common'; +import type { HttpSetup } from '@kbn/core/public'; +import type { MaintenanceWindow } from '../../../common'; +import type { CreateMaintenanceWindowRequestBody } from '../../../common/routes/maintenance_window/apis/create'; +import type { MaintenanceWindowResponse } from '../../../common/routes/maintenance_window/response'; -import { MaintenanceWindow } from '../../pages/maintenance_windows/types'; import { INTERNAL_BASE_ALERTING_API_PATH } from '../../../common'; +import { transformMaintenanceWindowResponse } from './transform_maintenance_window_response'; -const rewriteBodyRequest: RewriteResponseCase = ({ - rRule, - categoryIds, - scopedQuery, - ...res -}) => ({ - ...res, - r_rule: rRule, - category_ids: categoryIds, - scoped_query: scopedQuery, -}); +export interface CreateParams { + title: MaintenanceWindow['title']; + duration: MaintenanceWindow['duration']; + rRule: MaintenanceWindow['rRule']; + categoryIds?: MaintenanceWindow['categoryIds']; + scopedQuery?: MaintenanceWindow['scopedQuery']; +} -const rewriteBodyRes: RewriteRequestCase = ({ - r_rule: rRule, - category_ids: categoryIds, - scoped_query: scopedQuery, - ...rest -}) => ({ - ...rest, - rRule, - categoryIds, - scopedQuery, -}); +const transformCreateBodySchema = ( + createParams: CreateParams +): CreateMaintenanceWindowRequestBody => { + return { + title: createParams.title, + duration: createParams.duration, + r_rule: createParams.rRule as CreateMaintenanceWindowRequestBody['r_rule'], + ...(createParams.categoryIds !== undefined + ? { + category_ids: + createParams.categoryIds as CreateMaintenanceWindowRequestBody['category_ids'], + } + : {}), + ...(createParams.scopedQuery !== undefined ? { scoped_query: createParams.scopedQuery } : {}), + }; +}; export async function createMaintenanceWindow({ http, - maintenanceWindow, + createParams, }: { http: HttpSetup; - maintenanceWindow: MaintenanceWindow; + createParams: CreateParams; }): Promise { - const res = await http.post>( + const res = await http.post( `${INTERNAL_BASE_ALERTING_API_PATH}/rules/maintenance_window`, - { body: JSON.stringify(rewriteBodyRequest(maintenanceWindow)) } + { body: JSON.stringify(transformCreateBodySchema(createParams)) } ); - return rewriteBodyRes(res); + return transformMaintenanceWindowResponse(res); } diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/find.test.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/find.test.ts index de38f6de5af00c..14b7336187293d 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/find.test.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/find.test.ts @@ -6,7 +6,6 @@ */ import { httpServiceMock } from '@kbn/core/public/mocks'; -import { MaintenanceWindowFindResponse } from '../../pages/maintenance_windows/types'; import { findMaintenanceWindows } from './find'; import { MaintenanceWindowStatus } from '../../../common'; @@ -39,14 +38,13 @@ describe('findMaintenanceWindows', () => { updated_by: null, created_at: '2023-03-23T19:16:21.293Z', updated_at: '2023-03-23T19:16:21.293Z', - total: 1000, }, ], total: 1, }; http.get.mockResolvedValueOnce(apiResponse); - const maintenanceWindow: MaintenanceWindowFindResponse[] = [ + const maintenanceWindow = [ { id: '1', title: 'test', @@ -68,7 +66,6 @@ describe('findMaintenanceWindows', () => { updatedBy: null, createdAt: '2023-03-23T19:16:21.293Z', updatedAt: '2023-03-23T19:16:21.293Z', - total: 1000, }, ]; diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/find.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/find.ts index e54028d30dfa80..c63e491198ce9f 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/find.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/find.ts @@ -4,49 +4,20 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { HttpSetup } from '@kbn/core/public'; -import { AsApiContract, RewriteRequestCase } from '@kbn/actions-plugin/common'; +import type { HttpSetup } from '@kbn/core/public'; +import type { MaintenanceWindow } from '../../../common'; +import type { FindMaintenanceWindowsResponse } from '../../../common/routes/maintenance_window/apis/find'; -import { MaintenanceWindowFindResponse } from '../../pages/maintenance_windows/types'; import { INTERNAL_BASE_ALERTING_API_PATH } from '../../../common'; - -const rewriteBodyRes = (results: { - data: Array>; - total: number; -}): MaintenanceWindowFindResponse[] => { - return results.data.map((item) => transform(item)); -}; - -const transform: RewriteRequestCase = ({ - expiration_date: expirationDate, - r_rule: rRule, - event_start_time: eventStartTime, - event_end_time: eventEndTime, - created_by: createdBy, - updated_by: updatedBy, - created_at: createdAt, - updated_at: updatedAt, - ...rest -}) => ({ - ...rest, - expirationDate, - rRule, - eventStartTime, - eventEndTime, - createdBy, - updatedBy, - createdAt, - updatedAt, -}); +import { transformMaintenanceWindowResponse } from './transform_maintenance_window_response'; export async function findMaintenanceWindows({ http, }: { http: HttpSetup; -}): Promise { - const res = await http.get<{ - data: Array>; - total: number; - }>(`${INTERNAL_BASE_ALERTING_API_PATH}/rules/maintenance_window/_find`); - return rewriteBodyRes(res); +}): Promise { + const res = await http.get( + `${INTERNAL_BASE_ALERTING_API_PATH}/rules/maintenance_window/_find` + ); + return res.data.map((mw) => transformMaintenanceWindowResponse(mw)); } diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/finish.test.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/finish.test.ts index a67b7246a64f53..f20d810eed6c2b 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/finish.test.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/finish.test.ts @@ -6,7 +6,6 @@ */ import { httpServiceMock } from '@kbn/core/public/mocks'; -import { MaintenanceWindow } from '../../pages/maintenance_windows/types'; import { finishMaintenanceWindow } from './finish'; const http = httpServiceMock.createStartContract(); @@ -28,7 +27,7 @@ describe('finishMaintenanceWindow', () => { }; http.post.mockResolvedValueOnce(apiResponse); - const maintenanceWindow: MaintenanceWindow = { + const maintenanceWindow = { title: 'test', duration: 1, rRule: { diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/finish.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/finish.ts index a9ca1ef0a3fe60..5f59236603b169 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/finish.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/finish.ts @@ -4,21 +4,12 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { HttpSetup } from '@kbn/core/public'; -import { AsApiContract, RewriteRequestCase } from '@kbn/actions-plugin/common'; +import type { HttpSetup } from '@kbn/core/public'; +import type { MaintenanceWindow } from '../../../common'; +import type { MaintenanceWindowResponse } from '../../../common/routes/maintenance_window/response'; -import { MaintenanceWindow } from '../../pages/maintenance_windows/types'; import { INTERNAL_BASE_ALERTING_API_PATH } from '../../../common'; - -const rewriteBodyRes: RewriteRequestCase = ({ - r_rule: rRule, - category_ids: categoryIds, - ...rest -}) => ({ - ...rest, - rRule, - categoryIds, -}); +import { transformMaintenanceWindowResponse } from './transform_maintenance_window_response'; export async function finishMaintenanceWindow({ http, @@ -27,11 +18,11 @@ export async function finishMaintenanceWindow({ http: HttpSetup; maintenanceWindowId: string; }): Promise { - const res = await http.post>( + const res = await http.post( `${INTERNAL_BASE_ALERTING_API_PATH}/rules/maintenance_window/${encodeURIComponent( maintenanceWindowId )}/_finish` ); - return rewriteBodyRes(res); + return transformMaintenanceWindowResponse(res); } diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/get.test.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/get.test.ts index f19d77e55b4051..9d4887640d47e2 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/get.test.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/get.test.ts @@ -6,7 +6,6 @@ */ import { httpServiceMock } from '@kbn/core/public/mocks'; -import { MaintenanceWindow } from '../../pages/maintenance_windows/types'; import { getMaintenanceWindow } from './get'; const http = httpServiceMock.createStartContract(); @@ -28,7 +27,7 @@ describe('getMaintenanceWindow', () => { }; http.get.mockResolvedValueOnce(apiResponse); - const maintenanceWindow: MaintenanceWindow = { + const maintenanceWindow = { title: 'test', duration: 1, rRule: { diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/get.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/get.ts index 799a80cb28aad4..820f409c507792 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/get.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/get.ts @@ -4,23 +4,12 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { HttpSetup } from '@kbn/core/public'; -import { AsApiContract, RewriteRequestCase } from '@kbn/actions-plugin/common'; +import type { HttpSetup } from '@kbn/core/public'; +import type { MaintenanceWindow } from '../../../common'; +import type { MaintenanceWindowResponse } from '../../../common/routes/maintenance_window/response'; -import { MaintenanceWindow } from '../../pages/maintenance_windows/types'; import { INTERNAL_BASE_ALERTING_API_PATH } from '../../../common'; - -const rewriteBodyRes: RewriteRequestCase = ({ - r_rule: rRule, - category_ids: categoryIds, - scoped_query: scopedQuery, - ...rest -}) => ({ - ...rest, - scopedQuery, - categoryIds, - rRule, -}); +import { transformMaintenanceWindowResponse } from './transform_maintenance_window_response'; export async function getMaintenanceWindow({ http, @@ -29,11 +18,11 @@ export async function getMaintenanceWindow({ http: HttpSetup; maintenanceWindowId: string; }): Promise { - const res = await http.get>( + const res = await http.get( `${INTERNAL_BASE_ALERTING_API_PATH}/rules/maintenance_window/${encodeURIComponent( maintenanceWindowId )}` ); - return rewriteBodyRes(res); + return transformMaintenanceWindowResponse(res); } diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/transform_maintenance_window_response.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/transform_maintenance_window_response.ts new file mode 100644 index 00000000000000..a7887e684140d7 --- /dev/null +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/transform_maintenance_window_response.ts @@ -0,0 +1,32 @@ +/* + * 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. + */ + +import type { MaintenanceWindowResponse } from '../../../common/routes/maintenance_window/response'; +import type { MaintenanceWindow } from '../../../common'; + +export const transformMaintenanceWindowResponse = ( + response: MaintenanceWindowResponse +): MaintenanceWindow => { + return { + title: response.title, + enabled: response.enabled, + duration: response.duration, + expirationDate: response.expiration_date, + events: response.events, + rRule: response.r_rule, + ...(response.category_ids !== undefined ? { categoryIds: response.category_ids } : {}), + ...(response.scoped_query !== undefined ? { scopedQuery: response.scoped_query } : {}), + createdBy: response.created_by, + updatedBy: response.updated_by, + createdAt: response.created_at, + updatedAt: response.updated_at, + status: response.status as MaintenanceWindow['status'], + eventStartTime: response.event_start_time, + eventEndTime: response.event_end_time, + id: response.id, + }; +}; diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/update.test.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/update.test.ts index 4c46ac45199287..d814d9bc5942f1 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/update.test.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/update.test.ts @@ -6,7 +6,6 @@ */ import { httpServiceMock } from '@kbn/core/public/mocks'; -import { MaintenanceWindow } from '../../pages/maintenance_windows/types'; import { updateMaintenanceWindow } from './update'; const http = httpServiceMock.createStartContract(); @@ -28,7 +27,7 @@ describe('updateMaintenanceWindow', () => { }; http.post.mockResolvedValueOnce(apiResponse); - const maintenanceWindow: MaintenanceWindow = { + const updateParams = { title: 'test', duration: 1, rRule: { @@ -43,9 +42,9 @@ describe('updateMaintenanceWindow', () => { const result = await updateMaintenanceWindow({ http, maintenanceWindowId: '123', - maintenanceWindow, + updateParams, }); - expect(result).toEqual(maintenanceWindow); + expect(result).toEqual(updateParams); expect(http.post.mock.calls[0]).toMatchInlineSnapshot(` Array [ "/internal/alerting/rules/maintenance_window/123", diff --git a/x-pack/plugins/alerting/public/services/maintenance_windows_api/update.ts b/x-pack/plugins/alerting/public/services/maintenance_windows_api/update.ts index c72a2285ba5561..9f90acd683f353 100644 --- a/x-pack/plugins/alerting/public/services/maintenance_windows_api/update.ts +++ b/x-pack/plugins/alerting/public/services/maintenance_windows_api/update.ts @@ -4,51 +4,54 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { HttpSetup } from '@kbn/core/public'; -import { AsApiContract, RewriteRequestCase, RewriteResponseCase } from '@kbn/actions-plugin/common'; +import type { HttpSetup } from '@kbn/core/public'; +import type { MaintenanceWindow } from '../../../common'; +import type { UpdateMaintenanceWindowRequestBody } from '../../../common/routes/maintenance_window/apis/update'; +import type { MaintenanceWindowResponse } from '../../../common/routes/maintenance_window/response'; -import { MaintenanceWindow } from '../../pages/maintenance_windows/types'; import { INTERNAL_BASE_ALERTING_API_PATH } from '../../../common'; +import { transformMaintenanceWindowResponse } from './transform_maintenance_window_response'; -const rewriteBodyRequest: RewriteResponseCase = ({ - rRule, - categoryIds, - scopedQuery, - ...res -}) => ({ - ...res, - r_rule: rRule, - category_ids: categoryIds, - scoped_query: scopedQuery, -}); +export interface UpdateParams { + title: MaintenanceWindow['title']; + duration: MaintenanceWindow['duration']; + rRule: MaintenanceWindow['rRule']; + categoryIds?: MaintenanceWindow['categoryIds']; + scopedQuery?: MaintenanceWindow['scopedQuery']; +} -const rewriteBodyRes: RewriteRequestCase = ({ - r_rule: rRule, - category_ids: categoryIds, - scoped_query: scopedQuery, - ...rest -}) => ({ - ...rest, - rRule, - categoryIds, - scopedQuery, -}); +const transformUpdateBodySchema = ( + updateParams: UpdateParams +): UpdateMaintenanceWindowRequestBody => { + return { + title: updateParams.title, + duration: updateParams.duration, + r_rule: updateParams.rRule as UpdateMaintenanceWindowRequestBody['r_rule'], + ...(updateParams.categoryIds !== undefined + ? { + category_ids: + updateParams.categoryIds as UpdateMaintenanceWindowRequestBody['category_ids'], + } + : {}), + ...(updateParams.scopedQuery !== undefined ? { scoped_query: updateParams.scopedQuery } : {}), + }; +}; export async function updateMaintenanceWindow({ http, maintenanceWindowId, - maintenanceWindow, + updateParams, }: { http: HttpSetup; maintenanceWindowId: string; - maintenanceWindow: MaintenanceWindow; + updateParams: UpdateParams; }): Promise { - const res = await http.post>( + const res = await http.post( `${INTERNAL_BASE_ALERTING_API_PATH}/rules/maintenance_window/${encodeURIComponent( maintenanceWindowId )}`, - { body: JSON.stringify(rewriteBodyRequest(maintenanceWindow)) } + { body: JSON.stringify(transformUpdateBodySchema(updateParams)) } ); - return rewriteBodyRes(res); + return transformMaintenanceWindowResponse(res); }