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

actions.js « store « clusters_list « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 97ed0a7ab3793b0ba718b589c28611f85a47e2e1 (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
import * as Sentry from '~/sentry/wrapper';
import Poll from '~/lib/utils/poll';
import axios from '~/lib/utils/axios_utils';
import { deprecatedCreateFlash as flash } from '~/flash';
import { __ } from '~/locale';
import { MAX_REQUESTS } from '../constants';
import { parseIntPagination, normalizeHeaders } from '~/lib/utils/common_utils';
import * as types from './mutation_types';

const allNodesPresent = (clusters, retryCount) => {
  /*
    Nodes are coming from external Kubernetes clusters.
    They may fail for reasons GitLab cannot control.
    MAX_REQUESTS will ensure this poll stops at some point.
  */
  return retryCount > MAX_REQUESTS || clusters.every((cluster) => cluster.nodes != null);
};

export const reportSentryError = (_store, { error, tag }) => {
  Sentry.withScope((scope) => {
    scope.setTag('javascript_clusters_list', tag);
    Sentry.captureException(error);
  });
};

export const fetchClusters = ({ state, commit, dispatch }) => {
  let retryCount = 0;

  commit(types.SET_LOADING_NODES, true);

  const poll = new Poll({
    resource: {
      fetchClusters: (paginatedEndPoint) => axios.get(paginatedEndPoint),
    },
    data: `${state.endpoint}?page=${state.page}`,
    method: 'fetchClusters',
    successCallback: ({ data, headers }) => {
      retryCount += 1;

      try {
        if (data.clusters) {
          const normalizedHeaders = normalizeHeaders(headers);
          const paginationInformation = parseIntPagination(normalizedHeaders);

          commit(types.SET_CLUSTERS_DATA, { data, paginationInformation });
          commit(types.SET_LOADING_CLUSTERS, false);

          if (allNodesPresent(data.clusters, retryCount)) {
            poll.stop();
            commit(types.SET_LOADING_NODES, false);
          }
        }
      } catch (error) {
        poll.stop();

        commit(types.SET_LOADING_CLUSTERS, false);
        commit(types.SET_LOADING_NODES, false);

        dispatch('reportSentryError', { error, tag: 'fetchClustersSuccessCallback' });
      }
    },
    errorCallback: (response) => {
      poll.stop();

      commit(types.SET_LOADING_CLUSTERS, false);
      commit(types.SET_LOADING_NODES, false);
      flash(__('Clusters|An error occurred while loading clusters'));

      dispatch('reportSentryError', { error: response, tag: 'fetchClustersErrorCallback' });
    },
  });

  poll.makeRequest();
};

export const setPage = ({ commit }, page) => {
  commit(types.SET_PAGE, page);
};