grafana/public/app/features/alerting/state/reducers.ts
Ryan McKinley 3f15170914 Refactor: move some files to @grafana/data (#17952)
* moving to data WIP

* more refactoring

* add missing test

* mock full path

* remove sinon from grafana-ui
2019-07-06 08:05:53 +02:00

57 lines
1.5 KiB
TypeScript

import { AlertRuleDTO, AlertRule, AlertRulesState } from 'app/types';
import { Action, ActionTypes } from './actions';
import alertDef from './alertDef';
import { dateTime } from '@grafana/data';
export const initialState: AlertRulesState = { items: [], searchQuery: '', isLoading: false };
function convertToAlertRule(dto: AlertRuleDTO, state: string): AlertRule {
const stateModel = alertDef.getStateDisplayModel(state);
const rule: AlertRule = {
...dto,
stateText: stateModel.text,
stateIcon: stateModel.iconClass,
stateClass: stateModel.stateClass,
stateAge: dateTime(dto.newStateDate).fromNow(true),
};
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,
};