Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] [ResponceOps][MaintenanceWindow] MX Pagination (#202539) #205066

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,10 @@
],
"maintenance-window": [
"enabled",
"events"
"events",
"expirationDate",
"title",
"updatedAt"
],
"map": [
"bounds",
Expand Down
14 changes: 14 additions & 0 deletions packages/kbn-check-mappings-update-cli/current_mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2521,6 +2521,20 @@
"events": {
"format": "epoch_millis||strict_date_optional_time",
"type": "date_range"
},
"expirationDate": {
"type": "date"
},
"title": {
"fields": {
"keyword": {
"type": "keyword"
}
},
"type": "text"
},
"updatedAt": {
"type": "date"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
"lens": "5cfa2c52b979b4f8df56dd13c477e152183468b9",
"lens-ui-telemetry": "8c47a9e393861f76e268345ecbadfc8a5fb1e0bd",
"links": "1dd432cc94619a513b75cec43660a50be7aadc90",
"maintenance-window": "bf36863f5577c2d22625258bdad906eeb4cccccc",
"maintenance-window": "b84d9e0b3f89be0ae4b6fe1af6e38b4cd2554931",
"map": "76c71023bd198fb6b1163b31bafd926fe2ceb9da",
"metrics-data-source": "81b69dc9830699d9ead5ac8dcb9264612e2a3c89",
"metrics-explorer-view": "98cf395d0e87b89ab63f173eae16735584a8ff42",
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/alerting/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export {
MAINTENANCE_WINDOW_PATHS,
MAINTENANCE_WINDOW_DEEP_LINK_IDS,
MAINTENANCE_WINDOW_DATE_FORMAT,
MAINTENANCE_WINDOW_DEFAULT_PER_PAGE,
MAINTENANCE_WINDOW_DEFAULT_TABLE_ACTIVE_PAGE,
} from './maintenance_window';

export {
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/alerting/common/maintenance_window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@ export type MaintenanceWindowDeepLinkIds =
(typeof MAINTENANCE_WINDOW_DEEP_LINK_IDS)[keyof typeof MAINTENANCE_WINDOW_DEEP_LINK_IDS];

export const MAINTENANCE_WINDOW_DATE_FORMAT = 'MM/DD/YY hh:mm A';

export const MAINTENANCE_WINDOW_DEFAULT_PER_PAGE = 10 as const;
export const MAINTENANCE_WINDOW_DEFAULT_TABLE_ACTIVE_PAGE = 1 as const;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import { maintenanceWindowResponseSchemaV1 } from '../../../response';

const MAX_DOCS = 10000;

const statusSchema = schema.oneOf([
schema.literal('running'),
schema.literal('finished'),
schema.literal('upcoming'),
schema.literal('archived'),
]);

export const findMaintenanceWindowsRequestQuerySchema = schema.object(
{
// we do not need to use schema.maybe here, because if we do not pass property page, defaultValue will be used
Expand All @@ -30,6 +37,15 @@ export const findMaintenanceWindowsRequestQuerySchema = schema.object(
description: 'The number of maintenance windows to return per page.',
},
}),
search: schema.maybe(
schema.string({
meta: {
description:
'An Elasticsearch simple_query_string query that filters the objects in the response.',
},
})
),
status: schema.maybe(schema.oneOf([statusSchema, schema.arrayOf(statusSchema)])),
},
{
validate: (params) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AppMockRenderer, createAppMockRenderer } from '../lib/test_utils';
import { useFindMaintenanceWindows } from './use_find_maintenance_windows';

const mockAddDanger = jest.fn();
const mockedHttp = jest.fn();

jest.mock('../utils/kibana_react', () => {
const originalModule = jest.requireActual('../utils/kibana_react');
Expand All @@ -21,6 +22,7 @@ jest.mock('../utils/kibana_react', () => {
return {
services: {
...services,
http: mockedHttp,
notifications: { toasts: { addDanger: mockAddDanger } },
},
};
Expand All @@ -33,6 +35,8 @@ jest.mock('../services/maintenance_windows_api/find', () => ({

const { findMaintenanceWindows } = jest.requireMock('../services/maintenance_windows_api/find');

const defaultHookProps = { page: 1, perPage: 10, search: '', selectedStatus: [] };

let appMockRenderer: AppMockRenderer;

describe('useFindMaintenanceWindows', () => {
Expand All @@ -42,10 +46,20 @@ describe('useFindMaintenanceWindows', () => {
appMockRenderer = createAppMockRenderer();
});

it('should call findMaintenanceWindows with correct arguments on successful scenario', async () => {
renderHook(() => useFindMaintenanceWindows({ ...defaultHookProps }), {
wrapper: appMockRenderer.AppWrapper,
});

await waitFor(() =>
expect(findMaintenanceWindows).toHaveBeenCalledWith({ http: mockedHttp, ...defaultHookProps })
);
});

it('should call onError if api fails', async () => {
findMaintenanceWindows.mockRejectedValue('This is an error.');

renderHook(() => useFindMaintenanceWindows(), {
renderHook(() => useFindMaintenanceWindows({ ...defaultHookProps }), {
wrapper: appMockRenderer.AppWrapper,
});

Expand All @@ -55,7 +69,7 @@ describe('useFindMaintenanceWindows', () => {
});

it('should not try to find maintenance windows if not enabled', async () => {
renderHook(() => useFindMaintenanceWindows({ enabled: false }), {
renderHook(() => useFindMaintenanceWindows({ enabled: false, ...defaultHookProps }), {
wrapper: appMockRenderer.AppWrapper,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,32 @@ import { i18n } from '@kbn/i18n';
import { useQuery } from '@tanstack/react-query';
import { useKibana } from '../utils/kibana_react';
import { findMaintenanceWindows } from '../services/maintenance_windows_api/find';
import { type MaintenanceWindowStatus } from '../../common';

interface UseFindMaintenanceWindowsProps {
enabled?: boolean;
page: number;
perPage: number;
search: string;
selectedStatus: MaintenanceWindowStatus[];
}

export const useFindMaintenanceWindows = (props?: UseFindMaintenanceWindowsProps) => {
const { enabled = true } = props || {};
export const useFindMaintenanceWindows = (params: UseFindMaintenanceWindowsProps) => {
const { enabled = true, page, perPage, search, selectedStatus } = params;

const {
http,
notifications: { toasts },
} = useKibana().services;

const queryFn = () => {
return findMaintenanceWindows({ http });
return findMaintenanceWindows({
http,
page,
perPage,
search,
selectedStatus,
});
};

const onErrorFn = (error: Error) => {
Expand All @@ -36,24 +47,22 @@ export const useFindMaintenanceWindows = (props?: UseFindMaintenanceWindowsProps
}
};

const {
isLoading,
isFetching,
isInitialLoading,
data = [],
refetch,
} = useQuery({
queryKey: ['findMaintenanceWindows'],
const queryKey = ['findMaintenanceWindows', page, perPage, search, selectedStatus];

const { isLoading, isFetching, isInitialLoading, data, refetch } = useQuery({
queryKey,
queryFn,
onError: onErrorFn,
refetchOnWindowFocus: false,
retry: false,
cacheTime: 0,
enabled,
placeholderData: { maintenanceWindows: [], total: 0 },
keepPreviousData: true,
});

return {
maintenanceWindows: data,
data,
isLoading: enabled && (isLoading || isFetching),
isInitialLoading,
refetch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ describe('MaintenanceWindowsList', () => {
isLoading={false}
items={items}
readOnly={false}
page={1}
perPage={10}
total={22}
onPageChange={() => {}}
onStatusChange={() => {}}
selectedStatus={[]}
onSearchChange={() => {}}
/>
);

Expand Down Expand Up @@ -128,6 +135,13 @@ describe('MaintenanceWindowsList', () => {
isLoading={false}
items={items}
readOnly={true}
page={1}
perPage={10}
total={22}
onPageChange={() => {}}
onStatusChange={() => {}}
selectedStatus={[]}
onSearchChange={() => {}}
/>
);

Expand All @@ -145,6 +159,13 @@ describe('MaintenanceWindowsList', () => {
isLoading={false}
items={items}
readOnly={false}
page={1}
perPage={10}
total={22}
onPageChange={() => {}}
onStatusChange={() => {}}
selectedStatus={[]}
onSearchChange={() => {}}
/>
);
fireEvent.click(result.getByTestId('refresh-button'));
Expand Down
Loading
Loading