All files / ee/app/assets/javascripts/analytics/productivity_analytics/store/modules/charts actions.js

100% Statements 36/36
90.9% Branches 10/11
100% Functions 12/12
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                  4x 53x 212x 159x         4x 217x   4x   218x 216x   216x   216x     46x   46x 1x           1x   45x     170x       4x       89x     4x     175x 175x     4x 6x   6x     4x 2x     2x     2x     4x 2x   2x   1x     1x       4x 1x  
import axios from '~/lib/utils/axios_utils';
import { getDateInPast } from '~/lib/utils/datetime_utility';
import { chartKeys, scatterPlotAddonQueryDays } from '../../../constants';
import { transformScatterData } from '../../../utils';
import * as types from './mutation_types';
 
/**
 * Fetches data for all charts except for the main chart
 */
export const fetchSecondaryChartData = ({ state, dispatch }) => {
  Object.keys(state.charts).forEach((chartKey) => {
    if (chartKey !== chartKeys.main) {
      dispatch('fetchChartData', chartKey);
    }
  });
};
 
export const requestChartData = ({ commit }, chartKey) =>
  commit(types.REQUEST_CHART_DATA, chartKey);
 
export const fetchChartData = ({ dispatch, getters, state, rootState }, chartKey) => {
  // let's fetch data for enabled charts only
  if (state.charts[chartKey].enabled) {
    dispatch('requestChartData', chartKey);
 
    const params = getters.getFilterParams(chartKey);
 
    axios
      .get(rootState.endpoint, { params })
      .then((response) => {
        const { data } = response;
 
        if (chartKey === chartKeys.scatterplot) {
          const transformedData = transformScatterData(
            data,
            getDateInPast(rootState.filters.startDate, scatterPlotAddonQueryDays),
            new Date(rootState.filters.endDate),
          );
 
          dispatch('receiveChartDataSuccess', { chartKey, data, transformedData });
        } else {
          dispatch('receiveChartDataSuccess', { chartKey, data });
        }
      })
      .catch((error) => dispatch('receiveChartDataError', { chartKey, error }));
  }
};
 
export const receiveChartDataSuccess = (
  { commit },
  { chartKey, data = {}, transformedData = null },
) => {
  commit(types.RECEIVE_CHART_DATA_SUCCESS, { chartKey, data, transformedData });
};
 
export const receiveChartDataError = ({ commit }, { chartKey, error }) => {
  const {
    response: { status },
  } = error;
  commit(types.RECEIVE_CHART_DATA_ERROR, { chartKey, status });
};
 
export const setMetricType = ({ commit, dispatch }, { chartKey, metricType }) => {
  commit(types.SET_METRIC_TYPE, { chartKey, metricType });
 
  dispatch('fetchChartData', chartKey);
};
 
export const updateSelectedItems = ({ commit, dispatch }, { chartKey, item }) => {
  commit(types.UPDATE_SELECTED_CHART_ITEMS, { chartKey, item });
 
  // update secondary charts
  dispatch('fetchSecondaryChartData');
 
  // let's reset the page on the MR table and fetch data
  dispatch('table/setPage', 0, { root: true });
};
 
export const resetMainChartSelection = ({ commit, dispatch }, skipReload = false) => {
  commit(types.UPDATE_SELECTED_CHART_ITEMS, { chartKey: chartKeys.main, item: null });
 
  if (!skipReload) {
    // update secondary charts
    dispatch('fetchSecondaryChartData');
 
    // let's reset the page on the MR table and fetch data
    dispatch('table/setPage', 0, { root: true });
  }
};
 
export const setChartEnabled = ({ commit }, { chartKey, isEnabled }) =>
  commit(types.SET_CHART_ENABLED, { chartKey, isEnabled });