| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import { AlertRuleDTO, AlertRule, AlertRulesState } from 'app/types';
- import { Action, ActionTypes } from './actions';
- import alertDef from './alertDef';
- import { dateTime } from '@grafana/ui/src/utils/moment_wrapper';
- export const initialState: AlertRulesState = { items: [], searchQuery: '', isLoading: false };
- function convertToAlertRule(rule, state): AlertRule {
- const stateModel = alertDef.getStateDisplayModel(state);
- rule.stateText = stateModel.text;
- rule.stateIcon = stateModel.iconClass;
- rule.stateClass = stateModel.stateClass;
- rule.stateAge = dateTime(rule.newStateDate)
- .fromNow()
- .replace(' ago', '');
- if (rule.state !== 'paused') {
- if (rule.executionError) {
- rule.info = 'Execution Error: ' + rule.executionError;
- }
- if (rule.evalData && rule.evalData.noData) {
- rule.info = 'Query returned no data';
- }
- }
- return rule;
- }
- export const alertRulesReducer = (state = initialState, action: Action): AlertRulesState => {
- switch (action.type) {
- case ActionTypes.LoadAlertRules: {
- return { ...state, isLoading: true };
- }
- case ActionTypes.LoadedAlertRules: {
- const alertRules: AlertRuleDTO[] = action.payload;
- const alertRulesViewModel: AlertRule[] = alertRules.map(rule => {
- return convertToAlertRule(rule, rule.state);
- });
- return { ...state, items: alertRulesViewModel, isLoading: false };
- }
- case ActionTypes.SetSearchQuery:
- return { ...state, searchQuery: action.payload };
- }
- return state;
- };
- export default {
- alertRules: alertRulesReducer,
- };
|