From b3d58315749a2cdbd382b5a7b9f010fa30c2d283 Mon Sep 17 00:00:00 2001 From: Vikrant Gupta Date: Wed, 1 May 2024 14:39:39 +0530 Subject: [PATCH] fix: ch queries sending builder as query type in query range api for exceptions alerts (#4941) * fix: ch queries sending builder as query type in query range api for exceptions alerts * fix: ch queries sending builder as query type in query range api for exceptions alerts * fix: alerts routing from logs explorer and dashboards --- frontend/src/constants/query.ts | 1 + .../src/container/CreateAlertRule/index.tsx | 33 ++++++++++++------- .../src/container/FormAlertRules/index.tsx | 1 + 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/frontend/src/constants/query.ts b/frontend/src/constants/query.ts index 6b70ae9786..3759fb26b6 100644 --- a/frontend/src/constants/query.ts +++ b/frontend/src/constants/query.ts @@ -30,4 +30,5 @@ export enum QueryParams { integration = 'integration', pagination = 'pagination', relativeTime = 'relativeTime', + alertType = 'alertType', } diff --git a/frontend/src/container/CreateAlertRule/index.tsx b/frontend/src/container/CreateAlertRule/index.tsx index 5a98cd2d8e..e5a4772f30 100644 --- a/frontend/src/container/CreateAlertRule/index.tsx +++ b/frontend/src/container/CreateAlertRule/index.tsx @@ -1,8 +1,9 @@ import { Form, Row } from 'antd'; import { ENTITY_VERSION_V4 } from 'constants/app'; +import { QueryParams } from 'constants/query'; import FormAlertRules from 'container/FormAlertRules'; import { useGetCompositeQueryParam } from 'hooks/queryBuilder/useGetCompositeQueryParam'; -import { isEqual } from 'lodash-es'; +import history from 'lib/history'; import { useEffect, useState } from 'react'; import { useLocation } from 'react-router-dom'; import { AlertTypes } from 'types/api/alerts/alertTypes'; @@ -19,13 +20,25 @@ import SelectAlertType from './SelectAlertType'; function CreateRules(): JSX.Element { const [initValues, setInitValues] = useState(null); - const [alertType, setAlertType] = useState(); const location = useLocation(); const queryParams = new URLSearchParams(location.search); const version = queryParams.get('version'); + const alertTypeFromParams = queryParams.get(QueryParams.alertType); const compositeQuery = useGetCompositeQueryParam(); + function getAlertTypeFromDataSource(): AlertTypes | null { + if (!compositeQuery) { + return null; + } + const dataSource = compositeQuery?.builder?.queryData[0]?.dataSource; + + return ALERT_TYPE_VS_SOURCE_MAPPING[dataSource]; + } + + const [alertType, setAlertType] = useState( + (alertTypeFromParams as AlertTypes) || getAlertTypeFromDataSource(), + ); const [formInstance] = Form.useForm(); @@ -47,21 +60,17 @@ function CreateRules(): JSX.Element { version: version || ENTITY_VERSION_V4, }); } + queryParams.set(QueryParams.alertType, typ); + const generatedUrl = `${location.pathname}?${queryParams.toString()}`; + history.replace(generatedUrl); }; useEffect(() => { - if (!compositeQuery) { - return; - } - const dataSource = compositeQuery?.builder?.queryData[0]?.dataSource; - - const alertTypeFromQuery = ALERT_TYPE_VS_SOURCE_MAPPING[dataSource]; - - if (alertTypeFromQuery && !isEqual(alertType, alertTypeFromQuery)) { - onSelectType(alertTypeFromQuery); + if (alertType) { + onSelectType(alertType); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [compositeQuery]); + }, [alertType]); if (!initValues) { return ( diff --git a/frontend/src/container/FormAlertRules/index.tsx b/frontend/src/container/FormAlertRules/index.tsx index cb5bd1004e..c5d0356cb0 100644 --- a/frontend/src/container/FormAlertRules/index.tsx +++ b/frontend/src/container/FormAlertRules/index.tsx @@ -523,6 +523,7 @@ function FormAlertRules({ runQuery={handleRunQuery} alertDef={alertDef} panelType={panelType || PANEL_TYPES.TIME_SERIES} + key={currentQuery.queryType} />