From ed2626f980455b9215f8d1097e5b61838aa3e44c Mon Sep 17 00:00:00 2001 From: Maryam Saeidi Date: Thu, 7 Nov 2024 11:03:56 +0100 Subject: [PATCH] Fix getAlertSummary returning 400 (Bad Request) (#199116) Fixes #190126 Fixes #184649 ## Summary This PR fixes alert summary API returning 400 when there is no featured by only sending this request when featureId is available, otherwise showing a default state as shown below: #### Alert details page |With featureId | Without featureId (intermediate state)| |---|---| |![image](https://github.com/user-attachments/assets/d4b4be5a-43be-4365-bd28-66c9dd53d979)|![image](https://github.com/user-attachments/assets/be437052-7c94-4d89-9238-b63264a812c8)| #### Rule details page |With featureId | Without featureId| |---|---| |![image](https://github.com/user-attachments/assets/28ff4ac5-ffc7-4c9e-8087-408f2ccff98e)|![image](https://github.com/user-attachments/assets/b82f530e-a27c-44e5-a241-c179bc8f1f46)| --- .../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 ?? [];