Read about our upcoming Code of Conduct on this issue

embed.vue 2.89 KB
Newer Older
Tristan Read's avatar
Tristan Read committed
1
2
<script>
import { mapActions, mapState } from 'vuex';
Simon Knox's avatar
Simon Knox committed
3
import { getParameterValues, removeParams } from '~/lib/utils/url_utility';
Tristan Read's avatar
Tristan Read committed
4
import GraphGroup from './graph_group.vue';
5
import MonitorTimeSeriesChart from './charts/time_series.vue';
Simon Knox's avatar
Simon Knox committed
6
import { sidebarAnimationDuration } from '../constants';
Tristan Read's avatar
Tristan Read committed
7
8
9
10
11
12
13
import { getTimeDiff } from '../utils';

let sidebarMutationObserver;

export default {
  components: {
    GraphGroup,
14
    MonitorTimeSeriesChart,
Tristan Read's avatar
Tristan Read committed
15
16
17
18
19
20
21
22
  },
  props: {
    dashboardUrl: {
      type: String,
      required: true,
    },
  },
  data() {
Simon Knox's avatar
Simon Knox committed
23
24
25
26
27
28
29
30
31
    const defaultRange = getTimeDiff();
    const start = getParameterValues('start', this.dashboardUrl)[0] || defaultRange.start;
    const end = getParameterValues('end', this.dashboardUrl)[0] || defaultRange.end;

    const params = {
      start,
      end,
    };

Tristan Read's avatar
Tristan Read committed
32
    return {
Simon Knox's avatar
Simon Knox committed
33
      params,
Tristan Read's avatar
Tristan Read committed
34
35
36
37
      elWidth: 0,
    };
  },
  computed: {
38
    ...mapState('monitoringDashboard', ['dashboard', 'metricsWithData']),
39
    charts() {
40
41
42
      if (!this.dashboard || !this.dashboard.panel_groups) {
        return [];
      }
43
      const groupWithMetrics = this.dashboard.panel_groups.find(group =>
44
45
        group.panels.find(chart => this.chartHasData(chart)),
      ) || { panels: [] };
46

47
      return groupWithMetrics.panels.filter(chart => this.chartHasData(chart));
48
49
50
    },
    isSingleChart() {
      return this.charts.length === 1;
Tristan Read's avatar
Tristan Read committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    },
  },
  mounted() {
    this.setInitialState();
    this.fetchMetricsData(this.params);
    sidebarMutationObserver = new MutationObserver(this.onSidebarMutation);
    sidebarMutationObserver.observe(document.querySelector('.layout-page'), {
      attributes: true,
      childList: false,
      subtree: false,
    });
  },
  beforeDestroy() {
    if (sidebarMutationObserver) {
      sidebarMutationObserver.disconnect();
    }
  },
  methods: {
    ...mapActions('monitoringDashboard', [
      'fetchMetricsData',
      'setEndpoints',
      'setFeatureFlags',
      'setShowErrorBanner',
    ]),
75
76
    chartHasData(chart) {
      return chart.metrics.some(metric => this.metricsWithData.includes(metric.metric_id));
Tristan Read's avatar
Tristan Read committed
77
78
79
80
81
82
83
84
    },
    onSidebarMutation() {
      setTimeout(() => {
        this.elWidth = this.$el.clientWidth;
      }, sidebarAnimationDuration);
    },
    setInitialState() {
      this.setEndpoints({
Simon Knox's avatar
Simon Knox committed
85
        dashboardEndpoint: removeParams(['start', 'end'], this.dashboardUrl),
Tristan Read's avatar
Tristan Read committed
86
87
88
89
90
91
92
      });
      this.setShowErrorBanner(false);
    },
  },
};
</script>
<template>
93
94
  <div class="metrics-embed" :class="{ 'd-inline-flex col-lg-6 p-0': isSingleChart }">
    <div v-if="charts.length" class="row w-100 m-n2 pb-4">
95
      <monitor-time-series-chart
96
        v-for="graphData in charts"
Tristan Read's avatar
Tristan Read committed
97
        :key="graphData.title"
98
        class="w-100"
Tristan Read's avatar
Tristan Read committed
99
100
        :graph-data="graphData"
        :container-width="elWidth"
101
        :group-id="dashboardUrl"
Tristan Read's avatar
Tristan Read committed
102
103
        :project-path="null"
        :show-border="true"
104
        :single-embed="isSingleChart"
Tristan Read's avatar
Tristan Read committed
105
106
107
108
      />
    </div>
  </div>
</template>