-
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.
[AO] - Move the hook that fetches the alert annotations in the Alert …
…Details page to Observability (#157178) ## Summary Fixes #154170 by: - Make the `use_alerts_history` [hook](https://github.com/elastic/kibana/blob/main/x-pack/plugins/infra/public/hooks/use_alerts_history.ts) sharable: move it to the Observability package and updates the imports in `APM` and `Infra` to use it - Provided tests
- Loading branch information
Showing
10 changed files
with
334 additions
and
303 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
153 changes: 153 additions & 0 deletions
153
x-pack/packages/observability/alert_details/src/hooks/use_alerts_history.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,153 @@ | ||
/* | ||
* 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 { HttpSetup } from '@kbn/core-http-browser'; | ||
import { AlertConsumers } from '@kbn/rule-data-utils'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { | ||
type UseAlertsHistory, | ||
useAlertsHistory, | ||
type Props as useAlertsHistoryProps, | ||
} from './use_alerts_history'; | ||
|
||
const queryClient = new QueryClient({ | ||
logger: { | ||
log: () => {}, | ||
warn: () => {}, | ||
error: () => {}, | ||
}, | ||
defaultOptions: { | ||
queries: { retry: false, cacheTime: 0 }, | ||
}, | ||
}); | ||
const wrapper = ({ children }: any) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
|
||
describe('useAlertsHistory', () => { | ||
const start = '2023-04-10T00:00:00.000Z'; | ||
const end = '2023-05-10T00:00:00.000Z'; | ||
const ruleId = 'cfd36e60-ef22-11ed-91eb-b7893acacfe2'; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('returns no data with error when http client is not provided', async () => { | ||
const http = undefined; | ||
const { result, waitFor } = renderHook<useAlertsHistoryProps, UseAlertsHistory>( | ||
() => | ||
useAlertsHistory({ | ||
http, | ||
featureIds: [AlertConsumers.APM], | ||
ruleId, | ||
dateRange: { from: start, to: end }, | ||
}), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
await act(async () => { | ||
await waitFor(() => result.current.isError); | ||
}); | ||
expect(result.current.isError).toBeTruthy(); | ||
expect(result.current.isSuccess).toBeFalsy(); | ||
expect(result.current.isLoading).toBeFalsy(); | ||
}); | ||
it('returns no data when API error', async () => { | ||
const http = { | ||
post: jest.fn().mockImplementation(() => { | ||
throw new Error('ES error'); | ||
}), | ||
} as unknown as HttpSetup; | ||
const { result, waitFor } = renderHook<useAlertsHistoryProps, UseAlertsHistory>( | ||
() => | ||
useAlertsHistory({ | ||
http, | ||
featureIds: [AlertConsumers.APM], | ||
ruleId, | ||
dateRange: { from: start, to: end }, | ||
}), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
await act(async () => { | ||
await waitFor(() => result.current.isError); | ||
}); | ||
expect(result.current.isError).toBeTruthy(); | ||
expect(result.current.isSuccess).toBeFalsy(); | ||
expect(result.current.isLoading).toBeFalsy(); | ||
}); | ||
|
||
it('returns the alert history chart data', async () => { | ||
const http = { | ||
post: jest.fn().mockResolvedValue({ | ||
hits: { total: { value: 32, relation: 'eq' }, max_score: null, hits: [] }, | ||
aggregations: { | ||
avgTimeToRecoverUS: { doc_count: 28, recoveryTime: { value: 134959464.2857143 } }, | ||
histogramTriggeredAlerts: { | ||
buckets: [ | ||
{ key_as_string: '2023-04-10T00:00:00.000Z', key: 1681084800000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-11T00:00:00.000Z', key: 1681171200000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-12T00:00:00.000Z', key: 1681257600000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-13T00:00:00.000Z', key: 1681344000000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-14T00:00:00.000Z', key: 1681430400000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-15T00:00:00.000Z', key: 1681516800000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-16T00:00:00.000Z', key: 1681603200000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-17T00:00:00.000Z', key: 1681689600000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-18T00:00:00.000Z', key: 1681776000000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-19T00:00:00.000Z', key: 1681862400000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-20T00:00:00.000Z', key: 1681948800000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-21T00:00:00.000Z', key: 1682035200000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-22T00:00:00.000Z', key: 1682121600000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-23T00:00:00.000Z', key: 1682208000000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-24T00:00:00.000Z', key: 1682294400000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-25T00:00:00.000Z', key: 1682380800000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-26T00:00:00.000Z', key: 1682467200000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-27T00:00:00.000Z', key: 1682553600000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-28T00:00:00.000Z', key: 1682640000000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-29T00:00:00.000Z', key: 1682726400000, doc_count: 0 }, | ||
{ key_as_string: '2023-04-30T00:00:00.000Z', key: 1682812800000, doc_count: 0 }, | ||
{ key_as_string: '2023-05-01T00:00:00.000Z', key: 1682899200000, doc_count: 0 }, | ||
{ key_as_string: '2023-05-02T00:00:00.000Z', key: 1682985600000, doc_count: 0 }, | ||
{ key_as_string: '2023-05-03T00:00:00.000Z', key: 1683072000000, doc_count: 0 }, | ||
{ key_as_string: '2023-05-04T00:00:00.000Z', key: 1683158400000, doc_count: 0 }, | ||
{ key_as_string: '2023-05-05T00:00:00.000Z', key: 1683244800000, doc_count: 0 }, | ||
{ key_as_string: '2023-05-06T00:00:00.000Z', key: 1683331200000, doc_count: 0 }, | ||
{ key_as_string: '2023-05-07T00:00:00.000Z', key: 1683417600000, doc_count: 0 }, | ||
{ key_as_string: '2023-05-08T00:00:00.000Z', key: 1683504000000, doc_count: 0 }, | ||
{ key_as_string: '2023-05-09T00:00:00.000Z', key: 1683590400000, doc_count: 0 }, | ||
{ key_as_string: '2023-05-10T00:00:00.000Z', key: 1683676800000, doc_count: 32 }, | ||
], | ||
}, | ||
}, | ||
}), | ||
} as unknown as HttpSetup; | ||
const { result, waitFor } = renderHook<useAlertsHistoryProps, UseAlertsHistory>( | ||
() => | ||
useAlertsHistory({ | ||
http, | ||
featureIds: [AlertConsumers.APM], | ||
ruleId, | ||
dateRange: { from: start, to: end }, | ||
}), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
await act(async () => { | ||
await waitFor(() => result.current.isSuccess); | ||
}); | ||
expect(result.current.isLoading).toBeFalsy(); | ||
expect(result.current.isError).toBeFalsy(); | ||
expect(result.current.data.avgTimeToRecoverUS).toEqual(134959464.2857143); | ||
expect(result.current.data.histogramTriggeredAlerts?.length).toEqual(31); | ||
expect(result.current.data.totalTriggeredAlerts).toEqual(32); | ||
}); | ||
}); |
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
Oops, something went wrong.