-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ResponseOps][Alerts] Migrate alerts fields fetching to TanStack Query (
#188320) ## Summary Migrates the `useFetchBrowserFieldCapabilities` hook (renamed to `useFetchAlertsFieldsQuery`) to TanStack Query, following [this organizational logic](#186448 (comment)). This PR focuses mainly on the fetching logic itself, leaving the surrounding API surface mostly unchanged since it will be likely addressed in subsequent PRs. ## To verify 1. Create rules that fire alerts in different solutions 2. Check that the alerts table usages work correctly ({O11y, Security, Stack} alerts and rule details pages, ...) 1. Check that the alerts displayed in the table are coherent with the solution, KQL query, time filter, pagination 2. Check that pagination changes are reflected in the table 3. Check that changing the query when in pages > 0 resets the pagination to the first page 4. Check that the fields browser shows and works correctly (`Fields` button in the alerts table header) Closes point 2 of #186448 ### 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: kibanamachine <[email protected]>
- Loading branch information
1 parent
d62334f
commit f24e0cd
Showing
24 changed files
with
653 additions
and
523 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { SerializedFieldFormat } from '@kbn/field-formats-plugin/common'; | ||
import type { IFieldSubType } from '@kbn/es-query'; | ||
import type { RuntimeField } from '@kbn/data-views-plugin/common'; | ||
|
||
export interface BrowserField { | ||
aggregatable: boolean; | ||
category: string; | ||
description?: string | null; | ||
example?: string | number | null; | ||
fields: Readonly<Record<string, Partial<BrowserField>>>; | ||
format?: SerializedFieldFormat; | ||
indexes: string[]; | ||
name: string; | ||
searchable: boolean; | ||
type: string; | ||
subType?: IFieldSubType; | ||
readFromDocValues: boolean; | ||
runtimeField?: RuntimeField; | ||
} | ||
|
||
export type BrowserFields = Record<string, Partial<BrowserField>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ages/kbn-alerts-ui-shared/src/common/apis/fetch_alerts_fields/fetch_alerts_fields.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { httpServiceMock } from '@kbn/core/public/mocks'; | ||
import { fetchAlertsFields } from '.'; | ||
import { AlertConsumers } from '@kbn/rule-data-utils'; | ||
|
||
describe('fetchAlertsFields', () => { | ||
const http = httpServiceMock.createStartContract(); | ||
test('should call the browser_fields API with the correct parameters', async () => { | ||
const featureIds = [AlertConsumers.STACK_ALERTS]; | ||
http.get.mockResolvedValueOnce({ | ||
browserFields: { fakeCategory: {} }, | ||
fields: [ | ||
{ | ||
name: 'fakeCategory', | ||
}, | ||
], | ||
}); | ||
const result = await fetchAlertsFields({ http, featureIds }); | ||
expect(result).toEqual({ | ||
browserFields: { fakeCategory: {} }, | ||
fields: [ | ||
{ | ||
name: 'fakeCategory', | ||
}, | ||
], | ||
}); | ||
expect(http.get).toHaveBeenLastCalledWith('/internal/rac/alerts/browser_fields', { | ||
query: { featureIds }, | ||
}); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/kbn-alerts-ui-shared/src/common/apis/fetch_alerts_fields/fetch_alerts_fields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { FieldDescriptor } from '@kbn/data-views-plugin/server'; | ||
import type { BrowserFields } from '@kbn/alerting-types'; | ||
import type { FetchAlertsFieldsParams } from './types'; | ||
import { BASE_RAC_ALERTS_API_PATH } from '../../constants'; | ||
|
||
export const fetchAlertsFields = ({ http, featureIds }: FetchAlertsFieldsParams) => | ||
http.get<{ browserFields: BrowserFields; fields: FieldDescriptor[] }>( | ||
`${BASE_RAC_ALERTS_API_PATH}/browser_fields`, | ||
{ | ||
query: { featureIds }, | ||
} | ||
); |
10 changes: 10 additions & 0 deletions
10
packages/kbn-alerts-ui-shared/src/common/apis/fetch_alerts_fields/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export * from './fetch_alerts_fields'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
packages/kbn-alerts-ui-shared/src/common/hooks/use_fetch_alerts_fields_query.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React, { FunctionComponent } from 'react'; | ||
import type { HttpSetup } from '@kbn/core-http-browser'; | ||
import { AlertConsumers } from '@kbn/rule-data-utils'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { testQueryClientConfig } from '../test_utils/test_query_client_config'; | ||
import { useFetchAlertsFieldsQuery } from './use_fetch_alerts_fields_query'; | ||
|
||
const queryClient = new QueryClient(testQueryClientConfig); | ||
|
||
const wrapper: FunctionComponent = ({ children }) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
|
||
const mockHttpClient = { | ||
get: jest.fn(), | ||
} as unknown as HttpSetup; | ||
|
||
const emptyData = { browserFields: {}, fields: [] }; | ||
|
||
describe('useFetchAlertsFieldsQuery', () => { | ||
const mockHttpGet = jest.mocked(mockHttpClient.get); | ||
|
||
beforeEach(() => { | ||
mockHttpGet.mockResolvedValue({ | ||
browserFields: { fakeCategory: {} }, | ||
fields: [ | ||
{ | ||
name: 'fakeCategory', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
mockHttpGet.mockClear(); | ||
queryClient.clear(); | ||
}); | ||
|
||
it('should not fetch for siem', () => { | ||
const { result } = renderHook( | ||
() => useFetchAlertsFieldsQuery({ http: mockHttpClient, featureIds: ['siem'] }), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
|
||
expect(mockHttpGet).toHaveBeenCalledTimes(0); | ||
expect(result.current.data).toEqual(emptyData); | ||
}); | ||
|
||
it('should call the api only once', async () => { | ||
const { result, rerender, waitForValueToChange } = renderHook( | ||
() => useFetchAlertsFieldsQuery({ http: mockHttpClient, featureIds: ['apm'] }), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
|
||
await waitForValueToChange(() => result.current.data); | ||
|
||
expect(mockHttpGet).toHaveBeenCalledTimes(1); | ||
expect(result.current.data).toEqual({ | ||
browserFields: { fakeCategory: {} }, | ||
fields: [ | ||
{ | ||
name: 'fakeCategory', | ||
}, | ||
], | ||
}); | ||
|
||
rerender(); | ||
|
||
expect(mockHttpGet).toHaveBeenCalledTimes(1); | ||
expect(result.current.data).toEqual({ | ||
browserFields: { fakeCategory: {} }, | ||
fields: [ | ||
{ | ||
name: 'fakeCategory', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('should not fetch if the only featureId is not valid', async () => { | ||
const { result } = renderHook( | ||
() => | ||
useFetchAlertsFieldsQuery({ | ||
http: mockHttpClient, | ||
featureIds: ['alerts'] as unknown as AlertConsumers[], | ||
}), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
|
||
expect(mockHttpGet).toHaveBeenCalledTimes(0); | ||
expect(result.current.data).toEqual(emptyData); | ||
}); | ||
|
||
it('should not fetch if all featureId are not valid', async () => { | ||
const { result } = renderHook( | ||
() => | ||
useFetchAlertsFieldsQuery({ | ||
http: mockHttpClient, | ||
featureIds: ['alerts', 'tomato'] as unknown as AlertConsumers[], | ||
}), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
|
||
expect(mockHttpGet).toHaveBeenCalledTimes(0); | ||
expect(result.current.data).toEqual(emptyData); | ||
}); | ||
|
||
it('should filter out the non valid feature id', async () => { | ||
renderHook( | ||
() => | ||
useFetchAlertsFieldsQuery({ | ||
http: mockHttpClient, | ||
featureIds: ['alerts', 'apm', 'logs'] as AlertConsumers[], | ||
}), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
|
||
expect(mockHttpGet).toHaveBeenCalledTimes(1); | ||
expect(mockHttpGet).toHaveBeenCalledWith('/internal/rac/alerts/browser_fields', { | ||
query: { featureIds: ['apm', 'logs'] }, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.