2023-01-25 20:31:42 +05:30
|
|
|
import { ChartData } from 'chart.js';
|
2021-09-23 15:43:43 +05:30
|
|
|
import Spinner from 'components/Spinner';
|
2023-06-07 15:27:33 +03:00
|
|
|
import { useGetQueryRange } from 'hooks/queryBuilder/useGetQueryRange';
|
2023-06-28 15:19:52 +05:30
|
|
|
import { useStepInterval } from 'hooks/queryBuilder/useStepInterval';
|
2023-01-25 20:31:42 +05:30
|
|
|
import usePreviousValue from 'hooks/usePreviousValue';
|
2022-09-09 17:43:25 +05:30
|
|
|
import { getDashboardVariables } from 'lib/dashbaordVariables/getDashboardVariables';
|
2021-09-23 15:43:43 +05:30
|
|
|
import getChartData from 'lib/getChartData';
|
2022-06-07 16:14:49 +05:30
|
|
|
import isEmpty from 'lodash-es/isEmpty';
|
2023-08-02 20:41:09 +05:30
|
|
|
import { memo, useMemo, useState } from 'react';
|
2023-01-30 18:32:05 +05:30
|
|
|
import { useInView } from 'react-intersection-observer';
|
2023-08-02 20:41:09 +05:30
|
|
|
import { useSelector } from 'react-redux';
|
2021-09-23 15:43:43 +05:30
|
|
|
import { AppState } from 'store/reducers';
|
2023-01-25 10:54:36 +05:30
|
|
|
import DashboardReducer from 'types/reducer/dashboards';
|
2022-06-24 15:00:21 +05:30
|
|
|
import { GlobalReducer } from 'types/reducer/globalTime';
|
2023-08-02 20:41:09 +05:30
|
|
|
import { getSelectedDashboardVariable } from 'utils/dashboard/selectedDashboard';
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2022-06-07 16:14:49 +05:30
|
|
|
import EmptyWidget from '../EmptyWidget';
|
2023-08-10 11:44:36 +05:30
|
|
|
import { MenuItemKeys } from '../WidgetHeader/contants';
|
2023-08-02 20:41:09 +05:30
|
|
|
import { GridCardGraphProps } from './types';
|
|
|
|
|
import WidgetGraphComponent from './WidgetGraphComponent';
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2022-03-22 12:10:31 +05:30
|
|
|
function GridCardGraph({
|
2021-09-23 15:43:43 +05:30
|
|
|
widget,
|
2021-12-10 14:28:31 +05:30
|
|
|
name,
|
2022-03-22 12:10:31 +05:30
|
|
|
yAxisUnit,
|
2022-06-07 16:14:49 +05:30
|
|
|
layout = [],
|
|
|
|
|
setLayout,
|
2023-01-17 13:30:34 +02:00
|
|
|
onDragSelect,
|
2023-07-18 08:55:01 +05:30
|
|
|
onClickHandler,
|
2023-08-10 11:44:36 +05:30
|
|
|
headerMenuList = [MenuItemKeys.View],
|
2023-07-21 16:17:31 +05:30
|
|
|
isQueryEnabled,
|
2023-08-16 12:18:56 +05:30
|
|
|
threshold,
|
2022-03-22 12:10:31 +05:30
|
|
|
}: GridCardGraphProps): JSX.Element {
|
2023-08-02 08:22:33 +03:00
|
|
|
const { isAddWidget } = useSelector<AppState, DashboardReducer>(
|
|
|
|
|
(state) => state.dashboards,
|
|
|
|
|
);
|
|
|
|
|
|
2023-01-30 18:32:05 +05:30
|
|
|
const { ref: graphRef, inView: isGraphVisible } = useInView({
|
|
|
|
|
threshold: 0,
|
|
|
|
|
triggerOnce: true,
|
2023-06-20 09:07:19 +05:30
|
|
|
initialInView: false,
|
2023-01-30 18:32:05 +05:30
|
|
|
});
|
|
|
|
|
|
2023-01-25 20:31:42 +05:30
|
|
|
const [errorMessage, setErrorMessage] = useState<string | undefined>('');
|
2022-06-24 15:00:21 +05:30
|
|
|
|
2023-05-19 12:19:42 +05:30
|
|
|
const { minTime, maxTime, selectedTime: globalSelectedInterval } = useSelector<
|
2022-06-24 15:00:21 +05:30
|
|
|
AppState,
|
|
|
|
|
GlobalReducer
|
|
|
|
|
>((state) => state.globalTime);
|
2023-01-25 10:54:36 +05:30
|
|
|
const { dashboards } = useSelector<AppState, DashboardReducer>(
|
|
|
|
|
(state) => state.dashboards,
|
|
|
|
|
);
|
2023-07-18 08:55:01 +05:30
|
|
|
|
|
|
|
|
const variables = getSelectedDashboardVariable(dashboards);
|
2022-06-07 16:14:49 +05:30
|
|
|
|
2023-06-28 15:19:52 +05:30
|
|
|
const updatedQuery = useStepInterval(widget?.query);
|
|
|
|
|
|
2023-07-10 16:34:21 +03:00
|
|
|
const isEmptyWidget = useMemo(
|
|
|
|
|
() => widget?.id === 'empty' || isEmpty(widget),
|
|
|
|
|
[widget],
|
|
|
|
|
);
|
|
|
|
|
|
2023-06-07 15:27:33 +03:00
|
|
|
const queryResponse = useGetQueryRange(
|
|
|
|
|
{
|
|
|
|
|
selectedTime: widget?.timePreferance,
|
2023-06-15 14:13:58 +05:30
|
|
|
graphType: widget?.panelTypes,
|
2023-06-28 15:19:52 +05:30
|
|
|
query: updatedQuery,
|
2023-06-07 15:27:33 +03:00
|
|
|
globalSelectedInterval,
|
|
|
|
|
variables: getDashboardVariables(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
queryKey: [
|
2023-06-15 14:13:58 +05:30
|
|
|
`GetMetricsQueryRange-${widget?.timePreferance}-${globalSelectedInterval}-${widget?.id}`,
|
2023-01-25 10:54:36 +05:30
|
|
|
maxTime,
|
|
|
|
|
minTime,
|
|
|
|
|
globalSelectedInterval,
|
|
|
|
|
variables,
|
2023-08-16 12:18:56 +05:30
|
|
|
widget?.query,
|
|
|
|
|
widget?.panelTypes,
|
2023-06-07 15:27:33 +03:00
|
|
|
],
|
2023-01-23 20:21:24 +05:30
|
|
|
keepPreviousData: true,
|
2023-08-02 08:22:33 +03:00
|
|
|
enabled: isGraphVisible && !isEmptyWidget && isQueryEnabled && !isAddWidget,
|
2023-01-23 20:21:24 +05:30
|
|
|
refetchOnMount: false,
|
2023-01-25 20:31:42 +05:30
|
|
|
onError: (error) => {
|
2023-06-07 15:27:33 +03:00
|
|
|
setErrorMessage(error.message);
|
2023-01-25 20:31:42 +05:30
|
|
|
},
|
2023-01-23 20:21:24 +05:30
|
|
|
},
|
|
|
|
|
);
|
2022-06-24 15:00:21 +05:30
|
|
|
|
2023-01-23 20:21:24 +05:30
|
|
|
const chartData = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
getChartData({
|
|
|
|
|
queryData: [
|
|
|
|
|
{
|
2023-01-25 20:31:42 +05:30
|
|
|
queryData: queryResponse?.data?.payload?.data?.result || [],
|
2023-01-23 20:21:24 +05:30
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}),
|
2023-01-25 20:31:42 +05:30
|
|
|
[queryResponse],
|
2023-01-23 20:21:24 +05:30
|
|
|
);
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2023-01-25 20:31:42 +05:30
|
|
|
const prevChartDataSetRef = usePreviousValue<ChartData>(chartData);
|
|
|
|
|
|
2022-06-07 16:14:49 +05:30
|
|
|
const isEmptyLayout = widget?.id === 'empty' || isEmpty(widget);
|
|
|
|
|
|
2023-08-11 15:17:32 +05:30
|
|
|
if (queryResponse.isRefetching || queryResponse.isLoading) {
|
2023-08-02 20:41:09 +05:30
|
|
|
return <Spinner height="20vh" tip="Loading..." />;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 12:18:56 +05:30
|
|
|
if ((queryResponse.isError && !isEmptyLayout) || !isQueryEnabled) {
|
2022-02-10 22:20:31 +05:30
|
|
|
return (
|
2023-01-30 18:32:05 +05:30
|
|
|
<span ref={graphRef}>
|
2023-01-25 20:31:42 +05:30
|
|
|
{!isEmpty(widget) && prevChartDataSetRef && (
|
2023-08-02 20:41:09 +05:30
|
|
|
<WidgetGraphComponent
|
|
|
|
|
enableModel
|
|
|
|
|
enableWidgetHeader
|
|
|
|
|
widget={widget}
|
|
|
|
|
queryResponse={queryResponse}
|
|
|
|
|
errorMessage={errorMessage}
|
|
|
|
|
data={prevChartDataSetRef}
|
|
|
|
|
name={name}
|
|
|
|
|
yAxisUnit={yAxisUnit}
|
|
|
|
|
layout={layout}
|
|
|
|
|
setLayout={setLayout}
|
2023-08-16 12:18:56 +05:30
|
|
|
threshold={threshold}
|
2023-08-10 11:44:36 +05:30
|
|
|
headerMenuList={headerMenuList}
|
2023-08-02 20:41:09 +05:30
|
|
|
/>
|
2023-01-25 20:31:42 +05:30
|
|
|
)}
|
|
|
|
|
</span>
|
2022-02-10 22:20:31 +05:30
|
|
|
);
|
2021-09-23 15:43:43 +05:30
|
|
|
}
|
|
|
|
|
|
2023-08-11 15:17:32 +05:30
|
|
|
if (!isEmpty(widget) && prevChartDataSetRef?.labels) {
|
2023-01-17 03:07:30 -03:00
|
|
|
return (
|
2023-01-30 18:32:05 +05:30
|
|
|
<span ref={graphRef}>
|
2023-08-11 15:17:32 +05:30
|
|
|
<WidgetGraphComponent
|
|
|
|
|
enableModel={false}
|
|
|
|
|
enableWidgetHeader
|
|
|
|
|
widget={widget}
|
|
|
|
|
queryResponse={queryResponse}
|
|
|
|
|
errorMessage={errorMessage}
|
|
|
|
|
data={prevChartDataSetRef}
|
|
|
|
|
name={name}
|
|
|
|
|
yAxisUnit={yAxisUnit}
|
|
|
|
|
layout={layout}
|
|
|
|
|
setLayout={setLayout}
|
2023-08-16 12:18:56 +05:30
|
|
|
threshold={threshold}
|
2023-08-11 15:17:32 +05:30
|
|
|
headerMenuList={headerMenuList}
|
|
|
|
|
onClickHandler={onClickHandler}
|
|
|
|
|
/>
|
2023-08-02 20:41:09 +05:30
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-06-07 16:14:49 +05:30
|
|
|
|
2023-08-02 20:41:09 +05:30
|
|
|
return (
|
|
|
|
|
<span ref={graphRef}>
|
2023-01-25 20:31:42 +05:30
|
|
|
{!isEmpty(widget) && !!queryResponse.data?.payload && (
|
2023-08-02 20:41:09 +05:30
|
|
|
<WidgetGraphComponent
|
|
|
|
|
enableModel={!isEmptyLayout}
|
|
|
|
|
enableWidgetHeader={!isEmptyLayout}
|
|
|
|
|
widget={widget}
|
|
|
|
|
queryResponse={queryResponse}
|
|
|
|
|
errorMessage={errorMessage}
|
2023-01-23 20:21:24 +05:30
|
|
|
data={chartData}
|
|
|
|
|
name={name}
|
|
|
|
|
yAxisUnit={yAxisUnit}
|
|
|
|
|
onDragSelect={onDragSelect}
|
2023-08-16 12:18:56 +05:30
|
|
|
threshold={threshold}
|
2023-08-10 11:44:36 +05:30
|
|
|
headerMenuList={headerMenuList}
|
2023-08-03 23:02:25 +05:30
|
|
|
onClickHandler={onClickHandler}
|
2022-06-07 16:14:49 +05:30
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{isEmptyLayout && <EmptyWidget />}
|
2022-04-19 10:57:56 +05:30
|
|
|
</span>
|
2021-09-23 15:43:43 +05:30
|
|
|
);
|
2022-03-22 12:10:31 +05:30
|
|
|
}
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2023-01-17 13:30:34 +02:00
|
|
|
GridCardGraph.defaultProps = {
|
|
|
|
|
onDragSelect: undefined,
|
2023-07-18 08:55:01 +05:30
|
|
|
onClickHandler: undefined,
|
2023-07-21 16:17:31 +05:30
|
|
|
isQueryEnabled: true,
|
2023-08-16 12:18:56 +05:30
|
|
|
threshold: undefined,
|
2023-08-10 11:44:36 +05:30
|
|
|
headerMenuList: [MenuItemKeys.View],
|
2023-01-17 13:30:34 +02:00
|
|
|
};
|
|
|
|
|
|
2023-08-02 20:41:09 +05:30
|
|
|
export default memo(GridCardGraph);
|