reducers.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { AlertRuleDTO, AlertRule, AlertRulesState } from 'app/types';
  2. import { Action, ActionTypes } from './actions';
  3. import alertDef from './alertDef';
  4. import { dateTime } from '@grafana/data';
  5. export const initialState: AlertRulesState = { items: [], searchQuery: '', isLoading: false };
  6. function convertToAlertRule(dto: AlertRuleDTO, state: string): AlertRule {
  7. const stateModel = alertDef.getStateDisplayModel(state);
  8. const rule: AlertRule = {
  9. ...dto,
  10. stateText: stateModel.text,
  11. stateIcon: stateModel.iconClass,
  12. stateClass: stateModel.stateClass,
  13. stateAge: dateTime(dto.newStateDate).fromNow(true),
  14. };
  15. if (rule.state !== 'paused') {
  16. if (rule.executionError) {
  17. rule.info = 'Execution Error: ' + rule.executionError;
  18. }
  19. if (rule.evalData && rule.evalData.noData) {
  20. rule.info = 'Query returned no data';
  21. }
  22. }
  23. return rule;
  24. }
  25. export const alertRulesReducer = (state = initialState, action: Action): AlertRulesState => {
  26. switch (action.type) {
  27. case ActionTypes.LoadAlertRules: {
  28. return { ...state, isLoading: true };
  29. }
  30. case ActionTypes.LoadedAlertRules: {
  31. const alertRules: AlertRuleDTO[] = action.payload;
  32. const alertRulesViewModel: AlertRule[] = alertRules.map(rule => {
  33. return convertToAlertRule(rule, rule.state);
  34. });
  35. return { ...state, items: alertRulesViewModel, isLoading: false };
  36. }
  37. case ActionTypes.SetSearchQuery:
  38. return { ...state, searchQuery: action.payload };
  39. }
  40. return state;
  41. };
  42. export default {
  43. alertRules: alertRulesReducer,
  44. };