From 0960af3a4a91af1f2383c96183e61d11a30cbf5c Mon Sep 17 00:00:00 2001 From: Nikita Indik Date: Fri, 21 Apr 2023 13:18:22 +0200 Subject: [PATCH] Add tests --- x-pack/plugins/alerting/common/index.ts | 2 +- .../detection_rules/maintenance_window.cy.ts | 54 +++++ .../maintenance_window_callout/api.ts | 18 ++ .../maintenance_window_callout/constants.ts | 10 + .../maintenance_window_callout.test.tsx | 223 ++++++++++++++++++ .../maintenance_window_callout.tsx | 8 +- .../translations.ts | 24 +- .../use_fetch_active_maintenance_windows.ts | 27 +++ .../use_fetch_running_maintenance_windows.ts | 39 --- 9 files changed, 349 insertions(+), 56 deletions(-) create mode 100644 x-pack/plugins/security_solution/cypress/e2e/detection_rules/maintenance_window.cy.ts create mode 100644 x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/api.ts create mode 100644 x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/constants.ts create mode 100644 x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/maintenance_window_callout.test.tsx create mode 100644 x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/use_fetch_active_maintenance_windows.ts delete mode 100644 x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/use_fetch_running_maintenance_windows.ts diff --git a/x-pack/plugins/alerting/common/index.ts b/x-pack/plugins/alerting/common/index.ts index 8b4c04d15cfc439..78f4033124f6b85 100644 --- a/x-pack/plugins/alerting/common/index.ts +++ b/x-pack/plugins/alerting/common/index.ts @@ -60,4 +60,4 @@ export const INTERNAL_BASE_ALERTING_API_PATH = '/internal/alerting'; export const INTERNAL_ALERTING_API_FIND_RULES_PATH = `${INTERNAL_BASE_ALERTING_API_PATH}/rules/_find`; export const ALERTS_FEATURE_ID = 'alerts'; export const MONITORING_HISTORY_LIMIT = 200; -export const ENABLE_MAINTENANCE_WINDOWS = false; +export const ENABLE_MAINTENANCE_WINDOWS = true; diff --git a/x-pack/plugins/security_solution/cypress/e2e/detection_rules/maintenance_window.cy.ts b/x-pack/plugins/security_solution/cypress/e2e/detection_rules/maintenance_window.cy.ts new file mode 100644 index 000000000000000..7273935fdb3740e --- /dev/null +++ b/x-pack/plugins/security_solution/cypress/e2e/detection_rules/maintenance_window.cy.ts @@ -0,0 +1,54 @@ +/* + * 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 { INTERNAL_BASE_ALERTING_API_PATH } from '@kbn/alerting-plugin/common'; +import { cleanKibana } from '../../tasks/common'; +import { login, visit } from '../../tasks/login'; +import { DETECTIONS_RULE_MANAGEMENT_URL } from '../../urls/navigation'; + +describe('Maintenance window callout on Rule Management page', () => { + let maintenanceWindowId = ''; + + before(() => { + cleanKibana(); + login(); + + // Create a test maintenance window + cy.request({ + method: 'POST', + url: `${INTERNAL_BASE_ALERTING_API_PATH}/rules/maintenance_window`, + headers: { 'kbn-xsrf': 'cypress-creds' }, + body: { + title: 'My maintenance window', + duration: 10000, + r_rule: { + dtstart: new Date().toISOString(), + tzid: 'Europe/Amsterdam', + freq: 0, + count: 1, + }, + }, + }).then((response) => { + maintenanceWindowId = response.body.id; + }); + }); + + after(() => { + // Delete a test maintenance window + cy.request({ + method: 'DELETE', + url: `${INTERNAL_BASE_ALERTING_API_PATH}/rules/maintenance_window/${maintenanceWindowId}`, + headers: { 'kbn-xsrf': 'cypress-creds' }, + }); + }); + + it('Displays the callout when there are running maintenance windows', () => { + visit(DETECTIONS_RULE_MANAGEMENT_URL); + + cy.contains('A maintenance window is currently active'); + }); +}); diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/api.ts b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/api.ts new file mode 100644 index 000000000000000..63197dda7578aac --- /dev/null +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/api.ts @@ -0,0 +1,18 @@ +/* + * 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 { MaintenanceWindow } from '@kbn/alerting-plugin/common/maintenance_window'; +import { KibanaServices } from '../../../../common/lib/kibana'; +import { GET_ACTIVE_MAINTENANCE_WINDOWS_URL } from './constants'; + +export const fetchActiveMaintenanceWindows = async ( + signal?: AbortSignal +): Promise => + KibanaServices.get().http.fetch(GET_ACTIVE_MAINTENANCE_WINDOWS_URL, { + method: 'GET', + signal, + }); diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/constants.ts b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/constants.ts new file mode 100644 index 000000000000000..f1f67bbb4e498d8 --- /dev/null +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/constants.ts @@ -0,0 +1,10 @@ +/* + * 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 { INTERNAL_BASE_ALERTING_API_PATH } from '@kbn/alerting-plugin/common'; + +export const GET_ACTIVE_MAINTENANCE_WINDOWS_URL = `${INTERNAL_BASE_ALERTING_API_PATH}/rules/maintenance_window/_active`; diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/maintenance_window_callout.test.tsx b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/maintenance_window_callout.test.tsx new file mode 100644 index 000000000000000..5a76796ff4b0925 --- /dev/null +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/maintenance_window_callout.test.tsx @@ -0,0 +1,223 @@ +/* + * 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 React from 'react'; +import { render, waitFor, cleanup } from '@testing-library/react'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { useAppToasts } from '../../../../common/hooks/use_app_toasts'; +import { useAppToastsMock } from '../../../../common/hooks/use_app_toasts.mock'; +import { MaintenanceWindowCallout } from './maintenance_window_callout'; +import { TestProviders } from '../../../../common/mock'; +import { fetchActiveMaintenanceWindows } from './api'; + +jest.mock('../../../../common/hooks/use_app_toasts'); + +jest.mock('./api', () => ({ + fetchActiveMaintenanceWindows: jest.fn(() => Promise.resolve([])), +})); + +const RUNNING_MAINTENANCE_WINDOW_1 = { + title: 'Maintenance window 1', + enabled: true, + duration: 1800000, + events: [{ gte: '2023-04-20T16:27:30.753Z', lte: '2023-04-20T16:57:30.753Z' }], + id: '63057284-ac31-42ba-fe22-adfe9732e5ae', + status: 'running', + expiration_date: '2024-04-20T16:27:41.301Z', + r_rule: { + dtstart: '2023-04-20T16:27:30.753Z', + tzid: 'Europe/Amsterdam', + freq: 0, + count: 1, + }, + created_by: 'elastic', + updated_by: 'elastic', + created_at: '2023-04-20T16:27:41.301Z', + updated_at: '2023-04-20T16:27:41.301Z', + event_start_time: '2023-04-20T16:27:30.753Z', + event_end_time: '2023-04-20T16:57:30.753Z', +}; + +const RUNNING_MAINTENANCE_WINDOW_2 = { + title: 'Maintenance window 2', + enabled: true, + duration: 1800000, + events: [{ gte: '2023-04-20T16:47:42.871Z', lte: '2023-04-20T17:11:32.192Z' }], + id: '45894340-df98-11ed-ac81-bfcb4982b4fd', + status: 'running', + expiration_date: '2023-04-20T16:47:42.871Z', + r_rule: { + dtstart: '2023-04-20T16:47:42.871Z', + tzid: 'Europe/Amsterdam', + freq: 0, + count: 1, + }, + created_by: 'elastic', + updated_by: 'elastic', + created_at: '2023-04-20T16:30:51.208Z', + updated_at: '2023-04-20T16:30:51.208Z', + event_start_time: '2023-04-20T16:47:42.871Z', + event_end_time: '2023-04-20T17:11:32.192Z', +}; + +const UPCOMING_MAINTENANCE_WINDOW = { + title: 'Upcoming maintenance window', + enabled: true, + duration: 45972, + events: [ + { gte: '2023-04-21T10:36:14.028Z', lte: '2023-04-21T10:37:00.000Z' }, + { gte: '2023-04-28T10:36:14.028Z', lte: '2023-04-28T10:37:00.000Z' }, + { gte: '2023-05-05T10:36:14.028Z', lte: '2023-05-05T10:37:00.000Z' }, + { gte: '2023-05-12T10:36:14.028Z', lte: '2023-05-12T10:37:00.000Z' }, + { gte: '2023-05-19T10:36:14.028Z', lte: '2023-05-19T10:37:00.000Z' }, + { gte: '2023-05-26T10:36:14.028Z', lte: '2023-05-26T10:37:00.000Z' }, + { gte: '2023-06-02T10:36:14.028Z', lte: '2023-06-02T10:37:00.000Z' }, + { gte: '2023-06-09T10:36:14.028Z', lte: '2023-06-09T10:37:00.000Z' }, + { gte: '2023-06-16T10:36:14.028Z', lte: '2023-06-16T10:37:00.000Z' }, + { gte: '2023-06-23T10:36:14.028Z', lte: '2023-06-23T10:37:00.000Z' }, + { gte: '2023-06-30T10:36:14.028Z', lte: '2023-06-30T10:37:00.000Z' }, + { gte: '2023-07-07T10:36:14.028Z', lte: '2023-07-07T10:37:00.000Z' }, + { gte: '2023-07-14T10:36:14.028Z', lte: '2023-07-14T10:37:00.000Z' }, + { gte: '2023-07-21T10:36:14.028Z', lte: '2023-07-21T10:37:00.000Z' }, + { gte: '2023-07-28T10:36:14.028Z', lte: '2023-07-28T10:37:00.000Z' }, + { gte: '2023-08-04T10:36:14.028Z', lte: '2023-08-04T10:37:00.000Z' }, + { gte: '2023-08-11T10:36:14.028Z', lte: '2023-08-11T10:37:00.000Z' }, + { gte: '2023-08-18T10:36:14.028Z', lte: '2023-08-18T10:37:00.000Z' }, + { gte: '2023-08-25T10:36:14.028Z', lte: '2023-08-25T10:37:00.000Z' }, + { gte: '2023-09-01T10:36:14.028Z', lte: '2023-09-01T10:37:00.000Z' }, + { gte: '2023-09-08T10:36:14.028Z', lte: '2023-09-08T10:37:00.000Z' }, + { gte: '2023-09-15T10:36:14.028Z', lte: '2023-09-15T10:37:00.000Z' }, + { gte: '2023-09-22T10:36:14.028Z', lte: '2023-09-22T10:37:00.000Z' }, + { gte: '2023-09-29T10:36:14.028Z', lte: '2023-09-29T10:37:00.000Z' }, + { gte: '2023-10-06T10:36:14.028Z', lte: '2023-10-06T10:37:00.000Z' }, + { gte: '2023-10-13T10:36:14.028Z', lte: '2023-10-13T10:37:00.000Z' }, + { gte: '2023-10-20T10:36:14.028Z', lte: '2023-10-20T10:37:00.000Z' }, + { gte: '2023-10-27T10:36:14.028Z', lte: '2023-10-27T10:37:00.000Z' }, + { gte: '2023-11-03T11:36:14.028Z', lte: '2023-11-03T11:37:00.000Z' }, + { gte: '2023-11-10T11:36:14.028Z', lte: '2023-11-10T11:37:00.000Z' }, + { gte: '2023-11-17T11:36:14.028Z', lte: '2023-11-17T11:37:00.000Z' }, + { gte: '2023-11-24T11:36:14.028Z', lte: '2023-11-24T11:37:00.000Z' }, + { gte: '2023-12-01T11:36:14.028Z', lte: '2023-12-01T11:37:00.000Z' }, + { gte: '2023-12-08T11:36:14.028Z', lte: '2023-12-08T11:37:00.000Z' }, + { gte: '2023-12-15T11:36:14.028Z', lte: '2023-12-15T11:37:00.000Z' }, + { gte: '2023-12-22T11:36:14.028Z', lte: '2023-12-22T11:37:00.000Z' }, + { gte: '2023-12-29T11:36:14.028Z', lte: '2023-12-29T11:37:00.000Z' }, + { gte: '2024-01-05T11:36:14.028Z', lte: '2024-01-05T11:37:00.000Z' }, + { gte: '2024-01-12T11:36:14.028Z', lte: '2024-01-12T11:37:00.000Z' }, + { gte: '2024-01-19T11:36:14.028Z', lte: '2024-01-19T11:37:00.000Z' }, + { gte: '2024-01-26T11:36:14.028Z', lte: '2024-01-26T11:37:00.000Z' }, + { gte: '2024-02-02T11:36:14.028Z', lte: '2024-02-02T11:37:00.000Z' }, + { gte: '2024-02-09T11:36:14.028Z', lte: '2024-02-09T11:37:00.000Z' }, + { gte: '2024-02-16T11:36:14.028Z', lte: '2024-02-16T11:37:00.000Z' }, + { gte: '2024-02-23T11:36:14.028Z', lte: '2024-02-23T11:37:00.000Z' }, + { gte: '2024-03-01T11:36:14.028Z', lte: '2024-03-01T11:37:00.000Z' }, + { gte: '2024-03-08T11:36:14.028Z', lte: '2024-03-08T11:37:00.000Z' }, + { gte: '2024-03-15T11:36:14.028Z', lte: '2024-03-15T11:37:00.000Z' }, + { gte: '2024-03-22T11:36:14.028Z', lte: '2024-03-22T11:37:00.000Z' }, + { gte: '2024-03-29T11:36:14.028Z', lte: '2024-03-29T11:37:00.000Z' }, + { gte: '2024-04-05T10:36:14.028Z', lte: '2024-04-05T10:37:00.000Z' }, + { gte: '2024-04-12T10:36:14.028Z', lte: '2024-04-12T10:37:00.000Z' }, + { gte: '2024-04-19T10:36:14.028Z', lte: '2024-04-19T10:37:00.000Z' }, + ], + id: '5eafe070-e030-11ed-ac81-bfcb4982b4fd', + status: 'upcoming', + expiration_date: '2024-04-21T10:36:26.999Z', + r_rule: { + dtstart: '2023-04-21T10:36:14.028Z', + tzid: 'Europe/Amsterdam', + freq: 3, + interval: 1, + byweekday: ['FR'], + }, + created_by: 'elastic', + updated_by: 'elastic', + created_at: '2023-04-21T10:36:26.999Z', + updated_at: '2023-04-21T10:36:26.999Z', + event_start_time: '2023-04-28T10:36:14.028Z', + event_end_time: '2023-04-28T10:37:00.000Z', +}; + +describe('MaintenanceWindowCallout', () => { + let appToastsMock: jest.Mocked>; + + beforeEach(() => { + jest.resetAllMocks(); + + appToastsMock = useAppToastsMock.create(); + (useAppToasts as jest.Mock).mockReturnValue(appToastsMock); + }); + + afterEach(() => { + cleanup(); + jest.restoreAllMocks(); + }); + + it('MW callout should be visible if currently there is at least one "running" maintenance window', async () => { + (fetchActiveMaintenanceWindows as jest.Mock).mockResolvedValue([RUNNING_MAINTENANCE_WINDOW_1]); + + const { findByText } = render(, { wrapper: TestProviders }); + + expect(await findByText('A maintenance window is currently running')).toBeInTheDocument(); + }); + + it('A single MW callout should be visible if currently there are multiple "running" maintenance windows', async () => { + (fetchActiveMaintenanceWindows as jest.Mock).mockResolvedValue([ + RUNNING_MAINTENANCE_WINDOW_1, + RUNNING_MAINTENANCE_WINDOW_2, + ]); + + const { findAllByText } = render(, { wrapper: TestProviders }); + + expect(await findAllByText('A maintenance window is currently running')).toHaveLength(1); + }); + + it('MW callout should NOT be visible if currently there are no active (running or upcoming) maintenance windows', async () => { + (fetchActiveMaintenanceWindows as jest.Mock).mockResolvedValue([]); + + const { container } = render(, { wrapper: TestProviders }); + + expect(container).toBeEmptyDOMElement(); + }); + + it('MW callout should NOT be visible if currently there are no "running" maintenance windows', async () => { + (fetchActiveMaintenanceWindows as jest.Mock).mockResolvedValue([UPCOMING_MAINTENANCE_WINDOW]); + + const { container } = render(, { wrapper: TestProviders }); + + expect(container).toBeEmptyDOMElement(); + }); + + it('User should see an error toast if there was an error while fetching maintenance windows', async () => { + const createReactQueryWrapper = () => { + const queryClient = new QueryClient({ + defaultOptions: { + queries: { + // Turn retries off, otherwise we won't be able to test errors + retry: false, + }, + }, + }); + const wrapper: React.FC = ({ children }) => ( + {children} + ); + return wrapper; + }; + + const mockError = new Error('Network error'); + (fetchActiveMaintenanceWindows as jest.Mock).mockRejectedValue(mockError); + + render(, { wrapper: createReactQueryWrapper() }); + + await waitFor(() => { + expect(appToastsMock.addError).toHaveBeenCalledTimes(1); + expect(appToastsMock.addError).toHaveBeenCalledWith(mockError, { + title: 'Failed to check if maintenance window is running', + toastMessage: "Notification actions won't run while the maintenance window is running.", + }); + }); + }); +}); diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/maintenance_window_callout.tsx b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/maintenance_window_callout.tsx index 32b131042bf8ea9..6af65d1414f7145 100644 --- a/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/maintenance_window_callout.tsx +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/maintenance_window_callout.tsx @@ -7,14 +7,14 @@ import React from 'react'; import { EuiCallOut } from '@elastic/eui'; -import { useFetchRunningMaintenanceWindows } from './use_fetch_running_maintenance_windows'; +import { useFetchActiveMaintenanceWindows } from './use_fetch_active_maintenance_windows'; import * as i18n from './translations'; export const MaintenanceWindowCallout = () => { - const { data } = useFetchRunningMaintenanceWindows(); - const runningMaintenanceWindows = data || []; + const { data } = useFetchActiveMaintenanceWindows(); + const activeMaintenanceWindows = data || []; - if (runningMaintenanceWindows.length > 0) { + if (activeMaintenanceWindows.find(({ status }) => status === 'running')) { return ( {i18n.MAINTENANCE_WINDOW_RUNNING_DESCRIPTION} diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/translations.ts b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/translations.ts index 5fa20a56b20e71b..fd9465571eab076 100644 --- a/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/translations.ts +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/translations.ts @@ -7,30 +7,30 @@ import { i18n } from '@kbn/i18n'; -export const FETCH_ERROR = i18n.translate( - 'xpack.securitySolution.detectionEngine.ruleManagementUi.maintenanceWindowCallout.fetchError', +export const MAINTENANCE_WINDOW_RUNNING = i18n.translate( + 'xpack.securitySolution.detectionEngine.ruleManagementUi.maintenanceWindowCallout.maintenanceWindowActive', { - defaultMessage: 'Failed to check if maintenance window is running', + defaultMessage: 'A maintenance window is currently running', } ); -export const FETCH_ERROR_DESCRIPTION = i18n.translate( - 'xpack.securitySolution.detectionEngine.ruleManagementUi.maintenanceWindowCallout.fetchErrorDescription', +export const MAINTENANCE_WINDOW_RUNNING_DESCRIPTION = i18n.translate( + 'xpack.securitySolution.detectionEngine.ruleManagementUi.maintenanceWindowCallout.maintenanceWindowActiveDescription', { - defaultMessage: "Notification actions won't run while the maintenance window is running.", + defaultMessage: "Notification actions won't run while the maintenance window is running", } ); -export const MAINTENANCE_WINDOW_RUNNING = i18n.translate( - 'xpack.securitySolution.detectionEngine.ruleManagementUi.maintenanceWindowCallout.maintenanceWindowRunning', +export const FETCH_ERROR = i18n.translate( + 'xpack.securitySolution.detectionEngine.ruleManagementUi.maintenanceWindowCallout.fetchError', { - defaultMessage: 'A maintenance window is currently running.', + defaultMessage: 'Failed to check if maintenance window is running', } ); -export const MAINTENANCE_WINDOW_RUNNING_DESCRIPTION = i18n.translate( - 'xpack.securitySolution.detectionEngine.ruleManagementUi.maintenanceWindowCallout.maintenanceWindowRunningDescription', +export const FETCH_ERROR_DESCRIPTION = i18n.translate( + 'xpack.securitySolution.detectionEngine.ruleManagementUi.maintenanceWindowCallout.fetchErrorDescription', { - defaultMessage: "Notification actions won't run while the maintenance window is running", + defaultMessage: "Notification actions won't run while the maintenance window is running.", } ); diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/use_fetch_active_maintenance_windows.ts b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/use_fetch_active_maintenance_windows.ts new file mode 100644 index 000000000000000..a0874ca72f5f001 --- /dev/null +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/use_fetch_active_maintenance_windows.ts @@ -0,0 +1,27 @@ +/* + * 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 { useQuery } from '@tanstack/react-query'; +import { useAppToasts } from '../../../../common/hooks/use_app_toasts'; +import * as i18n from './translations'; +import { fetchActiveMaintenanceWindows } from './api'; +import { GET_ACTIVE_MAINTENANCE_WINDOWS_URL } from './constants'; + +export const useFetchActiveMaintenanceWindows = () => { + const { addError } = useAppToasts(); + + return useQuery( + ['GET', GET_ACTIVE_MAINTENANCE_WINDOWS_URL], + ({ signal }) => fetchActiveMaintenanceWindows(signal), + { + refetchInterval: 60000, + onError: (error) => { + addError(error, { title: i18n.FETCH_ERROR, toastMessage: i18n.FETCH_ERROR_DESCRIPTION }); + }, + } + ); +}; diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/use_fetch_running_maintenance_windows.ts b/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/use_fetch_running_maintenance_windows.ts deleted file mode 100644 index 9bc984369ee5d42..000000000000000 --- a/x-pack/plugins/security_solution/public/detection_engine/rule_management_ui/components/maintenance_window_callout/use_fetch_running_maintenance_windows.ts +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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 { useQuery } from '@tanstack/react-query'; -import type { MaintenanceWindow } from '@kbn/alerting-plugin/common/maintenance_window'; -import { INTERNAL_BASE_ALERTING_API_PATH } from '@kbn/alerting-plugin/common'; -import { useAppToasts } from '../../../../common/hooks/use_app_toasts'; -import { KibanaServices } from '../../../../common/lib/kibana'; -import * as i18n from './translations'; - -const GET_RUNNING_MAINTENANCE_WINDOWS_URL = `${INTERNAL_BASE_ALERTING_API_PATH}/rules/maintenance_window/_active`; - -export const fetchRunningMaintenanceWindows = async ( - signal?: AbortSignal -): Promise => { - return KibanaServices.get().http.fetch(GET_RUNNING_MAINTENANCE_WINDOWS_URL, { - method: 'GET', - signal, - }); -}; - -export const useFetchRunningMaintenanceWindows = () => { - const { addError } = useAppToasts(); - - return useQuery( - ['GET', GET_RUNNING_MAINTENANCE_WINDOWS_URL], - ({ signal }) => fetchRunningMaintenanceWindows(signal), - { - refetchInterval: 60000, - onError: (error) => { - addError(error, { title: i18n.FETCH_ERROR, toastMessage: i18n.FETCH_ERROR_DESCRIPTION }); - }, - } - ); -};