2024-02-28 14:56:50 +05:30
|
|
|
import { PANEL_TYPES } from 'constants/queryBuilder';
|
2023-06-07 15:27:33 +03:00
|
|
|
import { REACT_QUERY_KEY } from 'constants/reactQueryKeys';
|
|
|
|
|
import {
|
|
|
|
|
GetMetricQueryRange,
|
|
|
|
|
GetQueryResultsProps,
|
2023-10-08 23:21:17 +05:30
|
|
|
} from 'lib/dashboard/getQueryResults';
|
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
|
import { useQuery, UseQueryOptions, UseQueryResult } from 'react-query';
|
2023-06-07 15:27:33 +03:00
|
|
|
import { SuccessResponse } from 'types/api';
|
|
|
|
|
import { MetricRangePayloadProps } from 'types/api/metrics/getQueryRange';
|
|
|
|
|
|
2023-06-16 13:38:39 +03:00
|
|
|
type UseGetQueryRange = (
|
2023-06-07 15:27:33 +03:00
|
|
|
requestData: GetQueryResultsProps,
|
|
|
|
|
options?: UseQueryOptions<SuccessResponse<MetricRangePayloadProps>, Error>,
|
2023-06-16 13:38:39 +03:00
|
|
|
) => UseQueryResult<SuccessResponse<MetricRangePayloadProps>, Error>;
|
2023-06-13 16:26:12 +05:30
|
|
|
|
2023-06-16 13:38:39 +03:00
|
|
|
export const useGetQueryRange: UseGetQueryRange = (requestData, options) => {
|
2024-02-28 14:56:50 +05:30
|
|
|
const newRequestData: GetQueryResultsProps = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
...requestData,
|
|
|
|
|
graphType:
|
|
|
|
|
requestData.graphType === PANEL_TYPES.BAR
|
|
|
|
|
? PANEL_TYPES.TIME_SERIES
|
|
|
|
|
: requestData.graphType,
|
|
|
|
|
}),
|
|
|
|
|
[requestData],
|
|
|
|
|
);
|
|
|
|
|
|
2023-06-13 16:26:12 +05:30
|
|
|
const queryKey = useMemo(() => {
|
2023-12-12 17:18:57 +05:30
|
|
|
if (options?.queryKey && Array.isArray(options.queryKey)) {
|
2023-06-16 13:38:39 +03:00
|
|
|
return [...options.queryKey];
|
2023-06-13 16:26:12 +05:30
|
|
|
}
|
2023-12-12 17:18:57 +05:30
|
|
|
|
|
|
|
|
if (options?.queryKey && typeof options.queryKey === 'string') {
|
|
|
|
|
return options.queryKey;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 14:56:50 +05:30
|
|
|
return [REACT_QUERY_KEY.GET_QUERY_RANGE, newRequestData];
|
|
|
|
|
}, [options?.queryKey, newRequestData]);
|
2023-06-13 16:26:12 +05:30
|
|
|
|
|
|
|
|
return useQuery<SuccessResponse<MetricRangePayloadProps>, Error>({
|
2024-02-28 14:56:50 +05:30
|
|
|
queryFn: async ({ signal }) => GetMetricQueryRange(newRequestData, signal),
|
2023-06-07 15:27:33 +03:00
|
|
|
...options,
|
2023-06-13 16:26:12 +05:30
|
|
|
queryKey,
|
2023-06-07 15:27:33 +03:00
|
|
|
});
|
2023-06-13 16:26:12 +05:30
|
|
|
};
|