reducers.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { AlertRuleDTO, AlertRule, AlertRulesState } from 'app/types';
  2. import { Action, ActionTypes } from './actions';
  3. import alertDef from './alertDef';
  4. import { dateTime } from '@grafana/ui/src/utils/moment_wrapper';
  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)
  14. .fromNow()
  15. .replace(' ago', ''),
  16. };
  17. if (rule.state !== 'paused') {
  18. if (rule.executionError) {
  19. rule.info = 'Execution Error: ' + rule.executionError;
  20. }
  21. if (rule.evalData && rule.evalData.noData) {
  22. rule.info = 'Query returned no data';
  23. }
  24. }
  25. return rule;
  26. }
  27. export const alertRulesReducer = (state = initialState, action: Action): AlertRulesState => {
  28. switch (action.type) {
  29. case ActionTypes.LoadAlertRules: {
  30. return { ...state, isLoading: true };
  31. }
  32. case ActionTypes.LoadedAlertRules: {
  33. const alertRules: AlertRuleDTO[] = action.payload;
  34. const alertRulesViewModel: AlertRule[] = alertRules.map(rule => {
  35. return convertToAlertRule(rule, rule.state);
  36. });
  37. return { ...state, items: alertRulesViewModel, isLoading: false };
  38. }
  39. case ActionTypes.SetSearchQuery:
  40. return { ...state, searchQuery: action.payload };
  41. }
  42. return state;
  43. };
  44. export default {
  45. alertRules: alertRulesReducer,
  46. };