all files / src/chart/ chartReducer.js

38.78% Statements 19/49
33.33% Branches 6/18
13.33% Functions 2/15
29.27% Lines 12/41
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 15926×                                                                                                                                                                                                                                                                                                      
I/* eslint camelcase: 0 */
import PropTypes from 'prop-types';
 
import { now } from '../modules/dates';
import * as actions from './chartAction';
import { t } from '../locales';
 
export const chartPropType = {
  chartKey: PropTypes.string.isRequired,
  chartAlert: PropTypes.string,
  chartStatus: PropTypes.string,
  chartUpdateEndTime: PropTypes.number,
  chartUpdateStartTime: PropTypes.number,
  latestQueryFormData: PropTypes.object,
  queryRequest: PropTypes.object,
  queryResponse: PropTypes.object,
  triggerQuery: PropTypes.bool,
  lastRendered: PropTypes.number,
};
 
export const chart = {
  chartKey: '',
  chartAlert: null,
  chartStatus: 'loading',
  chartUpdateEndTime: null,
  chartUpdateStartTime: now(),
  latestQueryFormData: {},
  queryRequest: null,
  queryResponse: null,
  triggerQuery: true,
  lastRendered: 0,
};
 
export default function chartReducer(charts = {}, action) {
  const actionHandlers = {
    [actions.CHART_UPDATE_SUCCEEDED](state) {
      return { ...state,
        chartStatus: 'success',
        queryResponse: action.queryResponse,
        chartUpdateEndTime: now(),
      };
    },
    [actions.CHART_UPDATE_STARTED](state) {
      return { ...state,
        chartStatus: 'loading',
        chartAlert: null,
        chartUpdateEndTime: null,
        chartUpdateStartTime: now(),
        queryRequest: action.queryRequest,
      };
    },
    [actions.CHART_UPDATE_STOPPED](state) {
      return { ...state,
        chartStatus: 'stopped',
        chartAlert: t('Updating chart was stopped'),
      };
    },
    [actions.CHART_RENDERING_SUCCEEDED](state) {
      return { ...state,
        chartStatus: 'rendered',
      };
    },
    [actions.CHART_RENDERING_FAILED](state) {
      return { ...state,
        chartStatus: 'failed',
        chartAlert: t('An error occurred while rendering the visualization: %s', action.error),
      };
    },
    [actions.CHART_UPDATE_TIMEOUT](state) {
      return { ...state,
        chartStatus: 'failed',
        chartAlert: (
            `${t('Query timeout')} - ` +
            t(`visualization queries are set to timeout at ${action.timeout} seconds. `) +
            t('Perhaps your data has grown, your database is under unusual load, ' +
                'or you are simply querying a data source that is too large ' +
                'to be processed within the timeout range. ' +
                'If that is the case, we recommend that you summarize your data further.')),
      };
    },
    [actions.CHART_UPDATE_FAILED](state) {
      return { ...state,
        chartStatus: 'failed',
        chartAlert: action.queryResponse ? action.queryResponse.error : t('Network error.'),
        chartUpdateEndTime: now(),
        queryResponse: action.queryResponse,
      };
    },
    [actions.TRIGGER_QUERY](state) {
      return { ...state, triggerQuery: action.value };
    },
    [actions.RENDER_TRIGGERED](state) {
      return { ...state, lastRendered: action.value };
    },
    [actions.UPDATE_QUERY_FORM_DATA](state) {
      return { ...state, latestQueryFormData: action.value };
    },
    [actions.ANNOTATION_QUERY_STARTED](state) {
      if (state.annotationQuery &&
        state.annotationQuery[action.annotation.name]) {
        state.annotationQuery[action.annotation.name].abort();
      }
      const annotationQuery = {
        ...state.annotationQuery,
        [action.annotation.name]: action.queryRequest,
      };
      return {
        ...state,
        annotationQuery,
      };
    },
    [actions.ANNOTATION_QUERY_SUCCESS](state) {
      const annotationData = {
        ...state.annotationData,
        [action.annotation.name]: action.queryResponse.data,
      };
      const annotationError = { ...state.annotationError };
      delete annotationError[action.annotation.name];
      const annotationQuery = { ...state.annotationQuery };
      delete annotationQuery[action.annotation.name];
      return {
        ...state,
        annotationData,
        annotationError,
        annotationQuery,
      };
    },
    [actions.ANNOTATION_QUERY_FAILED](state) {
      const annotationData = { ...state.annotationData };
      delete annotationData[action.annotation.name];
      const annotationError = {
        ...state.annotationError,
        [action.annotation.name]: action.queryResponse ?
          action.queryResponse.error : t('Network error.'),
      };
      const annotationQuery = { ...state.annotationQuery };
      delete annotationQuery[action.annotation.name];
      return {
        ...state,
        annotationData,
        annotationError,
        annotationQuery,
      };
    },
  };
 
  /* eslint-disable no-param-reassign */
  if (Iaction.type === actions.REMOVE_CHART) {
    delete charts[action.key];
    return charts;
  }
 
  if (Iaction.type in actionHandlers) {
    return { ...charts, [action.key]: actionHandlers[action.type](charts[action.key], action) };
  }
 
  return charts;
}