The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/alerting/unified/reducers/alertmanager/muteTimings.ts

75 lines
3.3 KiB

import { createAction, createReducer } from '@reduxjs/toolkit';
import { remove } from 'lodash';
import { AlertManagerCortexConfig, MuteTimeInterval } from 'app/plugins/datasource/alertmanager/types';
import { removeTimeIntervalFromRoute, renameTimeInterval } from '../../utils/alertmanager';
export const addMuteTimingAction = createAction<{ interval: MuteTimeInterval }>('muteTiming/add');
export const updateMuteTimingAction = createAction<{
interval: MuteTimeInterval;
originalName: string;
}>('muteTiming/update');
export const deleteMuteTimingAction = createAction<{ name: string }>('muteTiming/delete');
const initialState: AlertManagerCortexConfig = {
alertmanager_config: {},
template_files: {},
};
/**
* This reducer will manage action related to mute timings and make sure all operations on the alertmanager
* configuration happen immutably and only mutate what they need.
*/
export const muteTimingsReducer = createReducer(initialState, (builder) => {
builder
// add a mute timing to the alertmanager configuration
.addCase(addMuteTimingAction, (draft, { payload }) => {
const { interval } = payload;
draft.alertmanager_config.time_intervals = (draft.alertmanager_config.time_intervals ?? []).concat(interval);
})
// add a mute timing to the alertmanager configuration
// make sure we update the mute timing in either the deprecated or the new time intervals property
.addCase(updateMuteTimingAction, (draft, { payload }) => {
const { interval, originalName } = payload;
const nameHasChanged = interval.name !== originalName;
const timeIntervals = draft.alertmanager_config.time_intervals ?? [];
const muteTimeIntervals = draft.alertmanager_config.mute_time_intervals ?? [];
const existingIntervalIndex = timeIntervals.findIndex(({ name }) => name === originalName);
if (existingIntervalIndex !== -1) {
timeIntervals[existingIntervalIndex] = interval;
}
const existingMuteIntervalIndex = muteTimeIntervals.findIndex(({ name }) => name === originalName);
if (existingMuteIntervalIndex !== -1) {
muteTimeIntervals[existingMuteIntervalIndex] = interval;
}
if (nameHasChanged && draft.alertmanager_config.route) {
draft.alertmanager_config.route = renameTimeInterval(
interval.name,
originalName,
draft.alertmanager_config.route
);
}
})
// delete a mute timing from the alertmanager configuration, since the configuration might be using the "deprecated" mute_time_intervals
// let's also check there
.addCase(deleteMuteTimingAction, (draft, { payload }) => {
const { name } = payload;
const { alertmanager_config } = draft;
const { time_intervals = [], mute_time_intervals = [] } = alertmanager_config;
// remove the mute timings from the legacy and new time intervals definition
remove(time_intervals, (interval) => interval.name === name);
remove(mute_time_intervals, (interval) => interval.name === name);
// remove the mute timing from all routes
alertmanager_config.route = removeTimeIntervalFromRoute(name, alertmanager_config.route ?? {});
})
.addDefaultCase((_state, action) => {
throw new Error(`Unknown action for mute timing reducer: ${action.type}`);
});
});