-
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.
Merge remote-tracking branch 'upstream/main' into billing-ftr
- Loading branch information
Showing
97 changed files
with
3,108 additions
and
1,262 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 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 { SerializableRecord } from '@kbn/utility-types'; | ||
|
||
export const uptimeOverviewLocatorID = 'UPTIME_OVERVIEW_LOCATOR'; | ||
|
||
export interface UptimeOverviewLocatorInfraParams extends SerializableRecord { | ||
ip?: string; | ||
host?: string; | ||
container?: string; | ||
pod?: string; | ||
} | ||
|
||
export interface UptimeOverviewLocatorParams extends SerializableRecord { | ||
dateRangeStart?: string; | ||
dateRangeEnd?: string; | ||
search?: string; | ||
} |
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
Oops, something went wrong.