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.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
chore: use single reducer in create alerts flow (#10179)
Amlan Kumar Nandy3fffe6e19851
diff --git a/frontend/src/container/CreateAlertV2/__tests__/utils.test.tsx b/frontend/src/container/CreateAlertV2/__tests__/utils.test.tsxindex d480707a63..060de087ad 100644--- a/frontend/src/container/CreateAlertV2/__tests__/utils.test.tsx+++ b/frontend/src/container/CreateAlertV2/__tests__/utils.test.tsx@@ -338,7 +338,7 @@ describe('CreateAlertV2 utils', () => {const props = getCreateAlertLocalStateFromAlertDef(args);expect(props).toBeDefined();expect(props).toMatchObject({- basicAlertState: {+ basic: {...INITIAL_ALERT_STATE,name: 'test-alert',labels: {@@ -348,10 +348,10 @@ describe('CreateAlertV2 utils', () => {yAxisUnit: UniversalYAxisUnit.MINUTES,},// as we have already verified these utils in their respective tests- thresholdState: expect.any(Object),- advancedOptionsState: expect.any(Object),- evaluationWindowState: expect.any(Object),- notificationSettingsState: expect.any(Object),+ threshold: expect.any(Object),+ advancedOptions: expect.any(Object),+ evaluationWindow: expect.any(Object),+ notificationSettings: expect.any(Object),});});});diff --git a/frontend/src/container/CreateAlertV2/context/constants.ts b/frontend/src/container/CreateAlertV2/context/constants.tsindex b6a50b0609..a5d5ffd3cb 100644--- a/frontend/src/container/CreateAlertV2/context/constants.ts+++ b/frontend/src/container/CreateAlertV2/context/constants.ts@@ -196,3 +196,11 @@ export const INITIAL_NOTIFICATION_SETTINGS_STATE: NotificationSettingsState = {description: NOTIFICATION_MESSAGE_PLACEHOLDER,routingPolicies: false,};++export const INITIAL_CREATE_ALERT_STATE = {+ basic: INITIAL_ALERT_STATE,+ threshold: INITIAL_ALERT_THRESHOLD_STATE,+ advancedOptions: INITIAL_ADVANCED_OPTIONS_STATE,+ evaluationWindow: INITIAL_EVALUATION_WINDOW_STATE,+ notificationSettings: INITIAL_NOTIFICATION_SETTINGS_STATE,+};diff --git a/frontend/src/container/CreateAlertV2/context/index.tsx b/frontend/src/container/CreateAlertV2/context/index.tsxindex c5b719cf69..88ed643a5d 100644--- a/frontend/src/container/CreateAlertV2/context/index.tsx+++ b/frontend/src/container/CreateAlertV2/context/index.tsx@@ -17,26 +17,22 @@ import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';import { mapQueryDataFromApi } from 'lib/newQueryBuilder/queryBuilderMappers/mapQueryDataFromApi';import { AlertTypes } from 'types/api/alerts/alertTypes';+import { INITIAL_CREATE_ALERT_STATE } from './constants';import {- INITIAL_ADVANCED_OPTIONS_STATE,- INITIAL_ALERT_STATE,- INITIAL_ALERT_THRESHOLD_STATE,- INITIAL_EVALUATION_WINDOW_STATE,- INITIAL_NOTIFICATION_SETTINGS_STATE,-} from './constants';-import {+ AdvancedOptionsAction,+ AlertThresholdAction,AlertThresholdMatchType,+ CreateAlertAction,+ CreateAlertSlice,+ EvaluationWindowAction,ICreateAlertContextProps,ICreateAlertProviderProps,+ NotificationSettingsAction,} from './types';import {- advancedOptionsReducer,- alertCreationReducer,- alertThresholdReducer,buildInitialAlertDef,- evaluationWindowReducer,+ createAlertReducer,getInitialAlertTypeFromURL,- notificationSettingsReducer,} from './utils';const CreateAlertContext = createContext<ICreateAlertContextProps | null>(null);@@ -65,10 +61,65 @@ export function CreateAlertProvider(const { currentQuery, redirectWithQueryBuilderData } = useQueryBuilder();- const [alertState, setAlertState] = useReducer(alertCreationReducer, {- ...INITIAL_ALERT_STATE,- yAxisUnit: currentQuery.unit,- });+ const [createAlertState, setCreateAlertState] = useReducer(+ createAlertReducer,+ {+ ...INITIAL_CREATE_ALERT_STATE,+ basic: {+ ...INITIAL_CREATE_ALERT_STATE.basic,+ yAxisUnit: currentQuery.unit,+ },+ },+ );++ const setAlertState = useCallback(+ (action: CreateAlertAction) => {+ setCreateAlertState({+ slice: CreateAlertSlice.BASIC,+ action,+ });+ },+ [setCreateAlertState],+ );++ const setThresholdState = useCallback(+ (action: AlertThresholdAction) => {+ setCreateAlertState({+ slice: CreateAlertSlice.THRESHOLD,+ action,+ });+ },+ [setCreateAlertState],+ );++ const setEvaluationWindow = useCallback(+ (action: EvaluationWindowAction) => {+ setCreateAlertState({+ slice: CreateAlertSlice.EVALUATION_WINDOW,+ action,+ });+ },+ [setCreateAlertState],+ );++ const setAdvancedOptions = useCallback(+ (action: AdvancedOptionsAction) => {+ setCreateAlertState({+ slice: CreateAlertSlice.ADVANCED_OPTIONS,+ action,+ });+ },+ [setCreateAlertState],+ );+ const setNotificationSettings = useCallback(+ (action: NotificationSettingsAction) => {+ setCreateAlertState({+ slice: CreateAlertSlice.NOTIFICATION_SETTINGS,+ action,+ });+ },+ [setCreateAlertState],+ );const location = useLocation();const queryParams = new URLSearchParams(location.search);@@ -104,92 +155,56 @@ export function CreateAlertProvider([redirectWithQueryBuilderData],);- const [thresholdState, setThresholdState] = useReducer(- alertThresholdReducer,- INITIAL_ALERT_THRESHOLD_STATE,- );-- const [evaluationWindow, setEvaluationWindow] = useReducer(- evaluationWindowReducer,- INITIAL_EVALUATION_WINDOW_STATE,- );-- const [advancedOptions, setAdvancedOptions] = useReducer(- advancedOptionsReducer,- INITIAL_ADVANCED_OPTIONS_STATE,- );-- const [notificationSettings, setNotificationSettings] = useReducer(- notificationSettingsReducer,- INITIAL_NOTIFICATION_SETTINGS_STATE,- );-useEffect(() => {- setThresholdState({- type: 'RESET',+ setCreateAlertState({+ slice: CreateAlertSlice.THRESHOLD,+ action: {+ type: 'RESET',+ },});if (thresholdsFromURL) {try {const thresholds = JSON.parse(thresholdsFromURL);- setThresholdState({- type: 'SET_THRESHOLDS',- payload: thresholds,+ setCreateAlertState({+ slice: CreateAlertSlice.THRESHOLD,+ action: {+ type: 'SET_THRESHOLDS',+ payload: thresholds,+ },});} catch (error) {console.error('Error parsing thresholds from URL:', error);}- setEvaluationWindow({- type: 'SET_INITIAL_STATE_FOR_METER',+ setCreateAlertState({+ slice: CreateAlertSlice.EVALUATION_WINDOW,+ action: {+ type: 'SET_INITIAL_STATE_FOR_METER',+ },});- setThresholdState({- type: 'SET_MATCH_TYPE',- payload: AlertThresholdMatchType.IN_TOTAL,+ setCreateAlertState({+ slice: CreateAlertSlice.THRESHOLD,+ action: {+ type: 'SET_MATCH_TYPE',+ payload: AlertThresholdMatchType.IN_TOTAL,+ },});}}, [alertType, thresholdsFromURL]);useEffect(() => {if (isEditMode && initialAlertState) {- setAlertState({- type: 'SET_INITIAL_STATE',- payload: initialAlertState.basicAlertState,- });- setThresholdState({- type: 'SET_INITIAL_STATE',- payload: initialAlertState.thresholdState,- });- setEvaluationWindow({- type: 'SET_INITIAL_STATE',- payload: initialAlertState.evaluationWindowState,- });- setAdvancedOptions({- type: 'SET_INITIAL_STATE',- payload: initialAlertState.advancedOptionsState,- });- setNotificationSettings({+ setCreateAlertState({type: 'SET_INITIAL_STATE',- payload: initialAlertState.notificationSettingsState,+ payload: initialAlertState,});}}, [initialAlertState, isEditMode]);const discardAlertRule = useCallback(() => {- setAlertState({- type: 'RESET',- });- setThresholdState({- type: 'RESET',- });- setEvaluationWindow({- type: 'RESET',- });- setAdvancedOptions({- type: 'RESET',- });- setNotificationSettings({+ setCreateAlertState({type: 'RESET',});handleAlertTypeChange(AlertTypes.METRICS_BASED_ALERT);@@ -212,17 +227,17 @@ export function CreateAlertProvider(const contextValue: ICreateAlertContextProps = useMemo(() => ({- alertState,+ alertState: createAlertState.basic,setAlertState,alertType,setAlertType: handleAlertTypeChange,- thresholdState,+ thresholdState: createAlertState.threshold,setThresholdState,- evaluationWindow,+ evaluationWindow: createAlertState.evaluationWindow,setEvaluationWindow,- advancedOptions,+ advancedOptions: createAlertState.advancedOptions,setAdvancedOptions,- notificationSettings,+ notificationSettings: createAlertState.notificationSettings,setNotificationSettings,discardAlertRule,createAlertRule,@@ -234,13 +249,14 @@ export function CreateAlertProvider(isEditMode: isEditMode || false,}),[- alertState,+ createAlertState,+ setAlertState,+ setThresholdState,+ setEvaluationWindow,+ setAdvancedOptions,+ setNotificationSettings,alertType,handleAlertTypeChange,- thresholdState,- evaluationWindow,- advancedOptions,- notificationSettings,discardAlertRule,createAlertRule,isCreatingAlertRule,diff --git a/frontend/src/container/CreateAlertV2/context/types.ts b/frontend/src/container/CreateAlertV2/context/types.tsindex face91faf1..94f75d8d8f 100644--- a/frontend/src/container/CreateAlertV2/context/types.ts+++ b/frontend/src/container/CreateAlertV2/context/types.ts@@ -9,8 +9,6 @@ import { AlertTypes } from 'types/api/alerts/alertTypes';import { PostableAlertRuleV2 } from 'types/api/alerts/alertTypesV2';import { Labels } from 'types/api/alerts/def';-import { GetCreateAlertLocalStateFromAlertDefReturn } from '../types';-export interface ICreateAlertContextProps {alertState: AlertState;setAlertState: Dispatch<CreateAlertAction>;@@ -52,7 +50,7 @@ export interface ICreateAlertContextProps {export interface ICreateAlertProviderProps {children: React.ReactNode;initialAlertType: AlertTypes;- initialAlertState?: GetCreateAlertLocalStateFromAlertDefReturn;+ initialAlertState?: CreateAlertState;isEditMode?: boolean;ruleId?: string;}@@ -272,3 +270,31 @@ export type NotificationSettingsAction =| { type: 'SET_ROUTING_POLICIES'; payload: boolean }| { type: 'SET_INITIAL_STATE'; payload: NotificationSettingsState }| { type: 'RESET' };++export type CreateAlertState = {+ basic: AlertState;+ threshold: AlertThresholdState;+ advancedOptions: AdvancedOptionsState;+ evaluationWindow: EvaluationWindowState;+ notificationSettings: NotificationSettingsState;+};++export enum CreateAlertSlice {+ BASIC = 'basic',+ THRESHOLD = 'threshold',+ ADVANCED_OPTIONS = 'advancedOptions',+ EVALUATION_WINDOW = 'evaluationWindow',+ NOTIFICATION_SETTINGS = 'notificationSettings',+}++export type CreateAlertReducerAction =+ | { slice: CreateAlertSlice.BASIC; action: CreateAlertAction }+ | { slice: CreateAlertSlice.THRESHOLD; action: AlertThresholdAction }+ | { slice: CreateAlertSlice.ADVANCED_OPTIONS; action: AdvancedOptionsAction }+ | { slice: CreateAlertSlice.EVALUATION_WINDOW; action: EvaluationWindowAction }+ | {+ slice: CreateAlertSlice.NOTIFICATION_SETTINGS;+ action: NotificationSettingsAction;+ }+ | { type: 'RESET' }+ | { type: 'SET_INITIAL_STATE'; payload: CreateAlertState };diff --git a/frontend/src/container/CreateAlertV2/context/utils.tsx b/frontend/src/container/CreateAlertV2/context/utils.tsxindex f4d9347454..ef502dbe12 100644--- a/frontend/src/container/CreateAlertV2/context/utils.tsx+++ b/frontend/src/container/CreateAlertV2/context/utils.tsx@@ -18,6 +18,7 @@ import {INITIAL_ADVANCED_OPTIONS_STATE,INITIAL_ALERT_STATE,INITIAL_ALERT_THRESHOLD_STATE,+ INITIAL_CREATE_ALERT_STATE,INITIAL_EVALUATION_WINDOW_STATE,INITIAL_NOTIFICATION_SETTINGS_STATE,} from './constants';@@ -28,6 +29,9 @@ import {AlertThresholdAction,AlertThresholdState,CreateAlertAction,+ CreateAlertReducerAction,+ CreateAlertSlice,+ CreateAlertState,EvaluationWindowAction,EvaluationWindowState,NotificationSettingsAction,@@ -251,3 +255,57 @@ export const notificationSettingsReducer = (return state;}};++export const createAlertReducer = (+ state: CreateAlertState,+ action: CreateAlertReducerAction,+): CreateAlertState => {+ // Global actions+ if ('type' in action) {+ switch (action.type) {+ case 'RESET':+ return INITIAL_CREATE_ALERT_STATE;+ case 'SET_INITIAL_STATE':+ return action.payload;+ default:+ return state;+ }+ }++ // Slice actions+ switch (action.slice) {+ case CreateAlertSlice.BASIC:+ return { ...state, basic: alertCreationReducer(state.basic, action.action) };+ case CreateAlertSlice.THRESHOLD:+ return {+ ...state,+ threshold: alertThresholdReducer(state.threshold, action.action),+ };+ case CreateAlertSlice.ADVANCED_OPTIONS:+ return {+ ...state,+ advancedOptions: advancedOptionsReducer(+ state.advancedOptions,+ action.action,+ ),+ };+ case CreateAlertSlice.EVALUATION_WINDOW:+ return {+ ...state,+ evaluationWindow: evaluationWindowReducer(+ state.evaluationWindow,+ action.action,+ ),+ };+ case CreateAlertSlice.NOTIFICATION_SETTINGS:+ return {+ ...state,+ notificationSettings: notificationSettingsReducer(+ state.notificationSettings,+ action.action,+ ),+ };+ default:+ return state;+ }+};diff --git a/frontend/src/container/CreateAlertV2/utils.tsx b/frontend/src/container/CreateAlertV2/utils.tsxindex adab21a270..29acfd7a73 100644--- a/frontend/src/container/CreateAlertV2/utils.tsx+++ b/frontend/src/container/CreateAlertV2/utils.tsx@@ -21,11 +21,11 @@ import {AlertThresholdMatchType,AlertThresholdOperator,AlertThresholdState,+ CreateAlertState,EvaluationWindowState,NotificationSettingsState,} from './context/types';import { EVALUATION_WINDOW_TIMEFRAME } from './EvaluationSettings/constants';-import { GetCreateAlertLocalStateFromAlertDefReturn } from './types';export function Spinner(): JSX.Element | null {const { isCreatingAlertRule, isUpdatingAlertRule } = useCreateAlertState();@@ -265,14 +265,14 @@ export function getThresholdStateFromAlertDef(export function getCreateAlertLocalStateFromAlertDef(alertDef: PostableAlertRuleV2 | undefined,-): GetCreateAlertLocalStateFromAlertDefReturn {+): CreateAlertState {if (!alertDef) {return {- basicAlertState: INITIAL_ALERT_STATE,- thresholdState: INITIAL_ALERT_THRESHOLD_STATE,- advancedOptionsState: INITIAL_ADVANCED_OPTIONS_STATE,- evaluationWindowState: INITIAL_EVALUATION_WINDOW_STATE,- notificationSettingsState: INITIAL_NOTIFICATION_SETTINGS_STATE,+ basic: INITIAL_ALERT_STATE,+ threshold: INITIAL_ALERT_THRESHOLD_STATE,+ advancedOptions: INITIAL_ADVANCED_OPTIONS_STATE,+ evaluationWindow: INITIAL_EVALUATION_WINDOW_STATE,+ notificationSettings: INITIAL_NOTIFICATION_SETTINGS_STATE,};}// Basic alert state@@ -294,10 +294,10 @@ export function getCreateAlertLocalStateFromAlertDef();return {- basicAlertState,- thresholdState,- advancedOptionsState,- evaluationWindowState,- notificationSettingsState,+ basic: basicAlertState,+ threshold: thresholdState,+ advancedOptions: advancedOptionsState,+ evaluationWindow: evaluationWindowState,+ notificationSettings: notificationSettingsState,};}