summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_merge_request_widget/components/mr_widget_memory_usage.js
blob: 395cc9e91fc25188da5215181fe5ceff288105fa (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
import statusCodes from '~/lib/utils/http_status';
import MemoryGraph from '../../vue_shared/components/memory_graph';
import MRWidgetService from '../services/mr_widget_service';

export default {
  name: 'MemoryUsage',
  props: {
    mr: { type: Object, required: true },
    service: { type: Object, required: true },
    metricsUrl: { type: String, required: true },
  },
  data() {
    return {
      // memoryFrom: 0,
      // memoryTo: 0,
      memoryMetrics: [],
      hasMetrics: false,
      loadFailed: false,
      loadingMetrics: true,
      backOffRequestCounter: 0,
    };
  },
  components: {
    'mr-memory-graph': MemoryGraph,
  },
  methods: {
    computeGraphData(metrics) {
      this.loadingMetrics = false;
      const { memory_values } = metrics;
      // if (memory_previous.length > 0) {
      //   this.memoryFrom = Number(memory_previous[0].value[1]).toFixed(2);
      // }
      //
      // if (memory_current.length > 0) {
      //   this.memoryTo = Number(memory_current[0].value[1]).toFixed(2);
      // }

      if (memory_values.length > 0) {
        this.hasMetrics = true;
        this.memoryMetrics = memory_values[0].values;
      }
    },
  },
  mounted() {
    this.$props.loadingMetrics = true;
    gl.utils.backOff((next, stop) => {
      MRWidgetService.fetchMetrics(this.$props.metricsUrl)
        .then((res) => {
          if (res.status === statusCodes.NO_CONTENT) {
            this.backOffRequestCounter = this.backOffRequestCounter += 1;
            if (this.backOffRequestCounter < 3) {
              next();
            } else {
              stop(res);
            }
          } else {
            stop(res);
          }
        })
        .catch(stop);
    })
    .then((res) => {
      if (res.status === statusCodes.NO_CONTENT) {
        return res;
      }

      return res.json();
    })
    .then((res) => {
      this.computeGraphData(res.metrics);
      return res;
    })
    .catch(() => {
      this.$props.loadFailed = true;
    });
  },
  template: `
    <div class="mr-info-list mr-memory-usage">
      <div class="legend"></div>
      <p
        v-if="loadingMetrics"
        class="usage-info usage-info-loading">
        <i
          class="fa fa-spinner fa-spin usage-info-load-spinner"
          aria-hidden="true" />Loading deployment statistics.
      </p>
      <p
        v-if="!hasMetrics && !loadingMetrics"
        class="usage-info usage-info-loading">
        Deployment statistics are not available currently.
      </p>
      <p
        v-if="hasMetrics"
        class="usage-info">
        Deployment memory usage:
      </p>
      <p
        v-if="loadFailed"
        class="usage-info">
        Failed to load deployment statistics.
      </p>
      <mr-memory-graph
        v-if="hasMetrics"
        :metrics="memoryMetrics"
        height="25"
        width="100" />
    </div>
  `,
};