summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/serverless/serverless_bundle.js
blob: 47a510d5fb59736cde8d09395e9215c287921508 (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
145
146
147
148
149
import Visibility from 'visibilityjs';
import Vue from 'vue';
import { s__ } from '../locale';
import Flash from '../flash';
import Poll from '../lib/utils/poll';
import ServerlessStore from './stores/serverless_store';
import ServerlessDetailsStore from './stores/serverless_details_store';
import GetFunctionsService from './services/get_functions_service';
import Functions from './components/functions.vue';
import FunctionDetails from './components/function_details.vue';

export default class Serverless {
  constructor() {
    if (document.querySelector('.js-serverless-function-details-page') != null) {
      const {
        serviceName,
        serviceDescription,
        serviceEnvironment,
        serviceUrl,
        serviceNamespace,
        servicePodcount,
      } = document.querySelector('.js-serverless-function-details-page').dataset;
      const el = document.querySelector('#js-serverless-function-details');
      this.store = new ServerlessDetailsStore();
      const { store } = this;

      const service = {
        name: serviceName,
        description: serviceDescription,
        environment: serviceEnvironment,
        url: serviceUrl,
        namespace: serviceNamespace,
        podcount: servicePodcount,
      };

      this.store.updateDetailedFunction(service);
      this.functionDetails = new Vue({
        el,
        data() {
          return {
            state: store.state,
          };
        },
        render(createElement) {
          return createElement(FunctionDetails, {
            props: {
              func: this.state.functionDetail,
            },
          });
        },
      });
    } else {
      const { statusPath, clustersPath, helpPath, installed } = document.querySelector(
        '.js-serverless-functions-page',
      ).dataset;

      this.service = new GetFunctionsService(statusPath);
      this.knativeInstalled = installed !== undefined;
      this.store = new ServerlessStore(this.knativeInstalled, clustersPath, helpPath);
      this.initServerless();
      this.functionLoadCount = 0;

      if (statusPath && this.knativeInstalled) {
        this.initPolling();
      }
    }
  }

  initServerless() {
    const { store } = this;
    const el = document.querySelector('#js-serverless-functions');

    this.functions = new Vue({
      el,
      data() {
        return {
          state: store.state,
        };
      },
      render(createElement) {
        return createElement(Functions, {
          props: {
            functions: this.state.functions,
            installed: this.state.installed,
            clustersPath: this.state.clustersPath,
            helpPath: this.state.helpPath,
            loadingData: this.state.loadingData,
            hasFunctionData: this.state.hasFunctionData,
          },
        });
      },
    });
  }

  initPolling() {
    this.poll = new Poll({
      resource: this.service,
      method: 'fetchData',
      successCallback: data => this.handleSuccess(data),
      errorCallback: () => Serverless.handleError(),
    });

    if (!Visibility.hidden()) {
      this.poll.makeRequest();
    } else {
      this.service
        .fetchData()
        .then(data => this.handleSuccess(data))
        .catch(() => Serverless.handleError());
    }

    Visibility.change(() => {
      if (!Visibility.hidden() && !this.destroyed) {
        this.poll.restart();
      } else {
        this.poll.stop();
      }
    });
  }

  handleSuccess(data) {
    if (data.status === 200) {
      this.store.updateFunctionsFromServer(data.data);
      this.store.updateLoadingState(false);
    } else if (data.status === 204) {
      /* Time out after 3 attempts to retrieve data */
      this.functionLoadCount += 1;
      if (this.functionLoadCount === 3) {
        this.poll.stop();
        this.store.toggleNoFunctionData();
      }
    }
  }

  static handleError() {
    Flash(s__('Serverless|An error occurred while retrieving serverless components'));
  }

  destroy() {
    this.destroyed = true;

    if (this.poll) {
      this.poll.stop();
    }

    this.functions.$destroy();
    this.functionDetails.$destroy();
  }
}