Read about our upcoming Code of Conduct on this issue

dashboard.vue 14.9 KB
Newer Older
1
<script>
2
3
4
import _ from 'underscore';
import { mapActions, mapState } from 'vuex';
import VueDraggable from 'vuedraggable';
5
6
7
8
import {
  GlButton,
  GlDropdown,
  GlDropdownItem,
9
  GlFormGroup,
10
11
12
13
  GlModal,
  GlModalDirective,
  GlTooltipDirective,
} from '@gitlab/ui';
14
import PanelType from 'ee_else_ce/monitoring/components/panel_type.vue';
15
import { s__ } from '~/locale';
16
import createFlash from '~/flash';
17
import Icon from '~/vue_shared/components/icon.vue';
18
import { getParameterValues, mergeUrlParams, redirectTo } from '~/lib/utils/url_utility';
19
import invalidUrl from '~/lib/utils/invalid_url';
20
import DateTimePicker from './date_time_picker/date_time_picker.vue';
21
import MonitorTimeSeriesChart from './charts/time_series.vue';
22
import MonitorSingleStatChart from './charts/single_stat.vue';
Mike Greiling's avatar
Mike Greiling committed
23
24
import GraphGroup from './graph_group.vue';
import EmptyState from './empty_state.vue';
25
26
import TrackEventDirective from '~/vue_shared/directives/track_event';
import { getTimeDiff, isValidDate, getAddMetricTrackingOptions } from '../utils';
27

Mike Greiling's avatar
Mike Greiling committed
28
29
export default {
  components: {
30
    VueDraggable,
31
    MonitorTimeSeriesChart,
32
33
    MonitorSingleStatChart,
    PanelType,
Mike Greiling's avatar
Mike Greiling committed
34
35
    GraphGroup,
    EmptyState,
36
    Icon,
Adriel Santiago's avatar
Adriel Santiago committed
37
    GlButton,
38
39
    GlDropdown,
    GlDropdownItem,
40
    GlFormGroup,
41
    GlModal,
42
    DateTimePicker,
43
44
  },
  directives: {
45
46
    GlModal: GlModalDirective,
    GlTooltip: GlTooltipDirective,
47
    TrackEvent: TrackEventDirective,
Mike Greiling's avatar
Mike Greiling committed
48
49
  },
  props: {
50
    externalDashboardUrl: {
Adriel Santiago's avatar
Adriel Santiago committed
51
52
53
54
      type: String,
      required: false,
      default: '',
    },
Mike Greiling's avatar
Mike Greiling committed
55
56
57
58
    hasMetrics: {
      type: Boolean,
      required: false,
      default: true,
Filipa Lacerda's avatar
Filipa Lacerda committed
59
    },
Mike Greiling's avatar
Mike Greiling committed
60
61
62
63
    showPanels: {
      type: Boolean,
      required: false,
      default: true,
64
    },
Mike Greiling's avatar
Mike Greiling committed
65
66
67
    documentationPath: {
      type: String,
      required: true,
68
    },
Mike Greiling's avatar
Mike Greiling committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    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,
    },
89
    deploymentsEndpoint: {
Mike Greiling's avatar
Mike Greiling committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
      type: String,
      required: false,
      default: null,
    },
    emptyGettingStartedSvgPath: {
      type: String,
      required: true,
    },
    emptyLoadingSvgPath: {
      type: String,
      required: true,
    },
    emptyNoDataSvgPath: {
      type: String,
      required: true,
    },
    emptyUnableToConnectSvgPath: {
      type: String,
      required: true,
    },
110
111
112
113
    environmentsEndpoint: {
      type: String,
      required: true,
    },
114
115
116
117
    currentEnvironmentName: {
      type: String,
      required: true,
    },
118
119
120
121
122
123
124
    customMetricsAvailable: {
      type: Boolean,
      required: false,
      default: false,
    },
    customMetricsPath: {
      type: String,
125
126
      required: false,
      default: invalidUrl,
127
128
129
    },
    validateQueryPath: {
      type: String,
130
131
      required: false,
      default: invalidUrl,
132
    },
133
134
135
136
137
    dashboardEndpoint: {
      type: String,
      required: false,
      default: invalidUrl,
    },
138
139
140
141
142
    currentDashboard: {
      type: String,
      required: false,
      default: '',
    },
143
144
145
146
147
    smallEmptyState: {
      type: Boolean,
      required: false,
      default: false,
    },
148
149
150
151
152
153
154
155
156
157
    alertsEndpoint: {
      type: String,
      required: false,
      default: null,
    },
    prometheusAlertsAvailable: {
      type: Boolean,
      required: false,
      default: false,
    },
158
159
160
161
162
    rearrangePanelsAvailable: {
      type: Boolean,
      required: false,
      default: false,
    },
Mike Greiling's avatar
Mike Greiling committed
163
164
165
166
  },
  data() {
    return {
      state: 'gettingStarted',
167
      formIsValid: null,
168
      selectedTimeWindow: {},
169
      isRearrangingPanels: false,
170
      hasValidDates: true,
Mike Greiling's avatar
Mike Greiling committed
171
172
    };
  },
173
174
175
176
  computed: {
    canAddMetrics() {
      return this.customMetricsAvailable && this.customMetricsPath.length;
    },
177
    ...mapState('monitoringDashboard', [
178
      'dashboard',
179
180
181
182
      'emptyState',
      'showEmptyState',
      'environments',
      'deploymentData',
183
184
      'metricsWithData',
      'useDashboardEndpoint',
185
      'allDashboards',
186
      'additionalPanelTypesEnabled',
187
    ]),
188
    firstDashboard() {
189
190
191
      return this.environmentsEndpoint.length > 0 && this.allDashboards.length > 0
        ? this.allDashboards[0]
        : {};
192
    },
193
194
195
    selectedDashboard() {
      return this.allDashboards.find(d => d.path === this.currentDashboard) || this.firstDashboard;
    },
196
    selectedDashboardText() {
197
      return this.selectedDashboard.display_name;
198
    },
199
200
201
    showRearrangePanelsBtn() {
      return !this.showEmptyState && this.rearrangePanelsAvailable;
    },
202
203
204
    addingMetricsAvailable() {
      return IS_EE && this.canAddMetrics && !this.showEmptyState;
    },
205
206
207
208
209
210
211
212
    hasHeaderButtons() {
      return (
        this.addingMetricsAvailable ||
        this.showRearrangePanelsBtn ||
        this.selectedDashboard.can_edit ||
        this.externalDashboardUrl.length
      );
    },
213
  },
Mike Greiling's avatar
Mike Greiling committed
214
  created() {
215
    this.setEndpoints({
Mike Greiling's avatar
Mike Greiling committed
216
      metricsEndpoint: this.metricsEndpoint,
217
      environmentsEndpoint: this.environmentsEndpoint,
218
      deploymentsEndpoint: this.deploymentsEndpoint,
219
      dashboardEndpoint: this.dashboardEndpoint,
220
      currentDashboard: this.currentDashboard,
221
      projectPath: this.projectPath,
Mike Greiling's avatar
Mike Greiling committed
222
223
224
225
    });
  },
  mounted() {
    if (!this.hasMetrics) {
226
      this.setGettingStartedEmptyState();
Mike Greiling's avatar
Mike Greiling committed
227
    } else {
Simon Knox's avatar
Simon Knox committed
228
229
230
231
232
233
234
235
236
      const defaultRange = getTimeDiff();
      const start = getParameterValues('start')[0] || defaultRange.start;
      const end = getParameterValues('end')[0] || defaultRange.end;

      const range = {
        start,
        end,
      };

237
      this.selectedTimeWindow = range;
Simon Knox's avatar
Simon Knox committed
238

239
      if (!isValidDate(start) || !isValidDate(end)) {
240
        this.hasValidDates = false;
241
242
        this.showInvalidDateError();
      } else {
243
        this.hasValidDates = true;
244
245
        this.fetchData(range);
      }
Mike Greiling's avatar
Mike Greiling committed
246
247
248
    }
  },
  methods: {
249
250
251
252
    ...mapActions('monitoringDashboard', [
      'fetchData',
      'setGettingStartedEmptyState',
      'setEndpoints',
253
      'setPanelGroupMetrics',
254
    ]),
255
    updateMetrics(key, panels) {
256
      this.setPanelGroupMetrics({
257
        panels,
258
259
260
261
262
263
264
265
        key,
      });
    },
    removeMetric(key, metrics, graphIndex) {
      this.setPanelGroupMetrics({
        metrics: metrics.filter((v, i) => i !== graphIndex),
        key,
      });
266
    },
267
268
269
270
271
    removeGraph(metrics, graphIndex) {
      // At present graphs will not be removed, they should removed using the vuex store
      // See https://gitlab.com/gitlab-org/gitlab/issues/27835
      metrics.splice(graphIndex, 1);
    },
272
273
274
    showInvalidDateError() {
      createFlash(s__('Metrics|Link contains an invalid time window.'));
    },
275
276
    generateLink(group, title, yLabel) {
      const dashboard = this.currentDashboard || this.firstDashboard.path;
277
      const params = _.pick({ dashboard, group, title, y_label: yLabel }, value => value != null);
278
279
      return mergeUrlParams(params, window.location.href);
    },
280
281
282
    hideAddMetricModal() {
      this.$refs.addMetricModal.hide();
    },
283
284
285
    toggleRearrangingPanels() {
      this.isRearrangingPanels = !this.isRearrangingPanels;
    },
286
287
288
289
290
291
    setFormValidity(isValid) {
      this.formIsValid = isValid;
    },
    submitCustomMetricsForm() {
      this.$refs.customMetricsForm.submit();
    },
292
293
294
295
296
    chartsWithData(panels) {
      return panels.filter(panel =>
        panel.metrics.some(metric => this.metricsWithData.includes(metric.metric_id)),
      );
    },
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
297
    groupHasData(group) {
298
      return this.chartsWithData(group.panels).length > 0;
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
299
    },
300
301
302
    onDateTimePickerApply(timeWindowUrlParams) {
      return redirectTo(mergeUrlParams(timeWindowUrlParams, window.location.href));
    },
303
    getAddMetricTrackingOptions,
Mike Greiling's avatar
Mike Greiling committed
304
  },
305
306
307
308
  addMetric: {
    title: s__('Metrics|Add metric'),
    modalId: 'add-metric',
  },
Mike Greiling's avatar
Mike Greiling committed
309
};
310
</script>
311

312
<template>
313
  <div class="prometheus-graphs">
314
    <div class="prometheus-graphs-header gl-p-3 pb-0 border-bottom bg-gray-light">
315
316
317
318
319
320
      <div class="row">
        <template v-if="environmentsEndpoint">
          <gl-form-group
            :label="__('Dashboard')"
            label-size="sm"
            label-for="monitor-dashboards-dropdown"
321
            class="col-sm-12 col-md-6 col-lg-2"
322
          >
323
324
325
326
327
            <gl-dropdown
              id="monitor-dashboards-dropdown"
              class="mb-0 d-flex js-dashboards-dropdown"
              toggle-class="dropdown-menu-toggle"
              :text="selectedDashboardText"
328
            >
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
              <gl-dropdown-item
                v-for="dashboard in allDashboards"
                :key="dashboard.path"
                :active="dashboard.path === currentDashboard"
                active-class="is-active"
                :href="`?dashboard=${dashboard.path}`"
                >{{ dashboard.display_name || dashboard.path }}</gl-dropdown-item
              >
            </gl-dropdown>
          </gl-form-group>

          <gl-form-group
            :label="s__('Metrics|Environment')"
            label-size="sm"
            label-for="monitor-environments-dropdown"
344
            class="col-sm-6 col-md-6 col-lg-2"
345
          >
346
347
348
349
350
351
            <gl-dropdown
              id="monitor-environments-dropdown"
              class="mb-0 d-flex js-environments-dropdown"
              toggle-class="dropdown-menu-toggle"
              :text="currentEnvironmentName"
              :disabled="environments.length === 0"
352
            >
353
354
355
356
357
358
359
360
361
362
363
364
              <gl-dropdown-item
                v-for="environment in environments"
                :key="environment.id"
                :active="environment.name === currentEnvironmentName"
                active-class="is-active"
                :href="environment.metrics_path"
                >{{ environment.name }}</gl-dropdown-item
              >
            </gl-dropdown>
          </gl-form-group>

          <gl-form-group
365
            v-if="hasValidDates"
366
367
368
            :label="s__('Metrics|Show last')"
            label-size="sm"
            label-for="monitor-time-window-dropdown"
369
            class="col-sm-6 col-md-6 col-lg-4"
370
          >
371
372
373
374
            <date-time-picker
              :selected-time-window="selectedTimeWindow"
              @onApply="onDateTimePickerApply"
            />
375
376
377
378
          </gl-form-group>
        </template>

        <gl-form-group
379
          v-if="hasHeaderButtons"
380
          label-for="prometheus-graphs-dropdown-buttons"
381
          class="dropdown-buttons col-md d-md-flex col-lg d-lg-flex align-items-end"
382
        >
383
          <div id="prometheus-graphs-dropdown-buttons">
384
385
386
387
388
389
390
391
392
            <gl-button
              v-if="showRearrangePanelsBtn"
              :pressed="isRearrangingPanels"
              variant="default"
              class="mr-2 mt-1 js-rearrange-button"
              @click="toggleRearrangingPanels"
            >
              {{ __('Arrange charts') }}
            </gl-button>
393
394
            <gl-button
              v-if="addingMetricsAvailable"
395
              ref="addMetricBtn"
396
              v-gl-modal="$options.addMetric.modalId"
397
              variant="outline-success"
398
              class="mr-2 mt-1"
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
            >
              {{ $options.addMetric.title }}
            </gl-button>
            <gl-modal
              v-if="addingMetricsAvailable"
              ref="addMetricModal"
              :modal-id="$options.addMetric.modalId"
              :title="$options.addMetric.title"
            >
              <form ref="customMetricsForm" :action="customMetricsPath" method="post">
                <custom-metrics-form-fields
                  :validate-query-path="validateQueryPath"
                  form-operation="post"
                  @formValidation="setFormValidity"
                />
              </form>
              <div slot="modal-footer">
                <gl-button @click="hideAddMetricModal">{{ __('Cancel') }}</gl-button>
                <gl-button
418
419
                  ref="submitCustomMetricsFormBtn"
                  v-track-event="getAddMetricTrackingOptions()"
420
421
422
423
424
425
426
427
428
                  :disabled="!formIsValid"
                  variant="success"
                  @click="submitCustomMetricsForm"
                >
                  {{ __('Save changes') }}
                </gl-button>
              </div>
            </gl-modal>

429
430
431
432
433
434
435
436
            <gl-button
              v-if="selectedDashboard.can_edit"
              class="mt-1 js-edit-link"
              :href="selectedDashboard.project_blob_path"
            >
              {{ __('Edit dashboard') }}
            </gl-button>

437
438
439
440
441
442
443
444
445
446
447
448
449
            <gl-button
              v-if="externalDashboardUrl.length"
              class="mt-1 js-external-dashboard-link"
              variant="primary"
              :href="externalDashboardUrl"
              target="_blank"
              rel="noopener noreferrer"
            >
              {{ __('View full dashboard') }}
              <icon name="external-link" />
            </gl-button>
          </div>
        </gl-form-group>
450
      </div>
Jose's avatar
Jose committed
451
    </div>
452

453
454
    <div v-if="!showEmptyState">
      <graph-group
455
        v-for="(groupData, index) in dashboard.panel_groups"
Tristan Read's avatar
Tristan Read committed
456
        :key="`${groupData.group}.${groupData.priority}`"
457
458
        :name="groupData.group"
        :show-panels="showPanels"
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
459
        :collapse-group="groupHasData(groupData)"
460
      >
461
        <vue-draggable
462
          :value="groupData.panels"
463
464
465
466
467
468
          group="metrics-dashboard"
          :component-data="{ attrs: { class: 'row mx-0 w-100' } }"
          :disabled="!isRearrangingPanels"
          @input="updateMetrics(groupData.key, $event)"
        >
          <div
469
            v-for="(graphData, graphIndex) in groupData.panels"
470
471
472
            :key="`panel-type-${graphIndex}`"
            class="col-12 col-lg-6 px-2 mb-2 draggable"
            :class="{ 'draggable-enabled': isRearrangingPanels }"
473
          >
474
475
476
477
            <div class="position-relative draggable-panel js-draggable-panel">
              <div
                v-if="isRearrangingPanels"
                class="draggable-remove js-draggable-remove p-2 w-100 position-absolute d-flex justify-content-end"
478
                @click="removeGraph(groupData.panels, graphIndex)"
479
480
481
482
              >
                <a class="mx-2 p-2 draggable-remove-link" :aria-label="__('Remove')"
                  ><icon name="close"
                /></a>
483
              </div>
484
485
486
487

              <panel-type
                :clipboard-text="generateLink(groupData.group, graphData.title, graphData.y_label)"
                :graph-data="graphData"
488
                :alerts-endpoint="alertsEndpoint"
489
490
                :prometheus-alerts-available="prometheusAlertsAvailable"
                :index="`${index}-${graphIndex}`"
491
492
              />
            </div>
493
494
          </div>
        </vue-draggable>
495
496
497
498
499
500
501
502
503
504
505
506
      </graph-group>
    </div>
    <empty-state
      v-else
      :selected-state="emptyState"
      :documentation-path="documentationPath"
      :settings-path="settingsPath"
      :clusters-path="clustersPath"
      :empty-getting-started-svg-path="emptyGettingStartedSvgPath"
      :empty-loading-svg-path="emptyLoadingSvgPath"
      :empty-no-data-svg-path="emptyNoDataSvgPath"
      :empty-unable-to-connect-svg-path="emptyUnableToConnectSvgPath"
507
      :compact="smallEmptyState"
508
    />
509
510
  </div>
</template>