From ce0e63b2b9255dcbf11f321714e29ca3ff1dad1e Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 7 Nov 2024 22:56:01 +1100 Subject: [PATCH] [8.x] Fix getAlertSummary returning 400 (Bad Request) (#199116) (#199281) # Backport This will backport the following commits from `main` to `8.x`: - [Fix getAlertSummary returning 400 (Bad Request) (#199116)](https://github.com/elastic/kibana/pull/199116) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Maryam Saeidi --- .../src/hooks/use_alerts_history.ts | 4 +++- .../hooks/use_load_alert_summary.ts | 22 ++++++++++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/x-pack/packages/observability/alert_details/src/hooks/use_alerts_history.ts b/x-pack/packages/observability/alert_details/src/hooks/use_alerts_history.ts index 7519fea5f99f8..193acb63f845b 100644 --- a/x-pack/packages/observability/alert_details/src/hooks/use_alerts_history.ts +++ b/x-pack/packages/observability/alert_details/src/hooks/use_alerts_history.ts @@ -55,6 +55,7 @@ export function useAlertsHistory({ http, instanceId, }: Props): UseAlertsHistory { + const enabled = !!featureIds.length; const { isInitialLoading, isLoading, isError, isSuccess, isRefetching, data } = useQuery({ queryKey: ['useAlertsHistory'], queryFn: async ({ signal }) => { @@ -71,10 +72,11 @@ export function useAlertsHistory({ }); }, refetchOnWindowFocus: false, + enabled, }); return { data: isInitialLoading ? EMPTY_ALERTS_HISTORY : data ?? EMPTY_ALERTS_HISTORY, - isLoading: isInitialLoading || isLoading || isRefetching, + isLoading: enabled && (isInitialLoading || isLoading || isRefetching), isSuccess, isError, }; diff --git a/x-pack/plugins/triggers_actions_ui/public/application/hooks/use_load_alert_summary.ts b/x-pack/plugins/triggers_actions_ui/public/application/hooks/use_load_alert_summary.ts index 50fcc6025938a..10ce201885098 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/hooks/use_load_alert_summary.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/hooks/use_load_alert_summary.ts @@ -102,16 +102,18 @@ async function fetchAlertSummary({ timeRange: AlertSummaryTimeRange; filter?: estypes.QueryDslQueryContainer; }): Promise { - const res = await http.post>(`${BASE_RAC_ALERTS_API_PATH}/_alert_summary`, { - signal, - body: JSON.stringify({ - fixed_interval: fixedInterval, - gte: utcFrom, - lte: utcTo, - featureIds, - filter: [filter], - }), - }); + const res = featureIds.length + ? await http.post>(`${BASE_RAC_ALERTS_API_PATH}/_alert_summary`, { + signal, + body: JSON.stringify({ + fixed_interval: fixedInterval, + gte: utcFrom, + lte: utcTo, + featureIds, + filter: [filter], + }), + }) + : {}; const activeAlertCount = res?.activeAlertCount ?? 0; const activeAlerts = res?.activeAlerts ?? [];