grafana/public/app/features/alerting/state/actions.ts

60 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-09-02 13:36:03 -05:00
import { Dispatch } from 'redux';
import { getBackendSrv } from 'app/core/services/backend_srv';
2018-09-04 08:00:04 -05:00
import { AlertRuleApi, StoreState } from 'app/types';
2018-09-02 13:36:03 -05:00
2018-09-03 06:46:39 -05:00
export enum ActionTypes {
LoadAlertRules = 'LOAD_ALERT_RULES',
SetSearchQuery = 'SET_SEARCH_QUERY',
}
2018-09-02 13:36:03 -05:00
export interface LoadAlertRulesAction {
2018-09-03 06:46:39 -05:00
type: ActionTypes.LoadAlertRules;
2018-09-04 08:00:04 -05:00
payload: AlertRuleApi[];
2018-09-02 13:36:03 -05:00
}
2018-09-03 06:46:39 -05:00
export interface SetSearchQueryAction {
type: ActionTypes.SetSearchQuery;
payload: string;
}
2018-09-04 08:00:04 -05:00
export const loadAlertRules = (rules: AlertRuleApi[]): LoadAlertRulesAction => ({
2018-09-03 06:46:39 -05:00
type: ActionTypes.LoadAlertRules,
2018-09-02 13:36:03 -05:00
payload: rules,
});
2018-09-03 06:46:39 -05:00
export const setSearchQuery = (query: string): SetSearchQueryAction => ({
type: ActionTypes.SetSearchQuery,
payload: query,
});
export type Action = LoadAlertRulesAction | SetSearchQueryAction;
2018-09-02 13:36:03 -05:00
2018-09-02 14:08:31 -05:00
export const getAlertRulesAsync = (options: { state: string }) => async (
dispatch: Dispatch<Action>
2018-09-04 08:00:04 -05:00
): Promise<AlertRuleApi[]> => {
2018-09-02 13:36:03 -05:00
try {
2018-09-02 14:08:31 -05:00
const rules = await getBackendSrv().get('/api/alerts', options);
2018-09-02 13:36:03 -05:00
dispatch(loadAlertRules(rules));
return rules;
} catch (error) {
console.error(error);
throw error;
}
};
export const togglePauseAlertRule = (id: number, options: { paused: boolean }) => async (
// Maybe fix dispatch type?
dispatch: Dispatch<any>,
getState: () => StoreState
): Promise<boolean> => {
try {
await getBackendSrv().post(`/api/alerts/${id}/pause`, options);
const stateFilter = getState().location.query.state || 'all';
dispatch(getAlertRulesAsync({ state: stateFilter.toString() }));
return true;
} catch (error) {
console.log(error);
throw error;
}
};