Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

3.7 KB
Raw Blame
import { useCallback } from 'react';
import { useMutation } from 'react-query';
// eslint-disable-next-line no-restricted-imports
import { useSelector } from 'react-redux';
import logEvent from 'api/common/logEvent';
import { getSubstituteVars } from 'api/dashboard/substitute_vars';
import { prepareQueryRangePayloadV5 } from 'api/v5/v5';
import { YAxisSource } from 'components/YAxisUnitSelector/types';
import { SOMETHING_WENT_WRONG } from 'constants/api';
import { ENTITY_VERSION_V5 } from 'constants/app';
import { QueryParams } from 'constants/query';
import ROUTES from 'constants/routes';
import { MenuItemKeys } from 'container/WidgetCard/Header/contants';
import { useDynamicVariableSuggestions } from 'hooks/dashboard/useDynamicVariableSuggestions';
import { useNotifications } from 'hooks/useNotifications';
import { mapQueryDataFromApi } from 'lib/newQueryBuilder/queryBuilderMappers/mapQueryDataFromApi';
import { isEmpty } from 'lodash-es';
import { AppState } from 'store/reducers';
import { Widgets } from 'types/api/widgets/widget';
import { GlobalReducer } from 'types/reducer/globalTime';
import { withBasePath } from 'utils/basePath';
import { getGraphType } from 'utils/getGraphType';

/**
 * @deprecated V1-only. V2 dashboards seed alerts from a panel via
 * `useCreateAlertFromPanel` / `buildCreateAlertUrl`
 * (pages/DashboardPage/.../Panel). Do not use in new code.
 */
const useCreateAlerts = (widget?: Widgets, caller?: string): VoidFunction => {
	const queryRangeMutation = useMutation(getSubstituteVars);

	const { selectedTime: globalSelectedInterval } = useSelector<
		AppState,
		GlobalReducer
	>((state) => state.globalTime);

	const { notifications } = useNotifications();

	const dashboardDynamicVariables = useDynamicVariableSuggestions();

	return useCallback(() => {
		if (!widget) {
			return;
		}

		if (caller === 'panelView') {
			logEvent('Panel Edit: Create alert', {
				panelType: widget.panelTypes,
				widgetId: widget.id,
				queryType: widget.query.queryType,
			});
		} else if (caller === 'dashboardView') {
			logEvent('Dashboard Detail: Panel action', {
				action: MenuItemKeys.CreateAlerts,
				panelType: widget.panelTypes,
				widgetId: widget.id,
				queryType: widget.query.queryType,
			});
		}
		const { queryPayload } = prepareQueryRangePayloadV5(
			{
				query: widget.query,
				globalSelectedInterval,
				graphType: getGraphType(widget.panelTypes),
				selectedTime: widget.timePreferance,
				originalGraphType: widget.panelTypes,
			},
			dashboardDynamicVariables,
		);
		queryRangeMutation.mutate(queryPayload, {
			onSuccess: (data) => {
				const updatedQuery = mapQueryDataFromApi(data.data.compositeQuery);
				if (widget.query.queryType) {
					updatedQuery.queryType = widget.query.queryType;
				}
				// If widget has a y-axis unit, set it to the updated query if it is not already set
				if (widget.yAxisUnit && !isEmpty(widget.yAxisUnit)) {
					updatedQuery.unit = widget.yAxisUnit;
				}

				const params = new URLSearchParams();
				params.set(
					QueryParams.compositeQuery,
					encodeURIComponent(JSON.stringify(updatedQuery)),
				);
				params.set(QueryParams.panelTypes, widget.panelTypes);
				params.set(QueryParams.version, ENTITY_VERSION_V5);
				params.set(QueryParams.source, YAxisSource.DASHBOARDS);

				const url = `${ROUTES.ALERTS_NEW}?${params.toString()}`;

				window.open(withBasePath(url), '_blank', 'noreferrer');
			},
			onError: () => {
				notifications.error({
					message: SOMETHING_WENT_WRONG,
				});
			},
		});
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [
		globalSelectedInterval,
		notifications,
		queryRangeMutation,
		dashboardDynamicVariables,
		widget,
	]);
};

export default useCreateAlerts;