dashboard.vue 4.93 KB
Newer Older
1 2
<script>
  import _ from 'underscore';
Phil Hughes's avatar
Phil Hughes committed
3
  import Flash from '../../flash';
4
  import MonitoringService from '../services/monitoring_service';
5
  import GraphGroup from './graph_group.vue';
6
  import Graph from './graph.vue';
7
  import EmptyState from './empty_state.vue';
8 9 10 11
  import MonitoringStore from '../stores/monitoring_store';
  import eventHub from '../event_hub';

  export default {
Filipa Lacerda's avatar
Filipa Lacerda committed
12 13 14 15 16 17
    components: {
      Graph,
      GraphGroup,
      EmptyState,
    },

18 19
    props: {
      hasMetrics: {
20 21 22
        type: Boolean,
        required: false,
        default: true,
23
      },
24 25 26 27 28
      showLegend: {
        type: Boolean,
        required: false,
        default: true,
      },
29 30 31 32 33
      showPanels: {
        type: Boolean,
        required: false,
        default: true,
      },
34 35 36 37 38
      forceSmallGraph: {
        type: Boolean,
        required: false,
        default: false,
      },
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
      documentationPath: {
        type: String,
        required: true,
      },
      settingsPath: {
        type: String,
        required: true,
      },
      clustersPath: {
        type: String,
        required: true,
      },
      tagsPath: {
        type: String,
        required: true,
      },
      projectPath: {
        type: String,
        required: true,
      },
      metricsEndpoint: {
        type: String,
        required: true,
      },
      deploymentEndpoint: {
        type: String,
        required: false,
        default: null,
      },
      emptyGettingStartedSvgPath: {
        type: String,
        required: true,
      },
      emptyLoadingSvgPath: {
        type: String,
        required: true,
      },
76 77 78 79
      emptyNoDataSvgPath: {
        type: String,
        required: true,
      },
80 81 82 83 84
      emptyUnableToConnectSvgPath: {
        type: String,
        required: true,
      },
    },
85

86
    data() {
87
      return {
88
        store: new MonitoringStore(),
89 90 91 92
        state: 'gettingStarted',
        showEmptyState: true,
        updateAspectRatio: false,
        updatedAspectRatios: 0,
93
        hoverData: {},
94 95 96 97
        resizeThrottled: {},
      };
    },

Filipa Lacerda's avatar
Filipa Lacerda committed
98 99 100 101 102 103 104 105 106 107 108 109 110
    created() {
      this.service = new MonitoringService({
        metricsEndpoint: this.metricsEndpoint,
        deploymentEndpoint: this.deploymentEndpoint,
      });
      eventHub.$on('toggleAspectRatio', this.toggleAspectRatio);
      eventHub.$on('hoverChanged', this.hoverChanged);
    },

    beforeDestroy() {
      eventHub.$off('toggleAspectRatio', this.toggleAspectRatio);
      eventHub.$off('hoverChanged', this.hoverChanged);
      window.removeEventListener('resize', this.resizeThrottled, false);
111 112
    },

Filipa Lacerda's avatar
Filipa Lacerda committed
113 114
    mounted() {
      this.resizeThrottled = _.throttle(this.resize, 600);
115
      if (!this.hasMetrics) {
Filipa Lacerda's avatar
Filipa Lacerda committed
116 117 118 119 120 121
        this.state = 'gettingStarted';
      } else {
        this.getGraphsData();
        window.addEventListener('resize', this.resizeThrottled, false);
      }
    },
122

123 124 125
    methods: {
      getGraphsData() {
        this.state = 'loading';
126 127 128 129 130 131 132
        Promise.all([
          this.service.getGraphsData()
            .then(data => this.store.storeMetrics(data)),
          this.service.getDeploymentData()
            .then(data => this.store.storeDeploymentData(data))
            .catch(() => new Flash('Error getting deployment information.')),
        ])
133 134 135 136 137 138 139
          .then(() => {
            if (this.store.groups.length < 1) {
              this.state = 'noData';
              return;
            }
            this.showEmptyState = false;
          })
140
          .catch(() => { this.state = 'unableToConnect'; });
141 142 143 144 145 146 147 148 149 150 151 152 153
      },

      resize() {
        this.updateAspectRatio = true;
      },

      toggleAspectRatio() {
        this.updatedAspectRatios = this.updatedAspectRatios += 1;
        if (this.store.getMetricsCount() === this.updatedAspectRatios) {
          this.updateAspectRatio = !this.updateAspectRatio;
          this.updatedAspectRatios = 0;
        }
      },
154 155 156 157

      hoverChanged(data) {
        this.hoverData = data;
      },
158 159 160
    },
  };
</script>
161

162
<template>
Filipa Lacerda's avatar
Filipa Lacerda committed
163 164 165 166
  <div
    v-if="!showEmptyState"
    class="prometheus-graphs"
  >
167
    <graph-group
168
      v-for="(groupData, index) in store.groups"
169
      :key="index"
170
      :name="groupData.group"
171
      :show-panels="showPanels"
172
    >
173 174
      <graph
        v-for="(graphData, index) in groupData.metrics"
175
        :key="index"
176
        :graph-data="graphData"
177
        :hover-data="hoverData"
178 179
        :update-aspect-ratio="updateAspectRatio"
        :deployment-data="store.deploymentData"
180 181
        :project-path="projectPath"
        :tags-path="tagsPath"
182
        :show-legend="showLegend"
183
        :small-graph="forceSmallGraph"
184 185
      />
    </graph-group>
186
  </div>
187
  <empty-state
188
    v-else
189 190 191
    :selected-state="state"
    :documentation-path="documentationPath"
    :settings-path="settingsPath"
192
    :clusters-path="clustersPath"
193 194
    :empty-getting-started-svg-path="emptyGettingStartedSvgPath"
    :empty-loading-svg-path="emptyLoadingSvgPath"
195
    :empty-no-data-svg-path="emptyNoDataSvgPath"
196
    :empty-unable-to-connect-svg-path="emptyUnableToConnectSvgPath"
197 198
  />
</template>