-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[ResponseOps][Alerts] Add alerts grouping aggregations endpoint #186475
Merged
umbopepato
merged 27 commits into
elastic:main
from
umbopepato:186383-alerts-grouping-aggregations-endpoint
Jul 4, 2024
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ac35282
Add get alerts group aggregations endpoint
umbopepato 15f9aa7
Add alerts group aggregation endpoint tests
umbopepato 46fea59
Merge branch 'main' of github.com:elastic/kibana into 186383-alerts-g…
umbopepato a47b66f
Fix import
umbopepato e091401
Automatically add unitsCount subaggregation in groupByField aggregation
umbopepato ae8b482
Merge branch 'main' of github.com:elastic/kibana into 186383-alerts-g…
umbopepato 0529905
Make aggregations field optional, improve documentation
umbopepato 6f57233
Merge branch 'main' of github.com:elastic/kibana into 186383-alerts-g…
umbopepato e39bfa5
Merge branch 'main' of github.com:elastic/kibana into 186383-alerts-g…
umbopepato d848db0
Fix tests
umbopepato 46fb4fd
Add tests and improve AlertsClient.find errors handling
umbopepato eaa7bd6
Merge branch 'main' of github.com:elastic/kibana into 186383-alerts-g…
umbopepato 52485c3
Merge branch 'main' of github.com:elastic/kibana into 186383-alerts-g…
umbopepato e8b0329
Revert error response transformation
umbopepato de6c865
Use type instead of interface for validation
umbopepato 62194c9
Merge branch 'main' of github.com:elastic/kibana into 186383-alerts-g…
umbopepato 238e803
Restrict filters and pagination options validation
umbopepato 1c1a1fd
Merge branch 'main' of github.com:elastic/kibana into 186383-alerts-g…
umbopepato 0731496
Fix test
umbopepato ca88557
Merge branch 'main' of github.com:elastic/kibana into 186383-alerts-g…
umbopepato 8869584
Improve typings and validations
umbopepato 11ac1fc
Fix AlertsClient.find not returning result
umbopepato 6fc8aee
Merge branch 'main' of github.com:elastic/kibana into 186383-alerts-g…
umbopepato cb2e535
Move alert group aggregations pagination tests
umbopepato 90a86d1
Improve filters validation schema and searchAlerts control flow
umbopepato 39e0ae2
Merge branch 'main' of github.com:elastic/kibana into 186383-alerts-g…
umbopepato c962e1f
Fix filter keys validation schema type assertion
umbopepato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
89 changes: 89 additions & 0 deletions
89
...es/kbn-alerts-ui-shared/src/common/hooks/use_get_alerts_group_aggregations_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,89 @@ | ||
/* | ||
* 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 from 'react'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { renderHook } from '@testing-library/react-hooks/dom'; | ||
import type { HttpStart } from '@kbn/core-http-browser'; | ||
import { ToastsStart } from '@kbn/core-notifications-browser'; | ||
import { AlertConsumers } from '@kbn/rule-data-utils'; | ||
|
||
import { useGetAlertsGroupAggregationsQuery } from './use_get_alerts_group_aggregations_query'; | ||
import { waitFor } from '@testing-library/react'; | ||
import { BASE_RAC_ALERTS_API_PATH } from '../constants'; | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: false, | ||
}, | ||
}, | ||
}); | ||
|
||
const wrapper = ({ children }: { children: Node }) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
|
||
const mockHttp = { | ||
post: jest.fn(), | ||
}; | ||
const http = mockHttp as unknown as HttpStart; | ||
|
||
const mockToasts = { | ||
addDanger: jest.fn(), | ||
}; | ||
const toasts = mockToasts as unknown as ToastsStart; | ||
|
||
const params = { | ||
featureIds: [AlertConsumers.STACK_ALERTS], | ||
groupByField: 'kibana.alert.rule.name', | ||
}; | ||
|
||
describe('useAlertsGroupAggregationsQuery', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('displays toast on errors', async () => { | ||
mockHttp.post.mockRejectedValue(new Error('An error occurred')); | ||
renderHook( | ||
() => | ||
useGetAlertsGroupAggregationsQuery({ | ||
params, | ||
enabled: true, | ||
toasts, | ||
http, | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => expect(mockToasts.addDanger).toHaveBeenCalled()); | ||
}); | ||
|
||
test('calls API endpoint with the correct body', async () => { | ||
renderHook( | ||
() => | ||
useGetAlertsGroupAggregationsQuery({ | ||
params, | ||
enabled: true, | ||
toasts, | ||
http, | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => | ||
expect(mockHttp.post).toHaveBeenLastCalledWith( | ||
`${BASE_RAC_ALERTS_API_PATH}/_group_aggregations`, | ||
{ | ||
body: JSON.stringify(params), | ||
} | ||
) | ||
); | ||
}); | ||
}); |
80 changes: 80 additions & 0 deletions
80
packages/kbn-alerts-ui-shared/src/common/hooks/use_get_alerts_group_aggregations_query.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,80 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import type { HttpStart } from '@kbn/core-http-browser'; | ||
import type { ToastsStart } from '@kbn/core-notifications-browser'; | ||
import { SearchResponseBody } from '@elastic/elasticsearch/lib/api/types'; | ||
import { AlertConsumers } from '@kbn/rule-data-utils'; | ||
import type { | ||
AggregationsAggregationContainer, | ||
QueryDslQueryContainer, | ||
SortCombinations, | ||
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
import { BASE_RAC_ALERTS_API_PATH } from '../constants'; | ||
|
||
export interface UseGetAlertsGroupAggregationsQueryProps { | ||
http: HttpStart; | ||
toasts: ToastsStart; | ||
enabled?: boolean; | ||
params: { | ||
featureIds: AlertConsumers[]; | ||
groupByField: string; | ||
aggregations?: Record<string, AggregationsAggregationContainer>; | ||
filters?: QueryDslQueryContainer[]; | ||
sort?: SortCombinations[]; | ||
pageIndex?: number; | ||
pageSize?: number; | ||
}; | ||
} | ||
|
||
/** | ||
* Fetches alerts aggregations for a given groupByField. | ||
* | ||
* Some default aggregations are applied: | ||
* - `groupByFields`, to get the buckets based on the provided grouping field, | ||
* - `unitsCount`, to count the number of alerts in each bucket, | ||
* - `unitsCount`, to count the total number of alerts targeted by the query, | ||
* - `groupsCount`, to count the total number of groups. | ||
* | ||
* The provided `aggregations` are applied within `groupByFields`. Here the `groupByField` runtime | ||
* field can be used to perform grouping-based aggregations. | ||
* | ||
* Applies alerting RBAC through featureIds. | ||
*/ | ||
export const useGetAlertsGroupAggregationsQuery = <T>({ | ||
http, | ||
toasts, | ||
enabled = true, | ||
params, | ||
}: UseGetAlertsGroupAggregationsQueryProps) => { | ||
const onErrorFn = (error: Error) => { | ||
if (error) { | ||
toasts.addDanger( | ||
i18n.translate( | ||
'alertsUIShared.hooks.useFindAlertsQuery.unableToFetchAlertsGroupingAggregations', | ||
{ | ||
defaultMessage: 'Unable to fetch alerts grouping aggregations', | ||
} | ||
) | ||
); | ||
} | ||
}; | ||
|
||
return useQuery({ | ||
queryKey: ['getAlertsGroupAggregations', JSON.stringify(params)], | ||
queryFn: () => | ||
http.post<SearchResponseBody<{}, T>>(`${BASE_RAC_ALERTS_API_PATH}/_group_aggregations`, { | ||
body: JSON.stringify(params), | ||
}), | ||
onError: onErrorFn, | ||
refetchOnWindowFocus: false, | ||
enabled, | ||
}); | ||
}; |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add tests for this in
packages/kbn-alerts-ui-shared/src/common/hooks/use_get_alerts_group_aggregations_query.test.ts
? Stuff likedisplays error toast if params are not JSON
orAPI gets called with the correct body.
.