debian-mirror-gitlab/app/assets/javascripts/logs/stores/actions.js

206 lines
5.9 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import { backOff } from '~/lib/utils/common_utils';
import httpStatusCodes from '~/lib/utils/http_status';
import axios from '~/lib/utils/axios_utils';
import { convertToFixedRange } from '~/lib/utils/datetime_range';
2020-07-28 23:09:34 +05:30
import { TOKEN_TYPE_POD_NAME, tracking, logExplorerOptions } from '../constants';
2020-06-23 00:09:42 +05:30
import trackLogs from '../logs_tracking_helper';
2020-04-08 14:13:33 +05:30
import * as types from './mutation_types';
const requestUntilData = (url, params) =>
backOff((next, stop) => {
axios
.get(url, { params })
.then(res => {
if (res.status === httpStatusCodes.ACCEPTED) {
next();
return;
}
stop(res);
})
.catch(err => {
stop(err);
});
});
2020-04-22 19:07:51 +05:30
const requestLogsUntilData = ({ commit, state }) => {
2020-04-08 14:13:33 +05:30
const params = {};
2020-07-28 23:09:34 +05:30
const type = state.environments.current
? logExplorerOptions.environments
: logExplorerOptions.managedApps;
const selectedObj = state[type].options.find(({ name }) => name === state[type].current);
const path =
type === logExplorerOptions.environments
? selectedObj.logs_api_path
: selectedObj.gitlab_managed_apps_logs_path;
2020-04-08 14:13:33 +05:30
if (state.pods.current) {
params.pod_name = state.pods.current;
}
if (state.search) {
params.search = state.search;
}
if (state.timeRange.current) {
try {
const { start, end } = convertToFixedRange(state.timeRange.current);
2020-04-22 19:07:51 +05:30
params.start_time = start;
params.end_time = end;
2020-04-08 14:13:33 +05:30
} catch {
2020-04-22 19:07:51 +05:30
commit(types.SHOW_TIME_RANGE_INVALID_WARNING);
2020-04-08 14:13:33 +05:30
}
}
if (state.logs.cursor) {
params.cursor = state.logs.cursor;
}
2020-07-28 23:09:34 +05:30
return requestUntilData(path, params);
2020-04-08 14:13:33 +05:30
};
2020-04-22 19:07:51 +05:30
/**
* Converts filters emitted by the component, e.g. a filterered-search
* to parameters to be applied to the filters of the store
* @param {Array} filters - List of strings or objects to filter by.
* @returns {Object} - An object with `search` and `podName` keys.
*/
const filtersToParams = (filters = []) => {
// Strings become part of the `search`
const search = filters
.filter(f => typeof f === 'string')
.join(' ')
.trim();
// null podName to show all pods
const podName = filters.find(f => f?.type === TOKEN_TYPE_POD_NAME)?.value?.data ?? null;
return { search, podName };
};
2020-04-08 14:13:33 +05:30
export const setInitData = ({ commit }, { timeRange, environmentName, podName }) => {
commit(types.SET_TIME_RANGE, timeRange);
commit(types.SET_PROJECT_ENVIRONMENT, environmentName);
commit(types.SET_CURRENT_POD_NAME, podName);
};
2020-04-22 19:07:51 +05:30
export const showFilteredLogs = ({ dispatch, commit }, filters = []) => {
const { podName, search } = filtersToParams(filters);
2020-04-08 14:13:33 +05:30
commit(types.SET_CURRENT_POD_NAME, podName);
2020-04-22 19:07:51 +05:30
commit(types.SET_SEARCH, search);
2020-06-23 00:09:42 +05:30
dispatch('fetchLogs', tracking.USED_SEARCH_BAR);
2020-04-08 14:13:33 +05:30
};
2020-04-22 19:07:51 +05:30
export const showPodLogs = ({ dispatch, commit }, podName) => {
commit(types.SET_CURRENT_POD_NAME, podName);
2020-06-23 00:09:42 +05:30
dispatch('fetchLogs', tracking.POD_LOG_CHANGED);
2020-04-08 14:13:33 +05:30
};
export const setTimeRange = ({ dispatch, commit }, timeRange) => {
commit(types.SET_TIME_RANGE, timeRange);
2020-06-23 00:09:42 +05:30
dispatch('fetchLogs', tracking.TIME_RANGE_SET);
2020-04-08 14:13:33 +05:30
};
export const showEnvironment = ({ dispatch, commit }, environmentName) => {
commit(types.SET_PROJECT_ENVIRONMENT, environmentName);
2020-06-23 00:09:42 +05:30
dispatch('fetchLogs', tracking.ENVIRONMENT_SELECTED);
};
2020-07-28 23:09:34 +05:30
export const showManagedApp = ({ dispatch, commit }, managedApp) => {
commit(types.SET_MANAGED_APP, managedApp);
dispatch('fetchLogs', tracking.MANAGED_APP_SELECTED);
};
2020-06-23 00:09:42 +05:30
export const refreshPodLogs = ({ dispatch, commit }) => {
commit(types.REFRESH_POD_LOGS);
dispatch('fetchLogs', tracking.REFRESH_POD_LOGS);
2020-04-08 14:13:33 +05:30
};
/**
* Fetch environments data and initial logs
* @param {Object} store
* @param {String} environmentsPath
*/
export const fetchEnvironments = ({ commit, dispatch }, environmentsPath) => {
commit(types.REQUEST_ENVIRONMENTS_DATA);
return axios
.get(environmentsPath)
.then(({ data }) => {
commit(types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS, data.environments);
2020-06-23 00:09:42 +05:30
dispatch('fetchLogs', tracking.ENVIRONMENT_SELECTED);
2020-04-08 14:13:33 +05:30
})
.catch(() => {
commit(types.RECEIVE_ENVIRONMENTS_DATA_ERROR);
});
};
2020-07-28 23:09:34 +05:30
/**
* Fetch managed apps data
* @param {Object} store
* @param {String} clustersPath
*/
export const fetchManagedApps = ({ commit }, clustersPath) => {
return axios
.get(clustersPath)
.then(({ data }) => {
commit(types.RECEIVE_MANAGED_APPS_DATA_SUCCESS, data.clusters);
})
.catch(() => {
commit(types.RECEIVE_MANAGED_APPS_DATA_ERROR);
});
};
2020-06-23 00:09:42 +05:30
export const fetchLogs = ({ commit, state }, trackingLabel) => {
2020-04-08 14:13:33 +05:30
commit(types.REQUEST_LOGS_DATA);
2020-04-22 19:07:51 +05:30
return requestLogsUntilData({ commit, state })
2020-04-08 14:13:33 +05:30
.then(({ data }) => {
const { pod_name, pods, logs, cursor } = data;
2020-06-23 00:09:42 +05:30
if (logs && logs.length > 0) {
trackLogs(trackingLabel);
}
2020-04-22 19:07:51 +05:30
commit(types.RECEIVE_LOGS_DATA_SUCCESS, { logs, cursor });
2020-04-08 14:13:33 +05:30
commit(types.SET_CURRENT_POD_NAME, pod_name);
commit(types.RECEIVE_PODS_DATA_SUCCESS, pods);
})
.catch(() => {
commit(types.RECEIVE_PODS_DATA_ERROR);
commit(types.RECEIVE_LOGS_DATA_ERROR);
});
};
export const fetchMoreLogsPrepend = ({ commit, state }) => {
if (state.logs.isComplete) {
// return when all logs are loaded
return Promise.resolve();
}
commit(types.REQUEST_LOGS_DATA_PREPEND);
2020-04-22 19:07:51 +05:30
return requestLogsUntilData({ commit, state })
2020-04-08 14:13:33 +05:30
.then(({ data }) => {
const { logs, cursor } = data;
commit(types.RECEIVE_LOGS_DATA_PREPEND_SUCCESS, { logs, cursor });
})
.catch(() => {
commit(types.RECEIVE_LOGS_DATA_PREPEND_ERROR);
});
};
2020-04-22 19:07:51 +05:30
export const dismissRequestEnvironmentsError = ({ commit }) => {
commit(types.HIDE_REQUEST_ENVIRONMENTS_ERROR);
};
export const dismissRequestLogsError = ({ commit }) => {
commit(types.HIDE_REQUEST_LOGS_ERROR);
};
export const dismissInvalidTimeRangeWarning = ({ commit }) => {
commit(types.HIDE_TIME_RANGE_INVALID_WARNING);
};
2020-04-08 14:13:33 +05:30
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};