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

225 lines
6.8 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-11-18 22:05:49 +05:30
import { normalizeHeaders, parseIntPagination } from '~/lib/utils/common_utils';
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-12-11 22:18:48 +05:30
return dispatch('fetchValueStreamStages');
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);
return getProjectValueStreams(fullPath)
.then(({ data }) => dispatch('receiveValueStreamsSuccess', data))
.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-11-18 22:05:49 +05:30
export const fetchStageData = ({
getters: { requestParams, filterParams, paginationParams },
commit,
}) => {
2021-06-08 01:23:25 +05:30
commit(types.REQUEST_STAGE_DATA);
2021-11-18 22:05:49 +05:30
return getValueStreamStageRecords(requestParams, { ...filterParams, ...paginationParams })
.then(({ data, headers }) => {
2021-09-04 01:27:46 +05:30
// 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-11-18 22:05:49 +05:30
const { page = null, nextPage = null } = parseIntPagination(normalizeHeaders(headers));
commit(types.SET_PAGINATION, { ...paginationParams, page, hasNextPage: Boolean(nextPage) });
2021-09-04 01:27:46 +05:30
}
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-12-11 22:18:48 +05:30
export const fetchValueStreamStageData = ({ dispatch }) =>
Promise.all([
dispatch('fetchCycleAnalyticsData'),
dispatch('fetchStageData'),
dispatch('fetchStageMedians'),
dispatch('fetchStageCountValues'),
]);
export const refetchStageData = async ({ dispatch, commit }) => {
commit(types.SET_LOADING, true);
await dispatch('fetchValueStreamStageData');
commit(types.SET_LOADING, false);
2021-09-04 01:27:46 +05:30
};
2021-12-11 22:18:48 +05:30
export const setSelectedStage = ({ dispatch, commit }, selectedStage = null) => {
commit(types.SET_SELECTED_STAGE, selectedStage);
return dispatch('refetchStageData');
2021-06-08 01:23:25 +05:30
};
2021-12-11 22:18:48 +05:30
export const setFilters = ({ dispatch }) => dispatch('refetchStageData');
2021-09-30 23:02:18 +05:30
2021-11-11 11:23:49 +05:30
export const setDateRange = ({ dispatch, commit }, { createdAfter, createdBefore }) => {
commit(types.SET_DATE_RANGE, { createdAfter, createdBefore });
2021-12-11 22:18:48 +05:30
return dispatch('refetchStageData');
};
export const setInitialStage = ({ dispatch, commit, state: { stages } }, stage) => {
const selectedStage = stage || stages[0];
commit(types.SET_SELECTED_STAGE, selectedStage);
return dispatch('fetchValueStreamStageData');
2021-09-04 01:27:46 +05:30
};
2021-06-08 01:23:25 +05:30
2021-11-18 22:05:49 +05:30
export const updateStageTablePagination = (
{ commit, dispatch, state: { selectedStage } },
paginationParams,
) => {
commit(types.SET_PAGINATION, paginationParams);
return dispatch('fetchStageData', selectedStage.id);
};
2021-12-11 22:18:48 +05:30
export const initializeVsa = async ({ commit, dispatch }, initialData = {}) => {
2021-06-08 01:23:25 +05:30
commit(types.INITIALIZE_VSA, initialData);
2021-10-27 15:23:28 +05:30
2021-11-11 11:23:49 +05:30
const {
endpoints: { fullPath, groupPath, milestonesPath = '', labelsPath = '' },
2021-12-11 22:18:48 +05:30
selectedAuthor,
selectedMilestone,
selectedAssigneeList,
selectedLabelList,
selectedStage = null,
2021-11-11 11:23:49 +05:30
} = initialData;
2021-12-11 22:18:48 +05:30
2021-11-11 11:23:49 +05:30
dispatch('filters/setEndpoints', {
labelsEndpoint: labelsPath,
milestonesEndpoint: milestonesPath,
groupEndpoint: groupPath,
projectEndpoint: fullPath,
});
2021-12-11 22:18:48 +05:30
dispatch('filters/initialize', {
selectedAuthor,
selectedMilestone,
selectedAssigneeList,
selectedLabelList,
});
commit(types.SET_LOADING, true);
await dispatch('fetchValueStreams');
await dispatch('setInitialStage', selectedStage);
commit(types.SET_LOADING, false);
2021-06-08 01:23:25 +05:30
};