summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/cycle_analytics/store/actions.js
blob: a7a2c8ea9d3648d67f4bab5445b0b4da97bd3103 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import {
  getProjectValueStreamStages,
  getProjectValueStreams,
  getProjectValueStreamMetrics,
  getValueStreamStageMedian,
  getValueStreamStageRecords,
  getValueStreamStageCounts,
} from '~/api/analytics_api';
import createFlash from '~/flash';
import { __ } from '~/locale';
import { DEFAULT_VALUE_STREAM, I18N_VSA_ERROR_STAGE_MEDIAN } from '../constants';
import * as types from './mutation_types';

export const setSelectedValueStream = ({ commit, dispatch }, valueStream) => {
  commit(types.SET_SELECTED_VALUE_STREAM, valueStream);
  return Promise.all([dispatch('fetchValueStreamStages'), dispatch('fetchCycleAnalyticsData')]);
};

export const fetchValueStreamStages = ({ commit, state }) => {
  const {
    endpoints: { fullPath },
    selectedValueStream: { id },
  } = state;
  commit(types.REQUEST_VALUE_STREAM_STAGES);

  return getProjectValueStreamStages(fullPath, id)
    .then(({ data }) => commit(types.RECEIVE_VALUE_STREAM_STAGES_SUCCESS, data))
    .catch(({ response: { status } }) => {
      commit(types.RECEIVE_VALUE_STREAM_STAGES_ERROR, status);
    });
};

export const receiveValueStreamsSuccess = ({ commit, dispatch }, data = []) => {
  commit(types.RECEIVE_VALUE_STREAMS_SUCCESS, data);
  if (data.length) {
    const [firstStream] = data;
    return dispatch('setSelectedValueStream', firstStream);
  }
  return dispatch('setSelectedValueStream', DEFAULT_VALUE_STREAM);
};

export const fetchValueStreams = ({ commit, dispatch, state }) => {
  const {
    endpoints: { fullPath },
  } = state;
  commit(types.REQUEST_VALUE_STREAMS);

  const stageRequests = ['setSelectedStage', 'fetchStageMedians', 'fetchStageCountValues'];
  return getProjectValueStreams(fullPath)
    .then(({ data }) => dispatch('receiveValueStreamsSuccess', data))
    .then(() => Promise.all(stageRequests.map((r) => dispatch(r))))
    .catch(({ response: { status } }) => {
      commit(types.RECEIVE_VALUE_STREAMS_ERROR, status);
    });
};
export const fetchCycleAnalyticsData = ({
  state: {
    endpoints: { requestPath },
  },
  getters: { legacyFilterParams },
  commit,
}) => {
  commit(types.REQUEST_CYCLE_ANALYTICS_DATA);

  return getProjectValueStreamMetrics(requestPath, legacyFilterParams)
    .then(({ data }) => commit(types.RECEIVE_CYCLE_ANALYTICS_DATA_SUCCESS, data))
    .catch(() => {
      commit(types.RECEIVE_CYCLE_ANALYTICS_DATA_ERROR);
      createFlash({
        message: __('There was an error while fetching value stream summary data.'),
      });
    });
};

export const fetchStageData = ({ getters: { requestParams, filterParams }, commit }) => {
  commit(types.REQUEST_STAGE_DATA);

  return getValueStreamStageRecords(requestParams, filterParams)
    .then(({ data }) => {
      // when there's a query timeout, the request succeeds but the error is encoded in the response data
      if (data?.error) {
        commit(types.RECEIVE_STAGE_DATA_ERROR, data.error);
      } else {
        commit(types.RECEIVE_STAGE_DATA_SUCCESS, data);
      }
    })
    .catch(() => commit(types.RECEIVE_STAGE_DATA_ERROR));
};

const getStageMedians = ({ stageId, vsaParams, filterParams = {} }) => {
  return getValueStreamStageMedian({ ...vsaParams, stageId }, filterParams).then(({ data }) => ({
    id: stageId,
    value: data?.value || null,
  }));
};

export const fetchStageMedians = ({
  state: { stages },
  getters: { requestParams: vsaParams, filterParams },
  commit,
}) => {
  commit(types.REQUEST_STAGE_MEDIANS);
  return Promise.all(
    stages.map(({ id: stageId }) =>
      getStageMedians({
        vsaParams,
        stageId,
        filterParams,
      }),
    ),
  )
    .then((data) => commit(types.RECEIVE_STAGE_MEDIANS_SUCCESS, data))
    .catch((error) => {
      commit(types.RECEIVE_STAGE_MEDIANS_ERROR, error);
      createFlash({ message: I18N_VSA_ERROR_STAGE_MEDIAN });
    });
};

const getStageCounts = ({ stageId, vsaParams, filterParams = {} }) => {
  return getValueStreamStageCounts({ ...vsaParams, stageId }, filterParams).then(({ data }) => ({
    id: stageId,
    ...data,
  }));
};

export const fetchStageCountValues = ({
  state: { stages },
  getters: { requestParams: vsaParams, filterParams },
  commit,
}) => {
  commit(types.REQUEST_STAGE_COUNTS);
  return Promise.all(
    stages.map(({ id: stageId }) =>
      getStageCounts({
        vsaParams,
        stageId,
        filterParams,
      }),
    ),
  )
    .then((data) => commit(types.RECEIVE_STAGE_COUNTS_SUCCESS, data))
    .catch((error) => {
      commit(types.RECEIVE_STAGE_COUNTS_ERROR, error);
      createFlash({
        message: __('There was an error fetching stage total counts'),
      });
    });
};

export const setSelectedStage = ({ dispatch, commit, state: { stages } }, selectedStage = null) => {
  const stage = selectedStage || stages[0];
  commit(types.SET_SELECTED_STAGE, stage);
  return dispatch('fetchStageData');
};

export const setLoading = ({ commit }, value) => commit(types.SET_LOADING, value);

const refetchStageData = (dispatch) => {
  return Promise.resolve()
    .then(() => dispatch('setLoading', true))
    .then(() =>
      Promise.all([
        dispatch('fetchCycleAnalyticsData'),
        dispatch('fetchStageData'),
        dispatch('fetchStageMedians'),
      ]),
    )
    .finally(() => dispatch('setLoading', false));
};

export const setFilters = ({ dispatch }) => refetchStageData(dispatch);

export const setDateRange = ({ dispatch, commit }, daysInPast) => {
  commit(types.SET_DATE_RANGE, daysInPast);
  return refetchStageData(dispatch);
};

export const initializeVsa = ({ commit, dispatch }, initialData = {}) => {
  commit(types.INITIALIZE_VSA, initialData);

  return dispatch('setLoading', true)
    .then(() => dispatch('fetchValueStreams'))
    .finally(() => dispatch('setLoading', false));
};