2021-09-23 15:43:43 +05:30
|
|
|
import { Button, Typography } from 'antd';
|
|
|
|
|
import getQueryResult from 'api/widgets/getQuery';
|
2022-03-24 12:06:57 +05:30
|
|
|
import { GraphOnClickHandler } from 'components/Graph';
|
2021-09-23 15:43:43 +05:30
|
|
|
import Spinner from 'components/Spinner';
|
|
|
|
|
import TimePreference from 'components/TimePreferenceDropDown';
|
|
|
|
|
import GridGraphComponent from 'container/GridGraphComponent';
|
|
|
|
|
import {
|
|
|
|
|
timeItems,
|
|
|
|
|
timePreferance,
|
2022-01-26 21:46:59 +05:30
|
|
|
timePreferenceType,
|
2021-09-23 15:43:43 +05:30
|
|
|
} from 'container/NewWidget/RightContainer/timeItems';
|
2022-01-26 21:46:59 +05:30
|
|
|
import convertToNanoSecondsToSecond from 'lib/convertToNanoSecondsToSecond';
|
2021-09-23 15:43:43 +05:30
|
|
|
import getChartData from 'lib/getChartData';
|
|
|
|
|
import GetMaxMinTime from 'lib/getMaxMinTime';
|
2022-01-26 21:46:59 +05:30
|
|
|
import GetMinMax from 'lib/getMinMax';
|
2021-09-23 15:43:43 +05:30
|
|
|
import getStartAndEndTime from 'lib/getStartAndEndTime';
|
2022-02-10 17:37:37 +05:30
|
|
|
import getStep from 'lib/getStep';
|
2022-05-19 23:08:27 +05:30
|
|
|
import React, { useCallback, useState } from 'react';
|
|
|
|
|
import { useQueries } from 'react-query';
|
2021-09-23 15:43:43 +05:30
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
|
import { AppState } from 'store/reducers';
|
|
|
|
|
import { Widgets } from 'types/api/dashboard/getAll';
|
2022-01-26 21:46:59 +05:30
|
|
|
import { GlobalReducer } from 'types/reducer/globalTime';
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2022-01-26 21:46:59 +05:30
|
|
|
import { NotFoundContainer, TimeContainer } from './styles';
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2022-03-22 12:10:31 +05:30
|
|
|
function FullView({
|
2021-10-20 09:24:55 +05:30
|
|
|
widget,
|
|
|
|
|
fullViewOptions = true,
|
|
|
|
|
onClickHandler,
|
2021-12-10 14:28:31 +05:30
|
|
|
name,
|
2022-03-15 15:18:33 +05:30
|
|
|
yAxisUnit,
|
2022-03-22 12:10:31 +05:30
|
|
|
}: FullViewProps): JSX.Element {
|
2022-01-26 21:46:59 +05:30
|
|
|
const { minTime, maxTime, selectedTime: globalSelectedTime } = useSelector<
|
|
|
|
|
AppState,
|
|
|
|
|
GlobalReducer
|
|
|
|
|
>((state) => state.globalTime);
|
2021-09-23 15:43:43 +05:30
|
|
|
|
|
|
|
|
const getSelectedTime = useCallback(
|
|
|
|
|
() =>
|
|
|
|
|
timeItems.find((e) => e.enum === (widget?.timePreferance || 'GLOBAL_TIME')),
|
|
|
|
|
[widget],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const [selectedTime, setSelectedTime] = useState<timePreferance>({
|
|
|
|
|
name: getSelectedTime()?.name || '',
|
|
|
|
|
enum: widget?.timePreferance || 'GLOBAL_TIME',
|
|
|
|
|
});
|
|
|
|
|
|
2022-05-19 23:08:27 +05:30
|
|
|
const maxMinTime = GetMaxMinTime({
|
|
|
|
|
graphType: widget.panelTypes,
|
|
|
|
|
maxTime,
|
|
|
|
|
minTime,
|
|
|
|
|
});
|
2022-01-26 21:46:59 +05:30
|
|
|
|
2022-05-19 23:08:27 +05:30
|
|
|
const getMinMax = (
|
|
|
|
|
time: timePreferenceType,
|
|
|
|
|
): { min: string | number; max: string | number } => {
|
|
|
|
|
if (time === 'GLOBAL_TIME') {
|
|
|
|
|
const minMax = GetMinMax(globalSelectedTime);
|
|
|
|
|
return {
|
|
|
|
|
min: convertToNanoSecondsToSecond(minMax.minTime / 1000),
|
|
|
|
|
max: convertToNanoSecondsToSecond(minMax.maxTime / 1000),
|
|
|
|
|
};
|
2021-09-23 15:43:43 +05:30
|
|
|
}
|
|
|
|
|
|
2022-05-19 23:08:27 +05:30
|
|
|
const minMax = getStartAndEndTime({
|
|
|
|
|
type: selectedTime.enum,
|
|
|
|
|
maxTime: maxMinTime.maxTime,
|
|
|
|
|
minTime: maxMinTime.minTime,
|
|
|
|
|
});
|
|
|
|
|
return { min: parseInt(minMax.start, 10), max: parseInt(minMax.end, 10) };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const queryMinMax = getMinMax(selectedTime.enum);
|
|
|
|
|
|
|
|
|
|
const queryLength = widget.query.filter((e) => e.query.length !== 0);
|
|
|
|
|
|
|
|
|
|
const response = useQueries(
|
|
|
|
|
queryLength.map((query) => {
|
|
|
|
|
return {
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
|
|
|
queryFn: () => {
|
|
|
|
|
return getQueryResult({
|
|
|
|
|
end: queryMinMax.max.toString(),
|
|
|
|
|
query: query.query,
|
|
|
|
|
start: queryMinMax.min.toString(),
|
|
|
|
|
step: `${getStep({
|
|
|
|
|
start: queryMinMax.min,
|
|
|
|
|
end: queryMinMax.max,
|
|
|
|
|
inputFormat: 's',
|
|
|
|
|
})}`,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
queryHash: `${query.query}-${query.legend}-${selectedTime.enum}`,
|
|
|
|
|
retryOnMount: false,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
);
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2022-05-19 23:08:27 +05:30
|
|
|
const isError =
|
|
|
|
|
response.find((e) => e?.data?.statusCode !== 200) !== undefined ||
|
|
|
|
|
response.some((e) => e.isError === true);
|
|
|
|
|
|
|
|
|
|
const isLoading = response.some((e) => e.isLoading === true);
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
|
return <Spinner height="100%" size="large" tip="Loading..." />;
|
2021-10-20 09:24:55 +05:30
|
|
|
}
|
|
|
|
|
|
2022-05-19 23:08:27 +05:30
|
|
|
if (isError || data === undefined || data[0] === undefined) {
|
2021-10-20 09:24:55 +05:30
|
|
|
return (
|
2022-05-19 23:08:27 +05:30
|
|
|
<NotFoundContainer>
|
|
|
|
|
<Typography>{errorMessage}</Typography>
|
|
|
|
|
</NotFoundContainer>
|
2021-10-20 09:24:55 +05:30
|
|
|
);
|
2021-09-23 15:43:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2021-10-20 09:24:55 +05:30
|
|
|
{fullViewOptions && (
|
|
|
|
|
<TimeContainer>
|
|
|
|
|
<TimePreference
|
|
|
|
|
{...{
|
|
|
|
|
selectedTime,
|
|
|
|
|
setSelectedTime,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2022-05-19 23:08:27 +05:30
|
|
|
<Button
|
|
|
|
|
onClick={(): void => {
|
|
|
|
|
response.forEach((e) => e.refetch());
|
|
|
|
|
}}
|
|
|
|
|
type="primary"
|
|
|
|
|
>
|
2021-10-20 09:24:55 +05:30
|
|
|
Refresh
|
|
|
|
|
</Button>
|
|
|
|
|
</TimeContainer>
|
|
|
|
|
)}
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2021-12-10 14:28:31 +05:30
|
|
|
<GridGraphComponent
|
|
|
|
|
{...{
|
|
|
|
|
GRAPH_TYPES: widget.panelTypes,
|
2022-05-19 23:08:27 +05:30
|
|
|
data: getChartData({
|
|
|
|
|
queryData: data.map((e) => ({
|
|
|
|
|
query: e?.map((e) => e.query).join(' ') || '',
|
|
|
|
|
queryData: e?.map((e) => e.queryData) || [],
|
|
|
|
|
legend: e?.map((e) => e.legend).join('') || '',
|
|
|
|
|
})),
|
|
|
|
|
}),
|
2021-12-10 14:28:31 +05:30
|
|
|
isStacked: widget.isStacked,
|
|
|
|
|
opacity: widget.opacity,
|
|
|
|
|
title: widget.title,
|
2022-03-22 12:10:31 +05:30
|
|
|
onClickHandler,
|
2021-12-10 14:28:31 +05:30
|
|
|
name,
|
2022-03-15 15:18:33 +05:30
|
|
|
yAxisUnit,
|
2021-12-10 14:28:31 +05:30
|
|
|
}}
|
|
|
|
|
/>
|
2021-09-23 15:43:43 +05:30
|
|
|
</>
|
|
|
|
|
);
|
2022-03-22 12:10:31 +05:30
|
|
|
}
|
2021-09-23 15:43:43 +05:30
|
|
|
|
|
|
|
|
interface FullViewProps {
|
|
|
|
|
widget: Widgets;
|
2021-10-20 09:24:55 +05:30
|
|
|
fullViewOptions?: boolean;
|
2022-03-24 12:06:57 +05:30
|
|
|
onClickHandler?: GraphOnClickHandler;
|
2021-12-10 14:28:31 +05:30
|
|
|
name: string;
|
2022-03-15 15:18:33 +05:30
|
|
|
yAxisUnit?: string;
|
2021-09-23 15:43:43 +05:30
|
|
|
}
|
|
|
|
|
|
2022-03-24 12:06:57 +05:30
|
|
|
FullView.defaultProps = {
|
|
|
|
|
fullViewOptions: undefined,
|
|
|
|
|
onClickHandler: undefined,
|
|
|
|
|
yAxisUnit: undefined,
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-16 21:13:20 +05:30
|
|
|
export default FullView;
|