2021-09-23 15:43:43 +05:30
|
|
|
import { Typography } from 'antd';
|
2022-06-24 15:00:21 +05:30
|
|
|
import { AxiosError } from 'axios';
|
|
|
|
|
import { ChartData } from 'chart.js';
|
2021-09-23 15:43:43 +05:30
|
|
|
import Spinner from 'components/Spinner';
|
|
|
|
|
import GridGraphComponent from 'container/GridGraphComponent';
|
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';
|
2022-06-24 15:00:21 +05:30
|
|
|
import React, { memo, useCallback, useEffect, useState } from 'react';
|
2022-06-07 16:14:49 +05:30
|
|
|
import { Layout } from 'react-grid-layout';
|
2022-03-22 12:10:31 +05:30
|
|
|
import { connect, useSelector } from 'react-redux';
|
2021-09-23 15:43:43 +05:30
|
|
|
import { bindActionCreators, Dispatch } from 'redux';
|
|
|
|
|
import { ThunkDispatch } from 'redux-thunk';
|
|
|
|
|
import {
|
|
|
|
|
DeleteWidget,
|
|
|
|
|
DeleteWidgetProps,
|
|
|
|
|
} from 'store/actions/dashboard/deleteWidget';
|
2022-06-24 15:00:21 +05:30
|
|
|
import { GetMetricQueryRange } from 'store/actions/dashboard/getQueryResults';
|
2021-09-23 15:43:43 +05:30
|
|
|
import { AppState } from 'store/reducers';
|
|
|
|
|
import AppActions from 'types/actions';
|
2021-10-20 09:24:55 +05:30
|
|
|
import { GlobalTime } from 'types/actions/globalTime';
|
2021-09-23 15:43:43 +05:30
|
|
|
import { Widgets } from 'types/api/dashboard/getAll';
|
2022-06-24 15:00:21 +05:30
|
|
|
import { GlobalReducer } from 'types/reducer/globalTime';
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2022-06-07 16:14:49 +05:30
|
|
|
import { LayoutProps } from '..';
|
|
|
|
|
import EmptyWidget from '../EmptyWidget';
|
2022-04-19 10:57:56 +05:30
|
|
|
import WidgetHeader from '../WidgetHeader';
|
2022-06-24 15:00:21 +05:30
|
|
|
import FullView from './FullView/index.metricsBuilder';
|
2022-03-14 20:12:42 +05:30
|
|
|
import { ErrorContainer, FullViewContainer, Modal } from './styles';
|
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,
|
|
|
|
|
deleteWidget,
|
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,
|
2022-03-22 12:10:31 +05:30
|
|
|
}: GridCardGraphProps): JSX.Element {
|
2022-06-24 15:00:21 +05:30
|
|
|
const [state, setState] = useState<GridCardGraphState>({
|
|
|
|
|
loading: true,
|
|
|
|
|
errorMessage: '',
|
|
|
|
|
error: false,
|
|
|
|
|
payload: undefined,
|
|
|
|
|
});
|
2022-04-19 10:57:56 +05:30
|
|
|
const [hovered, setHovered] = useState(false);
|
2021-09-23 15:43:43 +05:30
|
|
|
const [modal, setModal] = useState(false);
|
2022-06-24 15:00:21 +05:30
|
|
|
const [deleteModal, setDeleteModal] = useState(false);
|
|
|
|
|
|
2021-09-23 15:43:43 +05:30
|
|
|
const { minTime, maxTime } = useSelector<AppState, GlobalTime>(
|
|
|
|
|
(state) => state.globalTime,
|
|
|
|
|
);
|
2022-06-24 15:00:21 +05:30
|
|
|
const { selectedTime: globalSelectedInterval } = useSelector<
|
|
|
|
|
AppState,
|
|
|
|
|
GlobalReducer
|
|
|
|
|
>((state) => state.globalTime);
|
2022-06-07 16:14:49 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
// const getMaxMinTime = GetMaxMinTime({
|
|
|
|
|
// graphType: widget?.panelTypes,
|
|
|
|
|
// maxTime,
|
|
|
|
|
// minTime,
|
|
|
|
|
// });
|
2022-06-07 16:14:49 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
// const { start, end } = GetStartAndEndTime({
|
|
|
|
|
// type: widget?.timePreferance,
|
|
|
|
|
// maxTime: getMaxMinTime.maxTime,
|
|
|
|
|
// minTime: getMaxMinTime.minTime,
|
|
|
|
|
// });
|
2022-06-07 16:14:49 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
// const queryLength = widget?.query?.filter((e) => e.query.length !== 0) || [];
|
2022-06-07 16:14:49 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
// const response = useQueries(
|
|
|
|
|
// queryLength?.map((query) => {
|
|
|
|
|
// return {
|
|
|
|
|
// // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
|
|
|
// queryFn: () => {
|
|
|
|
|
// return getQueryResult({
|
|
|
|
|
// end,
|
|
|
|
|
// query: query?.query,
|
|
|
|
|
// start,
|
|
|
|
|
// step: '60',
|
|
|
|
|
// });
|
|
|
|
|
// },
|
|
|
|
|
// queryHash: `${query?.query}-${query?.legend}-${start}-${end}`,
|
|
|
|
|
// retryOnMount: false,
|
|
|
|
|
// };
|
|
|
|
|
// }),
|
|
|
|
|
// );
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
// const isError =
|
|
|
|
|
// response.find((e) => e?.data?.statusCode !== 200) !== undefined ||
|
|
|
|
|
// response.some((e) => e.isError === true);
|
2022-06-07 16:14:49 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
// const isLoading = response.some((e) => e.isLoading === true);
|
2022-06-07 16:14:49 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
// const errorMessage = response.find((e) => e.data?.error !== null)?.data?.error;
|
|
|
|
|
|
|
|
|
|
// const data = response.map((responseOfQuery) =>
|
|
|
|
|
// responseOfQuery?.data?.payload?.result.map((e, index) => ({
|
|
|
|
|
// query: queryLength[index]?.query,
|
|
|
|
|
// queryData: e,
|
|
|
|
|
// legend: queryLength[index]?.legend,
|
|
|
|
|
// })),
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
(async (): Promise<void> => {
|
|
|
|
|
try {
|
2022-09-09 17:43:25 +05:30
|
|
|
setState((state) => ({
|
|
|
|
|
...state,
|
|
|
|
|
error: false,
|
|
|
|
|
errorMessage: '',
|
|
|
|
|
loading: true,
|
|
|
|
|
}));
|
2022-06-24 15:00:21 +05:30
|
|
|
const response = await GetMetricQueryRange({
|
|
|
|
|
selectedTime: widget.timePreferance,
|
|
|
|
|
graphType: widget.panelTypes,
|
|
|
|
|
query: widget.query,
|
|
|
|
|
globalSelectedInterval,
|
2022-09-09 17:43:25 +05:30
|
|
|
variables: getDashboardVariables(),
|
2022-06-24 15:00:21 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const isError = response.error;
|
|
|
|
|
|
|
|
|
|
if (isError != null) {
|
|
|
|
|
setState((state) => ({
|
|
|
|
|
...state,
|
|
|
|
|
error: true,
|
|
|
|
|
errorMessage: isError || 'Something went wrong',
|
|
|
|
|
loading: false,
|
|
|
|
|
}));
|
|
|
|
|
} else {
|
|
|
|
|
const chartDataSet = getChartData({
|
|
|
|
|
queryData: [
|
|
|
|
|
{
|
|
|
|
|
queryData: response.payload?.data?.result
|
|
|
|
|
? response.payload?.data?.result
|
|
|
|
|
: [],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setState((state) => ({
|
|
|
|
|
...state,
|
|
|
|
|
loading: false,
|
|
|
|
|
payload: chartDataSet,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
setState((state) => ({
|
|
|
|
|
...state,
|
|
|
|
|
error: true,
|
|
|
|
|
errorMessage: (error as AxiosError).toString(),
|
|
|
|
|
loading: false,
|
|
|
|
|
}));
|
2022-09-09 17:43:25 +05:30
|
|
|
} finally {
|
|
|
|
|
setState((state) => ({
|
|
|
|
|
...state,
|
|
|
|
|
loading: false,
|
|
|
|
|
}));
|
2022-06-24 15:00:21 +05:30
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
}, [widget, maxTime, minTime, globalSelectedInterval]);
|
2021-09-23 15:43:43 +05:30
|
|
|
|
|
|
|
|
const onToggleModal = useCallback(
|
|
|
|
|
(func: React.Dispatch<React.SetStateAction<boolean>>) => {
|
|
|
|
|
func((value) => !value);
|
|
|
|
|
},
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
|
2022-03-24 12:06:57 +05:30
|
|
|
const onDeleteHandler = useCallback(() => {
|
2022-06-07 16:14:49 +05:30
|
|
|
const isEmptyWidget = widget?.id === 'empty' || isEmpty(widget);
|
|
|
|
|
|
|
|
|
|
const widgetId = isEmptyWidget ? layout[0].i : widget?.id;
|
|
|
|
|
|
|
|
|
|
deleteWidget({ widgetId, setLayout });
|
|
|
|
|
onToggleModal(setDeleteModal);
|
|
|
|
|
}, [deleteWidget, layout, onToggleModal, setLayout, widget]);
|
2022-03-24 12:06:57 +05:30
|
|
|
|
2022-03-21 21:04:32 +05:30
|
|
|
const getModals = (): JSX.Element => {
|
2022-02-11 12:00:46 +05:30
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Modal
|
|
|
|
|
destroyOnClose
|
2022-06-07 16:14:49 +05:30
|
|
|
onCancel={(): void => onToggleModal(setDeleteModal)}
|
2022-02-11 12:00:46 +05:30
|
|
|
visible={deleteModal}
|
|
|
|
|
title="Delete"
|
|
|
|
|
height="10vh"
|
|
|
|
|
onOk={onDeleteHandler}
|
|
|
|
|
centered
|
|
|
|
|
>
|
|
|
|
|
<Typography>Are you sure you want to delete this widget</Typography>
|
|
|
|
|
</Modal>
|
|
|
|
|
|
|
|
|
|
<Modal
|
|
|
|
|
title="View"
|
|
|
|
|
footer={[]}
|
|
|
|
|
centered
|
|
|
|
|
visible={modal}
|
|
|
|
|
onCancel={(): void => onToggleModal(setModal)}
|
|
|
|
|
width="85%"
|
|
|
|
|
destroyOnClose
|
|
|
|
|
>
|
|
|
|
|
<FullViewContainer>
|
2022-03-22 12:10:31 +05:30
|
|
|
<FullView
|
|
|
|
|
name={`${name}expanded`}
|
|
|
|
|
widget={widget}
|
|
|
|
|
yAxisUnit={yAxisUnit}
|
|
|
|
|
/>
|
2022-02-11 12:00:46 +05:30
|
|
|
</FullViewContainer>
|
|
|
|
|
</Modal>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-07 16:14:49 +05:30
|
|
|
const isEmptyLayout = widget?.id === 'empty' || isEmpty(widget);
|
|
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
if (state.error && !isEmptyLayout) {
|
2022-02-10 22:20:31 +05:30
|
|
|
return (
|
|
|
|
|
<>
|
2022-02-11 12:00:46 +05:30
|
|
|
{getModals()}
|
2022-04-19 10:57:56 +05:30
|
|
|
<WidgetHeader
|
|
|
|
|
parentHover={hovered}
|
|
|
|
|
title={widget?.title}
|
2022-02-10 22:20:31 +05:30
|
|
|
widget={widget}
|
2022-04-19 10:57:56 +05:30
|
|
|
onView={(): void => onToggleModal(setModal)}
|
2022-06-07 16:14:49 +05:30
|
|
|
onDelete={(): void => onToggleModal(setDeleteModal)}
|
2022-02-10 22:20:31 +05:30
|
|
|
/>
|
|
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
<ErrorContainer>{state.errorMessage}</ErrorContainer>
|
2022-02-10 22:20:31 +05:30
|
|
|
</>
|
|
|
|
|
);
|
2021-09-23 15:43:43 +05:30
|
|
|
}
|
|
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
if (
|
|
|
|
|
(state.loading === true || state.payload === undefined) &&
|
|
|
|
|
!isEmptyLayout
|
|
|
|
|
) {
|
|
|
|
|
return <Spinner height="20vh" tip="Loading..." />;
|
|
|
|
|
}
|
2021-09-23 15:43:43 +05:30
|
|
|
|
|
|
|
|
return (
|
2022-04-19 10:57:56 +05:30
|
|
|
<span
|
|
|
|
|
onMouseOver={(): void => {
|
|
|
|
|
setHovered(true);
|
|
|
|
|
}}
|
|
|
|
|
onFocus={(): void => {
|
|
|
|
|
setHovered(true);
|
|
|
|
|
}}
|
|
|
|
|
onMouseOut={(): void => {
|
|
|
|
|
setHovered(false);
|
|
|
|
|
}}
|
|
|
|
|
onBlur={(): void => {
|
|
|
|
|
setHovered(false);
|
|
|
|
|
}}
|
|
|
|
|
>
|
2022-06-07 16:14:49 +05:30
|
|
|
{!isEmptyLayout && (
|
|
|
|
|
<WidgetHeader
|
|
|
|
|
parentHover={hovered}
|
|
|
|
|
title={widget?.title}
|
|
|
|
|
widget={widget}
|
|
|
|
|
onView={(): void => onToggleModal(setModal)}
|
|
|
|
|
onDelete={(): void => onToggleModal(setDeleteModal)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{!isEmptyLayout && getModals()}
|
|
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
{!isEmpty(widget) && !!state.payload && (
|
2022-06-07 16:14:49 +05:30
|
|
|
<GridGraphComponent
|
|
|
|
|
{...{
|
|
|
|
|
GRAPH_TYPES: widget.panelTypes,
|
2022-06-24 15:00:21 +05:30
|
|
|
data: state.payload,
|
2022-06-07 16:14:49 +05:30
|
|
|
isStacked: widget.isStacked,
|
|
|
|
|
opacity: widget.opacity,
|
|
|
|
|
title: ' ', // empty title to accommodate absolutely positioned widget header
|
|
|
|
|
name,
|
|
|
|
|
yAxisUnit,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{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
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
interface GridCardGraphState {
|
|
|
|
|
loading: boolean;
|
|
|
|
|
error: boolean;
|
|
|
|
|
errorMessage: string;
|
|
|
|
|
payload: ChartData | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 15:43:43 +05:30
|
|
|
interface DispatchProps {
|
|
|
|
|
deleteWidget: ({
|
|
|
|
|
widgetId,
|
|
|
|
|
}: DeleteWidgetProps) => (dispatch: Dispatch<AppActions>) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface GridCardGraphProps extends DispatchProps {
|
|
|
|
|
widget: Widgets;
|
2021-12-10 14:28:31 +05:30
|
|
|
name: string;
|
2022-03-15 15:18:33 +05:30
|
|
|
yAxisUnit: string | undefined;
|
2022-06-07 16:14:49 +05:30
|
|
|
// eslint-disable-next-line react/require-default-props
|
|
|
|
|
layout?: Layout[];
|
|
|
|
|
// eslint-disable-next-line react/require-default-props
|
|
|
|
|
setLayout?: React.Dispatch<React.SetStateAction<LayoutProps[]>>;
|
2021-09-23 15:43:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = (
|
|
|
|
|
dispatch: ThunkDispatch<unknown, unknown, AppActions>,
|
|
|
|
|
): DispatchProps => ({
|
|
|
|
|
deleteWidget: bindActionCreators(DeleteWidget, dispatch),
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-07 16:14:49 +05:30
|
|
|
export default connect(null, mapDispatchToProps)(memo(GridCardGraph));
|