From 97b325a4a28206d921523e2ce34af0ac4be31bd0 Mon Sep 17 00:00:00 2001 From: Tristan Read Date: Mon, 22 Jul 2019 12:01:42 +0000 Subject: [PATCH] Add ability to embed metrics See https://gitlab.com/gitlab-org/gitlab-ce/issues/30423 --- .../behaviors/markdown/render_gfm.js | 4 + .../behaviors/markdown/render_metrics.js | 24 +++++ .../monitoring/components/charts/area.vue | 96 +++++++++--------- .../monitoring/components/dashboard.vue | 7 +- .../monitoring/components/embed.vue | 97 +++++++++++++++++++ .../javascripts/monitoring/constants.js | 2 + .../javascripts/monitoring/stores/actions.js | 12 ++- .../monitoring/stores/mutation_types.js | 1 + .../monitoring/stores/mutations.js | 3 + .../javascripts/monitoring/stores/state.js | 1 + app/assets/stylesheets/pages/prometheus.scss | 5 + .../behaviors/markdown/render_metrics_spec.js | 37 +++++++ spec/frontend/monitoring/embed/embed_spec.js | 78 +++++++++++++++ spec/frontend/monitoring/embed/mock_data.js | 87 +++++++++++++++++ spec/frontend/test_setup.js | 6 ++ 15 files changed, 410 insertions(+), 50 deletions(-) create mode 100644 app/assets/javascripts/behaviors/markdown/render_metrics.js create mode 100644 app/assets/javascripts/monitoring/components/embed.vue create mode 100644 spec/frontend/behaviors/markdown/render_metrics_spec.js create mode 100644 spec/frontend/monitoring/embed/embed_spec.js create mode 100644 spec/frontend/monitoring/embed/mock_data.js diff --git a/app/assets/javascripts/behaviors/markdown/render_gfm.js b/app/assets/javascripts/behaviors/markdown/render_gfm.js index bfb073fdcdc..789a057caf8 100644 --- a/app/assets/javascripts/behaviors/markdown/render_gfm.js +++ b/app/assets/javascripts/behaviors/markdown/render_gfm.js @@ -2,6 +2,7 @@ import $ from 'jquery'; import syntaxHighlight from '~/syntax_highlight'; import renderMath from './render_math'; import renderMermaid from './render_mermaid'; +import renderMetrics from './render_metrics'; import highlightCurrentUser from './highlight_current_user'; import initUserPopovers from '../../user_popovers'; import initMRPopovers from '../../mr_popover'; @@ -17,6 +18,9 @@ $.fn.renderGFM = function renderGFM() { highlightCurrentUser(this.find('.gfm-project_member').get()); initUserPopovers(this.find('.gfm-project_member').get()); initMRPopovers(this.find('.gfm-merge_request').get()); + if (gon.features && gon.features.gfmEmbeddedMetrics) { + renderMetrics(this.find('.js-render-metrics').get()); + } return this; }; diff --git a/app/assets/javascripts/behaviors/markdown/render_metrics.js b/app/assets/javascripts/behaviors/markdown/render_metrics.js new file mode 100644 index 00000000000..252b98610b6 --- /dev/null +++ b/app/assets/javascripts/behaviors/markdown/render_metrics.js @@ -0,0 +1,24 @@ +import Vue from 'vue'; +import Metrics from '~/monitoring/components/embed.vue'; +import { createStore } from '~/monitoring/stores'; + +// TODO: Handle copy-pasting - https://gitlab.com/gitlab-org/gitlab-ce/issues/64369. +export default function renderMetrics(elements) { + if (!elements.length) { + return; + } + + elements.forEach(element => { + const { dashboardUrl } = element.dataset; + const MetricsComponent = Vue.extend(Metrics); + + // eslint-disable-next-line no-new + new MetricsComponent({ + el: element, + store: createStore(), + propsData: { + dashboardUrl, + }, + }); + }); +} diff --git a/app/assets/javascripts/monitoring/components/charts/area.vue b/app/assets/javascripts/monitoring/components/charts/area.vue index 454ff4f284e..edf9423c74c 100644 --- a/app/assets/javascripts/monitoring/components/charts/area.vue +++ b/app/assets/javascripts/monitoring/components/charts/area.vue @@ -37,7 +37,13 @@ export default { }, projectPath: { type: String, - required: true, + required: false, + default: () => '', + }, + showBorder: { + type: Boolean, + required: false, + default: () => false, }, thresholds: { type: Array, @@ -234,52 +240,54 @@ export default {