forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Response Ops][Cases] Fetch alerts within observability (elastic#123883…
…) (elastic#125347) Co-authored-by: Kibana Machine <[email protected]> (cherry picked from commit 30ed7bb) Co-authored-by: Jonathan Buttner <[email protected]>
- Loading branch information
1 parent
7a65488
commit 6f5ced9
Showing
16 changed files
with
493 additions
and
72 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
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
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
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
53 changes: 0 additions & 53 deletions
53
x-pack/plugins/observability/public/pages/cases/helpers.ts
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
x-pack/plugins/observability/public/pages/cases/use_data_fetcher.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,65 @@ | ||
/* | ||
* 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 { useState, useMemo, useEffect } from 'react'; | ||
|
||
import { HttpSetup } from 'kibana/public'; | ||
import { useKibana } from '../../utils/kibana_react'; | ||
|
||
type DataFetcher<T, R> = (params: T, ctrl: AbortController, http: HttpSetup) => Promise<R>; | ||
|
||
export const useDataFetcher = <ApiCallParams, AlertDataType>({ | ||
paramsForApiCall, | ||
initialDataState, | ||
executeApiCall, | ||
shouldExecuteApiCall, | ||
}: { | ||
paramsForApiCall: ApiCallParams; | ||
initialDataState: AlertDataType; | ||
executeApiCall: DataFetcher<ApiCallParams, AlertDataType>; | ||
shouldExecuteApiCall: (params: ApiCallParams) => boolean; | ||
}) => { | ||
const { http } = useKibana().services; | ||
const [loading, setLoading] = useState(false); | ||
const [data, setData] = useState<AlertDataType>(initialDataState); | ||
|
||
const { fetch, cancel } = useMemo(() => { | ||
const abortController = new AbortController(); | ||
let isCanceled = false; | ||
|
||
return { | ||
fetch: async () => { | ||
if (shouldExecuteApiCall(paramsForApiCall)) { | ||
setLoading(true); | ||
|
||
const results = await executeApiCall(paramsForApiCall, abortController, http); | ||
if (!isCanceled) { | ||
setLoading(false); | ||
setData(results); | ||
} | ||
} | ||
}, | ||
cancel: () => { | ||
isCanceled = true; | ||
abortController.abort(); | ||
}, | ||
}; | ||
}, [executeApiCall, http, paramsForApiCall, shouldExecuteApiCall]); | ||
|
||
useEffect(() => { | ||
fetch(); | ||
|
||
return () => { | ||
cancel(); | ||
}; | ||
}, [fetch, cancel]); | ||
|
||
return { | ||
loading, | ||
data, | ||
}; | ||
}; |
99 changes: 99 additions & 0 deletions
99
x-pack/plugins/observability/public/pages/cases/use_fetch_alert_data.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,99 @@ | ||
/* | ||
* 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 { act, renderHook } from '@testing-library/react-hooks'; | ||
import { kibanaStartMock } from '../../utils/kibana_react.mock'; | ||
import { useFetchAlertData } from './use_fetch_alert_data'; | ||
|
||
const mockUseKibanaReturnValue = kibanaStartMock.startContract(); | ||
|
||
jest.mock('../../utils/kibana_react', () => ({ | ||
__esModule: true, | ||
useKibana: jest.fn(() => mockUseKibanaReturnValue), | ||
})); | ||
|
||
describe('useFetchAlertData', () => { | ||
const testIds = ['123']; | ||
|
||
beforeEach(() => { | ||
mockUseKibanaReturnValue.services.http.post.mockImplementation(async () => ({ | ||
hits: { | ||
hits: [ | ||
{ | ||
_id: '123', | ||
_index: 'index', | ||
_source: { | ||
testField: 'test', | ||
}, | ||
}, | ||
], | ||
}, | ||
})); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('initially is not loading and does not have data', async () => { | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook<string, [boolean, Record<string, unknown>]>( | ||
() => useFetchAlertData(testIds) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual([false, {}]); | ||
}); | ||
}); | ||
|
||
it('returns no data when an error occurs', async () => { | ||
mockUseKibanaReturnValue.services.http.post.mockImplementation(async () => { | ||
throw new Error('an http error'); | ||
}); | ||
|
||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook<string, [boolean, Record<string, unknown>]>( | ||
() => useFetchAlertData(testIds) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual([false, {}]); | ||
}); | ||
}); | ||
|
||
it('retrieves the alert data', async () => { | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook<string, [boolean, Record<string, unknown>]>( | ||
() => useFetchAlertData(testIds) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual([ | ||
false, | ||
{ '123': { _id: '123', _index: 'index', testField: 'test' } }, | ||
]); | ||
}); | ||
}); | ||
|
||
it('does not populate the results when the request is canceled', async () => { | ||
await act(async () => { | ||
const { result, waitForNextUpdate, unmount } = renderHook< | ||
string, | ||
[boolean, Record<string, unknown>] | ||
>(() => useFetchAlertData(testIds)); | ||
|
||
await waitForNextUpdate(); | ||
unmount(); | ||
|
||
expect(result.current).toEqual([false, {}]); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.