2023-11-15 15:33:45 +05:30
|
|
|
import GridPanelSwitch from 'container/GridPanelSwitch';
|
2023-11-15 17:14:09 +05:30
|
|
|
import { ThresholdProps } from 'container/NewWidget/RightContainer/Threshold/types';
|
2023-11-15 15:33:45 +05:30
|
|
|
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
|
|
|
|
|
import { useIsDarkMode } from 'hooks/useDarkMode';
|
|
|
|
|
import { useResizeObserver } from 'hooks/useDimensions';
|
|
|
|
|
import useUrlQuery from 'hooks/useUrlQuery';
|
2023-11-21 13:12:37 +05:30
|
|
|
import { getUPlotChartOptions } from 'lib/uPlotLib/getUplotChartOptions';
|
|
|
|
|
import { getUPlotChartData } from 'lib/uPlotLib/utils/getUplotChartData';
|
2023-12-14 22:56:25 +05:30
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
2023-11-15 15:33:45 +05:30
|
|
|
import { UseQueryResult } from 'react-query';
|
2023-12-14 22:56:25 +05:30
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2023-11-15 15:33:45 +05:30
|
|
|
import { UpdateTimeInterval } from 'store/actions';
|
2023-12-14 22:56:25 +05:30
|
|
|
import { AppState } from 'store/reducers';
|
2023-11-15 15:33:45 +05:30
|
|
|
import { SuccessResponse } from 'types/api';
|
|
|
|
|
import { Widgets } from 'types/api/dashboard/getAll';
|
|
|
|
|
import { MetricRangePayloadProps } from 'types/api/metrics/getQueryRange';
|
2023-12-14 22:56:25 +05:30
|
|
|
import { GlobalReducer } from 'types/reducer/globalTime';
|
|
|
|
|
import { getTimeRange } from 'utils/getTimeRange';
|
2023-11-15 15:33:45 +05:30
|
|
|
|
|
|
|
|
function WidgetGraph({
|
|
|
|
|
getWidgetQueryRange,
|
|
|
|
|
selectedWidget,
|
|
|
|
|
yAxisUnit,
|
2023-11-15 17:14:09 +05:30
|
|
|
thresholds,
|
2023-11-15 18:25:02 +05:30
|
|
|
fillSpans,
|
2024-01-09 14:19:23 +05:30
|
|
|
softMax,
|
|
|
|
|
softMin,
|
2023-11-15 15:33:45 +05:30
|
|
|
}: WidgetGraphProps): JSX.Element {
|
|
|
|
|
const { stagedQuery } = useQueryBuilder();
|
|
|
|
|
|
2023-12-14 22:56:25 +05:30
|
|
|
const { minTime, maxTime, selectedTime: globalSelectedInterval } = useSelector<
|
|
|
|
|
AppState,
|
|
|
|
|
GlobalReducer
|
|
|
|
|
>((state) => state.globalTime);
|
|
|
|
|
|
|
|
|
|
const [minTimeScale, setMinTimeScale] = useState<number>();
|
|
|
|
|
const [maxTimeScale, setMaxTimeScale] = useState<number>();
|
|
|
|
|
|
|
|
|
|
useEffect((): void => {
|
|
|
|
|
const { startTime, endTime } = getTimeRange(getWidgetQueryRange);
|
|
|
|
|
|
|
|
|
|
setMinTimeScale(startTime);
|
|
|
|
|
setMaxTimeScale(endTime);
|
|
|
|
|
}, [getWidgetQueryRange, maxTime, minTime, globalSelectedInterval]);
|
|
|
|
|
|
2023-11-15 15:33:45 +05:30
|
|
|
const graphRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
|
|
const containerDimensions = useResizeObserver(graphRef);
|
|
|
|
|
|
2023-11-15 18:25:02 +05:30
|
|
|
const chartData = getUPlotChartData(
|
|
|
|
|
getWidgetQueryRange?.data?.payload,
|
|
|
|
|
fillSpans,
|
|
|
|
|
);
|
2023-11-15 15:33:45 +05:30
|
|
|
|
|
|
|
|
const isDarkMode = useIsDarkMode();
|
|
|
|
|
|
|
|
|
|
const params = useUrlQuery();
|
|
|
|
|
|
|
|
|
|
const widgetId = params.get('widgetId');
|
|
|
|
|
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const onDragSelect = useCallback(
|
|
|
|
|
(start: number, end: number): void => {
|
|
|
|
|
const startTimestamp = Math.trunc(start);
|
|
|
|
|
const endTimestamp = Math.trunc(end);
|
|
|
|
|
|
|
|
|
|
if (startTimestamp !== endTimestamp) {
|
|
|
|
|
dispatch(UpdateTimeInterval('custom', [startTimestamp, endTimestamp]));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[dispatch],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const options = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
getUPlotChartOptions({
|
|
|
|
|
id: widgetId || 'legend_widget',
|
|
|
|
|
yAxisUnit,
|
|
|
|
|
apiResponse: getWidgetQueryRange?.data?.payload,
|
|
|
|
|
dimensions: containerDimensions,
|
|
|
|
|
isDarkMode,
|
|
|
|
|
onDragSelect,
|
2023-11-15 19:17:06 +05:30
|
|
|
thresholds,
|
2023-11-15 18:25:02 +05:30
|
|
|
fillSpans,
|
2023-12-14 22:56:25 +05:30
|
|
|
minTimeScale,
|
|
|
|
|
maxTimeScale,
|
2024-01-09 14:19:23 +05:30
|
|
|
softMax,
|
|
|
|
|
softMin,
|
2023-11-15 15:33:45 +05:30
|
|
|
}),
|
|
|
|
|
[
|
|
|
|
|
widgetId,
|
|
|
|
|
yAxisUnit,
|
|
|
|
|
getWidgetQueryRange?.data?.payload,
|
|
|
|
|
containerDimensions,
|
|
|
|
|
isDarkMode,
|
|
|
|
|
onDragSelect,
|
2023-11-15 19:17:06 +05:30
|
|
|
thresholds,
|
2023-11-15 18:25:02 +05:30
|
|
|
fillSpans,
|
2023-12-14 22:56:25 +05:30
|
|
|
minTimeScale,
|
|
|
|
|
maxTimeScale,
|
2024-01-09 14:19:23 +05:30
|
|
|
softMax,
|
|
|
|
|
softMin,
|
2023-11-15 15:33:45 +05:30
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div ref={graphRef} style={{ height: '100%' }}>
|
|
|
|
|
<GridPanelSwitch
|
|
|
|
|
data={chartData}
|
|
|
|
|
options={options}
|
|
|
|
|
panelType={selectedWidget.panelTypes}
|
|
|
|
|
name={widgetId || 'legend_widget'}
|
|
|
|
|
yAxisUnit={yAxisUnit}
|
|
|
|
|
panelData={
|
|
|
|
|
getWidgetQueryRange.data?.payload.data.newResult.data.result || []
|
|
|
|
|
}
|
|
|
|
|
query={stagedQuery || selectedWidget.query}
|
2023-11-15 17:14:09 +05:30
|
|
|
thresholds={thresholds}
|
2023-11-15 15:33:45 +05:30
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface WidgetGraphProps {
|
2023-11-15 17:14:09 +05:30
|
|
|
thresholds: ThresholdProps[];
|
2023-11-15 15:33:45 +05:30
|
|
|
yAxisUnit: string;
|
|
|
|
|
selectedWidget: Widgets;
|
2023-11-15 18:25:02 +05:30
|
|
|
fillSpans: boolean;
|
2023-11-15 15:33:45 +05:30
|
|
|
getWidgetQueryRange: UseQueryResult<
|
|
|
|
|
SuccessResponse<MetricRangePayloadProps, unknown>,
|
|
|
|
|
Error
|
|
|
|
|
>;
|
2024-01-09 14:19:23 +05:30
|
|
|
softMax: number | null;
|
|
|
|
|
softMin: number | null;
|
2023-11-15 15:33:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default WidgetGraph;
|