Welcome to mirror list, hosted at ThFree Co, Russian Federation.

actions.js « stores « logs « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: be847108a491c6764c6c82efcc3324f30bab9a08 (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
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';

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);
      });
  });

const requestLogsUntilData = ({ commit, state }) => {
  const params = {};
  const { logs_api_path } = state.environments.options.find(
    ({ name }) => name === state.environments.current,
  );

  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);
      params.start_time = start;
      params.end_time = end;
    } catch {
      commit(types.SHOW_TIME_RANGE_INVALID_WARNING);
    }
  }
  if (state.logs.cursor) {
    params.cursor = state.logs.cursor;
  }

  return requestUntilData(logs_api_path, params);
};

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);
};

export const showPodLogs = ({ dispatch, commit }, podName) => {
  commit(types.SET_CURRENT_POD_NAME, podName);
  dispatch('fetchLogs');
};

export const setSearch = ({ dispatch, commit }, searchQuery) => {
  commit(types.SET_SEARCH, searchQuery);
  dispatch('fetchLogs');
};

export const setTimeRange = ({ dispatch, commit }, timeRange) => {
  commit(types.SET_TIME_RANGE, timeRange);
  dispatch('fetchLogs');
};

export const showEnvironment = ({ dispatch, commit }, environmentName) => {
  commit(types.SET_PROJECT_ENVIRONMENT, environmentName);
  dispatch('fetchLogs');
};

/**
 * 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);
      dispatch('fetchLogs');
    })
    .catch(() => {
      commit(types.RECEIVE_ENVIRONMENTS_DATA_ERROR);
    });
};

export const fetchLogs = ({ commit, state }) => {
  commit(types.REQUEST_LOGS_DATA);

  return requestLogsUntilData({ commit, state })
    .then(({ data }) => {
      const { pod_name, pods, logs, cursor } = data;
      commit(types.RECEIVE_LOGS_DATA_SUCCESS, { logs, cursor });
      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);

  return requestLogsUntilData({ commit, state })
    .then(({ data }) => {
      const { logs, cursor } = data;
      commit(types.RECEIVE_LOGS_DATA_PREPEND_SUCCESS, { logs, cursor });
    })
    .catch(() => {
      commit(types.RECEIVE_LOGS_DATA_PREPEND_ERROR);
    });
};

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);
};

// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};