debian-mirror-gitlab/app/assets/javascripts/cycle_analytics/store/actions.js

185 lines
5.6 KiB
JavaScript
Raw Normal View History

2021-09-04 01:27:46 +05:30
import {
getProjectValueStreamStages,
getProjectValueStreams,
getProjectValueStreamMetrics,
2021-09-30 23:02:18 +05:30
getValueStreamStageMedian,
2021-10-27 15:23:28 +05:30
getValueStreamStageRecords,
getValueStreamStageCounts,
2021-09-04 01:27:46 +05:30
} from '~/api/analytics_api';
2021-06-08 01:23:25 +05:30
import createFlash from '~/flash';
import { __ } from '~/locale';
2021-10-27 15:23:28 +05:30
import { DEFAULT_VALUE_STREAM, I18N_VSA_ERROR_STAGE_MEDIAN } from '../constants';
2021-06-08 01:23:25 +05:30
import * as types from './mutation_types';
2021-09-04 01:27:46 +05:30
export const setSelectedValueStream = ({ commit, dispatch }, valueStream) => {
commit(types.SET_SELECTED_VALUE_STREAM, valueStream);
2021-10-27 15:23:28 +05:30
return Promise.all([dispatch('fetchValueStreamStages'), dispatch('fetchCycleAnalyticsData')]);
2021-09-04 01:27:46 +05:30
};
export const fetchValueStreamStages = ({ commit, state }) => {
2021-10-27 15:23:28 +05:30
const {
endpoints: { fullPath },
selectedValueStream: { id },
} = state;
2021-09-04 01:27:46 +05:30
commit(types.REQUEST_VALUE_STREAM_STAGES);
2021-10-27 15:23:28 +05:30
return getProjectValueStreamStages(fullPath, id)
2021-09-04 01:27:46 +05:30
.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 }) => {
2021-09-30 23:02:18 +05:30
const {
2021-10-27 15:23:28 +05:30
endpoints: { fullPath },
2021-09-30 23:02:18 +05:30
} = state;
2021-09-04 01:27:46 +05:30
commit(types.REQUEST_VALUE_STREAMS);
2021-10-27 15:23:28 +05:30
const stageRequests = ['setSelectedStage', 'fetchStageMedians', 'fetchStageCountValues'];
2021-09-04 01:27:46 +05:30
return getProjectValueStreams(fullPath)
.then(({ data }) => dispatch('receiveValueStreamsSuccess', data))
2021-09-30 23:02:18 +05:30
.then(() => Promise.all(stageRequests.map((r) => dispatch(r))))
2021-09-04 01:27:46 +05:30
.catch(({ response: { status } }) => {
commit(types.RECEIVE_VALUE_STREAMS_ERROR, status);
});
};
2021-09-30 23:02:18 +05:30
export const fetchCycleAnalyticsData = ({
2021-10-27 15:23:28 +05:30
state: {
endpoints: { requestPath },
},
2021-09-30 23:02:18 +05:30
getters: { legacyFilterParams },
commit,
}) => {
2021-06-08 01:23:25 +05:30
commit(types.REQUEST_CYCLE_ANALYTICS_DATA);
2021-09-30 23:02:18 +05:30
return getProjectValueStreamMetrics(requestPath, legacyFilterParams)
2021-06-08 01:23:25 +05:30
.then(({ data }) => commit(types.RECEIVE_CYCLE_ANALYTICS_DATA_SUCCESS, data))
.catch(() => {
commit(types.RECEIVE_CYCLE_ANALYTICS_DATA_ERROR);
createFlash({
2021-09-04 01:27:46 +05:30
message: __('There was an error while fetching value stream summary data.'),
2021-06-08 01:23:25 +05:30
});
});
};
2021-10-27 15:23:28 +05:30
export const fetchStageData = ({ getters: { requestParams, filterParams }, commit }) => {
2021-06-08 01:23:25 +05:30
commit(types.REQUEST_STAGE_DATA);
2021-10-27 15:23:28 +05:30
return getValueStreamStageRecords(requestParams, filterParams)
2021-09-04 01:27:46 +05:30
.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);
}
2021-06-08 01:23:25 +05:30
})
.catch(() => commit(types.RECEIVE_STAGE_DATA_ERROR));
};
2021-09-30 23:02:18 +05:30
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);
2021-10-27 15:23:28 +05:30
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);
2021-09-30 23:02:18 +05:30
createFlash({
2021-10-27 15:23:28 +05:30
message: __('There was an error fetching stage total counts'),
2021-09-30 23:02:18 +05:30
});
});
};
2021-09-04 01:27:46 +05:30
export const setSelectedStage = ({ dispatch, commit, state: { stages } }, selectedStage = null) => {
2021-06-08 01:23:25 +05:30
const stage = selectedStage || stages[0];
commit(types.SET_SELECTED_STAGE, stage);
2021-09-04 01:27:46 +05:30
return dispatch('fetchStageData');
};
2021-10-27 15:23:28 +05:30
export const setLoading = ({ commit }, value) => commit(types.SET_LOADING, value);
const refetchStageData = (dispatch) => {
2021-09-04 01:27:46 +05:30
return Promise.resolve()
2021-10-27 15:23:28 +05:30
.then(() => dispatch('setLoading', true))
.then(() =>
Promise.all([
dispatch('fetchCycleAnalyticsData'),
dispatch('fetchStageData'),
dispatch('fetchStageMedians'),
]),
)
.finally(() => dispatch('setLoading', false));
2021-06-08 01:23:25 +05:30
};
2021-10-27 15:23:28 +05:30
export const setFilters = ({ dispatch }) => refetchStageData(dispatch);
2021-09-30 23:02:18 +05:30
2021-10-27 15:23:28 +05:30
export const setDateRange = ({ dispatch, commit }, daysInPast) => {
commit(types.SET_DATE_RANGE, daysInPast);
return refetchStageData(dispatch);
2021-09-04 01:27:46 +05:30
};
2021-06-08 01:23:25 +05:30
export const initializeVsa = ({ commit, dispatch }, initialData = {}) => {
commit(types.INITIALIZE_VSA, initialData);
2021-10-27 15:23:28 +05:30
return dispatch('setLoading', true)
.then(() => dispatch('fetchValueStreams'))
.finally(() => dispatch('setLoading', false));
2021-06-08 01:23:25 +05:30
};