2023-12-15 17:08:35 +05:30
|
|
|
import { getQueryRangeFormat } from 'api/dashboard/queryRangeFormat';
|
|
|
|
|
import { SOMETHING_WENT_WRONG } from 'constants/api';
|
2024-03-01 14:51:50 +05:30
|
|
|
import { DEFAULT_ENTITY_VERSION } from 'constants/app';
|
2023-12-15 17:08:35 +05:30
|
|
|
import { QueryParams } from 'constants/query';
|
|
|
|
|
import ROUTES from 'constants/routes';
|
|
|
|
|
import { useNotifications } from 'hooks/useNotifications';
|
|
|
|
|
import { getDashboardVariables } from 'lib/dashbaordVariables/getDashboardVariables';
|
|
|
|
|
import { prepareQueryRangePayload } from 'lib/dashboard/prepareQueryRangePayload';
|
|
|
|
|
import history from 'lib/history';
|
|
|
|
|
import { mapQueryDataFromApi } from 'lib/newQueryBuilder/queryBuilderMappers/mapQueryDataFromApi';
|
|
|
|
|
import { useDashboard } from 'providers/Dashboard/Dashboard';
|
|
|
|
|
import { useCallback } from 'react';
|
|
|
|
|
import { useMutation } from 'react-query';
|
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
|
import { AppState } from 'store/reducers';
|
|
|
|
|
import { Widgets } from 'types/api/dashboard/getAll';
|
|
|
|
|
import { GlobalReducer } from 'types/reducer/globalTime';
|
2024-02-28 14:56:50 +05:30
|
|
|
import { getGraphType } from 'utils/getGraphType';
|
2023-12-15 17:08:35 +05:30
|
|
|
|
|
|
|
|
const useCreateAlerts = (widget?: Widgets): VoidFunction => {
|
|
|
|
|
const queryRangeMutation = useMutation(getQueryRangeFormat);
|
|
|
|
|
|
|
|
|
|
const { selectedTime: globalSelectedInterval } = useSelector<
|
|
|
|
|
AppState,
|
|
|
|
|
GlobalReducer
|
|
|
|
|
>((state) => state.globalTime);
|
|
|
|
|
|
|
|
|
|
const { notifications } = useNotifications();
|
|
|
|
|
|
|
|
|
|
const { selectedDashboard } = useDashboard();
|
|
|
|
|
|
|
|
|
|
return useCallback(() => {
|
|
|
|
|
if (!widget) return;
|
|
|
|
|
|
|
|
|
|
const { queryPayload } = prepareQueryRangePayload({
|
|
|
|
|
query: widget.query,
|
|
|
|
|
globalSelectedInterval,
|
2024-02-28 14:56:50 +05:30
|
|
|
graphType: getGraphType(widget.panelTypes),
|
2023-12-15 17:08:35 +05:30
|
|
|
selectedTime: widget.timePreferance,
|
|
|
|
|
variables: getDashboardVariables(selectedDashboard?.data.variables),
|
|
|
|
|
});
|
|
|
|
|
queryRangeMutation.mutate(queryPayload, {
|
|
|
|
|
onSuccess: (data) => {
|
|
|
|
|
const updatedQuery = mapQueryDataFromApi(data.compositeQuery);
|
|
|
|
|
|
|
|
|
|
history.push(
|
|
|
|
|
`${ROUTES.ALERTS_NEW}?${QueryParams.compositeQuery}=${encodeURIComponent(
|
|
|
|
|
JSON.stringify(updatedQuery),
|
2024-03-01 14:51:50 +05:30
|
|
|
)}&${QueryParams.panelTypes}=${widget.panelTypes}&version=${
|
|
|
|
|
selectedDashboard?.data.version || DEFAULT_ENTITY_VERSION
|
|
|
|
|
}`,
|
2023-12-15 17:08:35 +05:30
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
onError: () => {
|
|
|
|
|
notifications.error({
|
|
|
|
|
message: SOMETHING_WENT_WRONG,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}, [
|
|
|
|
|
globalSelectedInterval,
|
|
|
|
|
notifications,
|
|
|
|
|
queryRangeMutation,
|
|
|
|
|
selectedDashboard?.data.variables,
|
2024-03-01 14:51:50 +05:30
|
|
|
selectedDashboard?.data.version,
|
2023-12-15 17:08:35 +05:30
|
|
|
widget,
|
|
|
|
|
]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useCreateAlerts;
|