diff --git a/frontend/src/constants/reactQueryKeys.ts b/frontend/src/constants/reactQueryKeys.ts index 7cc38c153e..920bba6e7b 100644 --- a/frontend/src/constants/reactQueryKeys.ts +++ b/frontend/src/constants/reactQueryKeys.ts @@ -1,3 +1,4 @@ export const REACT_QUERY_KEY = { GET_ALL_LICENCES: 'GET_ALL_LICENCES', + GET_QUERY_RANGE: 'GET_QUERY_RANGE', }; diff --git a/frontend/src/container/FormAlertRules/ChartPreview/index.tsx b/frontend/src/container/FormAlertRules/ChartPreview/index.tsx index e1d2dcf220..2b10756266 100644 --- a/frontend/src/container/FormAlertRules/ChartPreview/index.tsx +++ b/frontend/src/container/FormAlertRules/ChartPreview/index.tsx @@ -6,11 +6,10 @@ import GridGraphComponent from 'container/GridGraphComponent'; import { GRAPH_TYPES } from 'container/NewDashboard/ComponentsSlider'; import { timePreferenceType } from 'container/NewWidget/RightContainer/timeItems'; import { Time } from 'container/TopNav/DateTimeSelection/config'; +import { useGetQueryRange } from 'hooks/queryBuilder/useGetQueryRange'; import getChartData from 'lib/getChartData'; import { useMemo } from 'react'; import { useTranslation } from 'react-i18next'; -import { useQuery } from 'react-query'; -import { GetMetricQueryRange } from 'store/actions/dashboard/getQueryResults'; import { Query } from 'types/api/queryBuilder/queryBuilderData'; import { EQueryType } from 'types/common/dashboard'; @@ -26,9 +25,6 @@ export interface ChartPreviewProps { threshold?: number | undefined; userQueryKey?: string; } -interface QueryResponseError { - message?: string; -} function ChartPreview({ name, @@ -76,39 +72,38 @@ function ChartPreview({ } }, [query]); - const queryResponse = useQuery({ - queryKey: [ - 'chartPreview', - userQueryKey || JSON.stringify(query), - selectedInterval, - ], - queryFn: () => - GetMetricQueryRange({ - query: query || { - queryType: EQueryType.QUERY_BUILDER, - promql: [], - builder: { - queryFormulas: [], - queryData: [], - }, - clickhouse_sql: [], + const queryResponse = useGetQueryRange( + { + query: query || { + queryType: EQueryType.QUERY_BUILDER, + promql: [], + builder: { + queryFormulas: [], + queryData: [], }, - globalSelectedInterval: selectedInterval, - graphType, - selectedTime, - }), - retry: false, - enabled: canQuery, - }); + clickhouse_sql: [], + }, + globalSelectedInterval: selectedInterval, + graphType, + selectedTime, + }, + { + queryKey: [ + 'chartPreview', + userQueryKey || JSON.stringify(query), + selectedInterval, + ], + retry: false, + enabled: canQuery, + }, + ); const chartDataSet = queryResponse.isError ? null : getChartData({ queryData: [ { - queryData: queryResponse?.data?.payload?.data?.result - ? queryResponse?.data?.payload?.data?.result - : [], + queryData: queryResponse?.data?.payload?.data?.result ?? [], }, ], }); @@ -119,8 +114,7 @@ function ChartPreview({ {(queryResponse?.isError || queryResponse?.error) && ( {' '} - {(queryResponse?.error as QueryResponseError).message || - t('preview_chart_unexpected_error')} + {queryResponse.error.message || t('preview_chart_unexpected_error')} )} {queryResponse.isLoading && } diff --git a/frontend/src/container/GridGraphLayout/Graph/FullView/index.metricsBuilder.tsx b/frontend/src/container/GridGraphLayout/Graph/FullView/index.metricsBuilder.tsx index 04b35ee83b..1f5759f34b 100644 --- a/frontend/src/container/GridGraphLayout/Graph/FullView/index.metricsBuilder.tsx +++ b/frontend/src/container/GridGraphLayout/Graph/FullView/index.metricsBuilder.tsx @@ -7,16 +7,13 @@ import { timeItems, timePreferance, } from 'container/NewWidget/RightContainer/timeItems'; +import { useGetQueryRange } from 'hooks/queryBuilder/useGetQueryRange'; import { getDashboardVariables } from 'lib/dashbaordVariables/getDashboardVariables'; import getChartData from 'lib/getChartData'; import { useCallback, useMemo, useState } from 'react'; -import { useQuery } from 'react-query'; import { useSelector } from 'react-redux'; -import { GetMetricQueryRange } from 'store/actions/dashboard/getQueryResults'; import { AppState } from 'store/reducers'; -import { ErrorResponse, SuccessResponse } from 'types/api'; import { Widgets } from 'types/api/dashboard/getAll'; -import { MetricRangePayloadProps } from 'types/api/metrics/getQueryRange'; import { GlobalReducer } from 'types/reducer/globalTime'; import { TimeContainer } from './styles'; @@ -44,18 +41,24 @@ function FullView({ name: getSelectedTime()?.name || '', enum: widget?.timePreferance || 'GLOBAL_TIME', }); - const response = useQuery< - SuccessResponse | ErrorResponse - >( - `FullViewGetMetricsQueryRange-${selectedTime.enum}-${globalSelectedTime}-${widget.id}`, + + const queryKey = useMemo( () => - GetMetricQueryRange({ - selectedTime: selectedTime.enum, - graphType: widget.panelTypes, - query: widget.query, - globalSelectedInterval: globalSelectedTime, - variables: getDashboardVariables(), - }), + `FullViewGetMetricsQueryRange-${selectedTime.enum}-${globalSelectedTime}-${widget.id}`, + [selectedTime, globalSelectedTime, widget], + ); + + const response = useGetQueryRange( + { + selectedTime: selectedTime.enum, + graphType: widget.panelTypes, + query: widget.query, + globalSelectedInterval: globalSelectedTime, + variables: getDashboardVariables(), + }, + { + queryKey, + }, ); const chartDataSet = useMemo( diff --git a/frontend/src/container/GridGraphLayout/Graph/index.tsx b/frontend/src/container/GridGraphLayout/Graph/index.tsx index c581f7f917..29390c7036 100644 --- a/frontend/src/container/GridGraphLayout/Graph/index.tsx +++ b/frontend/src/container/GridGraphLayout/Graph/index.tsx @@ -3,6 +3,7 @@ import { ChartData } from 'chart.js'; import Spinner from 'components/Spinner'; import GridGraphComponent from 'container/GridGraphComponent'; import { UpdateDashboard } from 'container/GridGraphLayout/utils'; +import { useGetQueryRange } from 'hooks/queryBuilder/useGetQueryRange'; import { useNotifications } from 'hooks/useNotifications'; import usePreviousValue from 'hooks/usePreviousValue'; import { getDashboardVariables } from 'lib/dashbaordVariables/getDashboardVariables'; @@ -20,7 +21,6 @@ import { import { Layout } from 'react-grid-layout'; import { useTranslation } from 'react-i18next'; import { useInView } from 'react-intersection-observer'; -import { useQuery } from 'react-query'; import { connect, useSelector } from 'react-redux'; import { bindActionCreators } from 'redux'; import { ThunkDispatch } from 'redux-thunk'; @@ -28,7 +28,6 @@ import { DeleteWidget, DeleteWidgetProps, } from 'store/actions/dashboard/deleteWidget'; -import { GetMetricQueryRange } from 'store/actions/dashboard/getQueryResults'; import { AppState } from 'store/reducers'; import AppActions from 'types/actions'; import { Widgets } from 'types/api/dashboard/getAll'; @@ -81,33 +80,28 @@ function GridCardGraph({ const selectedData = selectedDashboard?.data; const { variables } = selectedData; - const queryResponse = useQuery( - [ - `GetMetricsQueryRange-${widget?.timePreferance}-${globalSelectedInterval}-${widget.id}`, - { + const queryResponse = useGetQueryRange( + { + selectedTime: widget?.timePreferance, + graphType: widget.panelTypes, + query: widget.query, + globalSelectedInterval, + variables: getDashboardVariables(), + }, + { + queryKey: [ + `GetMetricsQueryRange-${widget?.timePreferance}-${globalSelectedInterval}-${widget.id}`, widget, maxTime, minTime, globalSelectedInterval, variables, - }, - ], - () => - GetMetricQueryRange({ - selectedTime: widget?.timePreferance, - graphType: widget.panelTypes, - query: widget.query, - globalSelectedInterval, - variables: getDashboardVariables(), - }), - { + ], keepPreviousData: true, enabled: isGraphVisible, refetchOnMount: false, onError: (error) => { - if (error instanceof Error) { - setErrorMessage(error.message); - } + setErrorMessage(error.message); }, }, ); diff --git a/frontend/src/container/GridGraphLayout/utils.ts b/frontend/src/container/GridGraphLayout/utils.ts index 625557173f..1cef8eed7c 100644 --- a/frontend/src/container/GridGraphLayout/utils.ts +++ b/frontend/src/container/GridGraphLayout/utils.ts @@ -50,14 +50,6 @@ export const UpdateDashboard = async ( queryData: [initialQueryBuilderFormValues], }, }, - queryData: { - data: { - queryData: widgetData?.queryData.data.queryData || [], - }, - error: false, - errorMessage: '', - loading: false, - }, timePreferance: widgetData?.timePreferance || 'GLOBAL_TIME', title: widgetData ? copyTitle : '', }, diff --git a/frontend/src/container/ListOfDashboard/ImportJSON/index.tsx b/frontend/src/container/ListOfDashboard/ImportJSON/index.tsx index b737f7d0f9..3a7f406b4e 100644 --- a/frontend/src/container/ListOfDashboard/ImportJSON/index.tsx +++ b/frontend/src/container/ListOfDashboard/ImportJSON/index.tsx @@ -71,23 +71,8 @@ function ImportJSON({ setDashboardCreating(true); const dashboardData = JSON.parse(editorValue) as DashboardData; - // removing the queryData - const parsedWidgets: DashboardData = { - ...dashboardData, - widgets: dashboardData.widgets?.map((e) => ({ - ...e, - queryData: { - ...e.queryData, - data: e.queryData.data, - error: false, - errorMessage: '', - loading: false, - }, - })), - }; - const response = await createDashboard({ - ...parsedWidgets, + ...dashboardData, uploadedGrafana, }); diff --git a/frontend/src/container/MetricsApplication/MetricsApplication.factory.ts b/frontend/src/container/MetricsApplication/MetricsApplication.factory.ts index 82b3856f98..366dc864a6 100644 --- a/frontend/src/container/MetricsApplication/MetricsApplication.factory.ts +++ b/frontend/src/container/MetricsApplication/MetricsApplication.factory.ts @@ -10,12 +10,6 @@ export const getWidgetQueryBuilder = (query: Widgets['query']): Widgets => ({ opacity: '0', panelTypes: PANEL_TYPES.TIME_SERIES, query, - queryData: { - data: { queryData: [] }, - error: false, - errorMessage: '', - loading: false, - }, timePreferance: 'GLOBAL_TIME', title: '', }); diff --git a/frontend/src/container/NewDashboard/DescriptionOfDashboard/ShareModal.tsx b/frontend/src/container/NewDashboard/DescriptionOfDashboard/ShareModal.tsx index 51d83b23a3..d7ccd451e4 100644 --- a/frontend/src/container/NewDashboard/DescriptionOfDashboard/ShareModal.tsx +++ b/frontend/src/container/NewDashboard/DescriptionOfDashboard/ShareModal.tsx @@ -6,28 +6,14 @@ import { useTranslation } from 'react-i18next'; import { useCopyToClipboard } from 'react-use'; import { DashboardData } from 'types/api/dashboard/getAll'; -import { cleardQueryData, downloadObjectAsJson } from './util'; +import { downloadObjectAsJson } from './util'; function ShareModal({ isJSONModalVisible, onToggleHandler, selectedData, }: ShareModalProps): JSX.Element { - const getParsedValue = (): string => { - const updatedData: DashboardData = { - ...selectedData, - widgets: selectedData.widgets?.map((widget) => ({ - ...widget, - queryData: { - ...widget.queryData, - loading: false, - error: false, - errorMessage: '', - }, - })), - }; - return JSON.stringify(updatedData, null, 2); - }; + const getParsedValue = (): string => JSON.stringify(selectedData, null, 2); const [jsonValue, setJSONValue] = useState(getParsedValue()); const [isViewJSON, setIsViewJSON] = useState(false); @@ -53,7 +39,6 @@ function ShareModal({ } }, [state.error, state.value, t, notifications]); - const selectedDataCleaned = cleardQueryData(selectedData); const GetFooterComponent = useMemo(() => { if (!isViewJSON) { return ( @@ -69,7 +54,7 @@ function ShareModal({ ); - }, [isViewJSON, jsonValue, selectedData, selectedDataCleaned, setCopy, t]); + }, [isViewJSON, jsonValue, selectedData, setCopy, t]); return ( ({ - ...widget, - queryData: { - ...widget.queryData, - data: { - queryData: [], - }, - }, - })), - }; -} diff --git a/frontend/src/container/NewWidget/LeftContainer/QuerySection/index.tsx b/frontend/src/container/NewWidget/LeftContainer/QuerySection/index.tsx index 1d8cbd763b..655ecaaee7 100644 --- a/frontend/src/container/NewWidget/LeftContainer/QuerySection/index.tsx +++ b/frontend/src/container/NewWidget/LeftContainer/QuerySection/index.tsx @@ -1,11 +1,13 @@ import { Button, Tabs, Typography } from 'antd'; import TextToolTip from 'components/TextToolTip'; import { GRAPH_TYPES } from 'container/NewDashboard/ComponentsSlider'; +import { WidgetGraphProps } from 'container/NewWidget/types'; import { QueryBuilder } from 'container/QueryBuilder'; +import { useGetWidgetQueryRange } from 'hooks/queryBuilder/useGetWidgetQueryRange'; import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder'; import { useShareBuilderUrl } from 'hooks/queryBuilder/useShareBuilderUrl'; import useUrlQuery from 'hooks/useUrlQuery'; -import { useCallback, useEffect, useState } from 'react'; +import { useCallback } from 'react'; import { connect, useSelector } from 'react-redux'; import { bindActionCreators, Dispatch } from 'redux'; import { ThunkDispatch } from 'redux-thunk'; @@ -23,16 +25,22 @@ import DashboardReducer from 'types/reducer/dashboards'; import ClickHouseQueryContainer from './QueryBuilder/clickHouse'; import PromQLQueryContainer from './QueryBuilder/promQL'; -function QuerySection({ updateQuery, selectedGraph }: QueryProps): JSX.Element { +function QuerySection({ + updateQuery, + selectedGraph, + selectedTime, +}: QueryProps): JSX.Element { const { currentQuery, redirectWithQueryBuilderData } = useQueryBuilder(); const urlQuery = useUrlQuery(); - const [isInit, setIsInit] = useState(false); + const { dashboards } = useSelector( + (state) => state.dashboards, + ); - const { dashboards, isLoadingQueryResult } = useSelector< - AppState, - DashboardReducer - >((state) => state.dashboards); + const getWidgetQueryRange = useGetWidgetQueryRange({ + graphType: selectedGraph, + selectedTime: selectedTime.enum, + }); const [selectedDashboards] = dashboards; const { widgets } = selectedDashboards.data; @@ -46,23 +54,11 @@ function QuerySection({ updateQuery, selectedGraph }: QueryProps): JSX.Element { const { query } = selectedWidget; - const { compositeQuery } = useShareBuilderUrl({ defaultValue: query }); - - useEffect(() => { - if (!isInit && compositeQuery) { - setIsInit(true); - updateQuery({ - updatedQuery: compositeQuery, - widgetId: urlQuery.get('widgetId') || '', - yAxisUnit: selectedWidget.yAxisUnit, - }); - } - }, [isInit, compositeQuery, selectedWidget, urlQuery, updateQuery]); + useShareBuilderUrl({ defaultValue: query }); const handleStageQuery = useCallback( (updatedQuery: Query): void => { updateQuery({ - updatedQuery, widgetId: urlQuery.get('widgetId') || '', yAxisUnit: selectedWidget.yAxisUnit, }); @@ -115,7 +111,7 @@ function QuerySection({ updateQuery, selectedGraph }: QueryProps): JSX.Element {