mirror of https://github.com/grafana/grafana
Alerting: Introduce useProduceNewAlertmanagerConfiguration (#91623)
useProduceNewAlertmanagerConfigurationpull/91404/head
parent
8136fbef1f
commit
e9e5989549
@ -0,0 +1,19 @@ |
||||
interface RequestState { |
||||
error?: unknown; |
||||
|
||||
isUninitialized: boolean; |
||||
isSuccess: boolean; |
||||
isError: boolean; |
||||
isLoading: boolean; |
||||
} |
||||
|
||||
// @TODO what to do with the other props that we get from RTKQ's state such as originalArgs, etc?
|
||||
export function mergeRequestStates(...states: RequestState[]): RequestState { |
||||
return { |
||||
error: states.find((s) => s.error), |
||||
isUninitialized: states.every((s) => s.isUninitialized), |
||||
isSuccess: states.every((s) => s.isSuccess), |
||||
isError: states.some((s) => s.isError), |
||||
isLoading: states.some((s) => s.isLoading), |
||||
}; |
||||
} |
@ -0,0 +1,64 @@ |
||||
import { Action } from '@reduxjs/toolkit'; |
||||
import reduceReducers from 'reduce-reducers'; |
||||
|
||||
import { AlertManagerCortexConfig } from 'app/plugins/datasource/alertmanager/types'; |
||||
|
||||
import { alertmanagerApi } from '../api/alertmanagerApi'; |
||||
import { useAlertmanager } from '../state/AlertmanagerContext'; |
||||
|
||||
import { mergeRequestStates } from './mergeRequestStates'; |
||||
|
||||
const ERR_NO_ACTIVE_AM = new Error('no active Alertmanager'); |
||||
|
||||
const { useLazyGetAlertmanagerConfigurationQuery, useUpdateAlertmanagerConfigurationMutation } = alertmanagerApi; |
||||
|
||||
export const initialAlertmanagerConfiguration: AlertManagerCortexConfig = { |
||||
alertmanager_config: { |
||||
receivers: [], |
||||
route: {}, |
||||
time_intervals: [], |
||||
mute_time_intervals: [], |
||||
inhibit_rules: [], |
||||
templates: [], |
||||
}, |
||||
template_files: {}, |
||||
}; |
||||
|
||||
const configurationReducer = reduceReducers(initialAlertmanagerConfiguration); |
||||
|
||||
/** |
||||
* This hook will make sure we are always applying actions that mutate the Alertmanager configuration |
||||
* on top of the latest Alertmanager configuration object. |
||||
*/ |
||||
export function useProduceNewAlertmanagerConfiguration() { |
||||
const { selectedAlertmanager } = useAlertmanager(); |
||||
|
||||
const [fetchAlertmanagerConfig, fetchAlertmanagerState] = useLazyGetAlertmanagerConfigurationQuery(); |
||||
const [updateAlertManager, updateAlertmanagerState] = useUpdateAlertmanagerConfigurationMutation(); |
||||
|
||||
const newConfigurationState = mergeRequestStates(fetchAlertmanagerState, updateAlertmanagerState); |
||||
|
||||
if (!selectedAlertmanager) { |
||||
throw ERR_NO_ACTIVE_AM; |
||||
} |
||||
|
||||
/** |
||||
* This function will fetch the latest Alertmanager configuration, apply a diff to it via a reducer and |
||||
* returns the result. |
||||
* |
||||
* ┌────────────────────────────┐ ┌───────────────┐ ┌───────────────────┐ |
||||
* │ fetch latest configuration │─▶│ apply reducer │─▶│ new rule config │ |
||||
* └────────────────────────────┘ └───────────────┘ └───────────────────┘ |
||||
*/ |
||||
const produceNewAlertmanagerConfiguration = async (action: Action) => { |
||||
const currentAlertmanagerConfiguration = await fetchAlertmanagerConfig(selectedAlertmanager).unwrap(); |
||||
const newConfig = configurationReducer(currentAlertmanagerConfiguration, action); |
||||
|
||||
return updateAlertManager({ |
||||
selectedAlertmanager, |
||||
config: newConfig, |
||||
}).unwrap(); |
||||
}; |
||||
|
||||
return [produceNewAlertmanagerConfiguration, newConfigurationState] as const; |
||||
} |
Loading…
Reference in new issue