Commit c85f0acc authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'sy-transient-embeds' into 'master'

Add support for metrics embeds not backed by the DB

See merge request gitlab-org/gitlab!28618
parents 1fc514cc 645461f6
...@@ -222,7 +222,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -222,7 +222,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController
def metrics_dashboard_params def metrics_dashboard_params
params params
.permit(:embedded, :group, :title, :y_label, :dashboard_path, :environment, :sample_metrics) .permit(:embedded, :group, :title, :y_label, :dashboard_path, :environment, :sample_metrics, :embed_json)
.merge(dashboard_path: params[:dashboard], environment: environment) .merge(dashboard_path: params[:dashboard], environment: environment)
end end
......
# frozen_string_literal: true
# Acts as a pass-through to allow embeddable dashboards to be
# generated based on external data, but still processed with the
# required attributes that allow the FE to render them appropriately.
#
# Use Gitlab::Metrics::Dashboard::Finder to retrive dashboards.
module Metrics
module Dashboard
class TransientEmbedService < ::Metrics::Dashboard::BaseEmbedService
extend ::Gitlab::Utils::Override
class << self
def valid_params?(params)
[
embedded?(params[:embedded]),
params[:embed_json]
].all?
end
end
private
override :get_raw_dashboard
def get_raw_dashboard
JSON.parse(params[:embed_json])
end
override :sequence
def sequence
[STAGES::EndpointInserter]
end
end
end
end
---
title: Add support for database-independent embedded metric charts
merge_request: 28618
author:
type: added
...@@ -16,6 +16,7 @@ module Gitlab ...@@ -16,6 +16,7 @@ module Gitlab
::Metrics::Dashboard::GitlabAlertEmbedService, ::Metrics::Dashboard::GitlabAlertEmbedService,
::Metrics::Dashboard::CustomMetricEmbedService, ::Metrics::Dashboard::CustomMetricEmbedService,
::Metrics::Dashboard::GrafanaMetricEmbedService, ::Metrics::Dashboard::GrafanaMetricEmbedService,
::Metrics::Dashboard::TransientEmbedService,
::Metrics::Dashboard::DynamicEmbedService, ::Metrics::Dashboard::DynamicEmbedService,
::Metrics::Dashboard::DefaultEmbedService, ::Metrics::Dashboard::DefaultEmbedService,
::Metrics::Dashboard::SystemDashboardService, ::Metrics::Dashboard::SystemDashboardService,
......
...@@ -136,6 +136,36 @@ describe 'Metrics rendering', :js, :use_clean_rails_memory_store_caching, :sidek ...@@ -136,6 +136,36 @@ describe 'Metrics rendering', :js, :use_clean_rails_memory_store_caching, :sidek
end end
end end
context 'transient metrics embeds' do
let(:metrics_url) { urls.metrics_project_environment_url(project, environment, embed_json: embed_json) }
let(:title) { 'Important Metrics' }
let(:embed_json) do
{
panel_groups: [{
panels: [{
type: "line-graph",
title: title,
y_label: "metric",
metrics: [{
query_range: "metric * 0.5 < 1"
}]
}]
}]
}.to_json
end
before do
stub_any_prometheus_request_with_response
end
it 'shows embedded metrics' do
visit project_issue_path(project, issue)
expect(page).to have_css('div.prometheus-graph')
expect(page).to have_text(title)
end
end
def import_common_metrics def import_common_metrics
::Gitlab::DatabaseImporters::CommonMetrics::Importer.new.execute ::Gitlab::DatabaseImporters::CommonMetrics::Importer.new.execute
end end
......
...@@ -98,6 +98,17 @@ describe Gitlab::Metrics::Dashboard::ServiceSelector do ...@@ -98,6 +98,17 @@ describe Gitlab::Metrics::Dashboard::ServiceSelector do
it { is_expected.to be Metrics::Dashboard::GrafanaMetricEmbedService } it { is_expected.to be Metrics::Dashboard::GrafanaMetricEmbedService }
end end
context 'with the embed defined in the arguments' do
let(:arguments) do
{
embedded: true,
embed_json: '{}'
}
end
it { is_expected.to be Metrics::Dashboard::TransientEmbedService }
end
end end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
describe Metrics::Dashboard::TransientEmbedService, :use_clean_rails_memory_store_caching do
let_it_be(:project) { build(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:environment) { create(:environment, project: project) }
before do
project.add_maintainer(user)
end
describe '.valid_params?' do
let(:params) { { embedded: 'true', embed_json: '{}' } }
subject { described_class.valid_params?(params) }
it { is_expected.to be_truthy }
context 'missing embedded' do
let(:params) { { embed_json: '{}' } }
it { is_expected.to be_falsey }
end
context 'not embedded' do
let(:params) { { embedded: 'false', embed_json: '{}' } }
it { is_expected.to be_falsey }
end
context 'missing embed_json' do
let(:params) { { embedded: 'true' } }
it { is_expected.to be_falsey }
end
end
describe '#get_dashboard' do
let(:embed_json) do
{
panel_groups: [{
panels: [{
type: 'line-graph',
title: 'title',
y_label: 'y_label',
metrics: [{
query_range: 'up',
label: 'y_label'
}]
}]
}]
}.to_json
end
let(:service_params) { [project, user, { environment: environment, embedded: 'true', embed_json: embed_json }] }
let(:service_call) { described_class.new(*service_params).get_dashboard }
it_behaves_like 'valid embedded dashboard service response'
it_behaves_like 'raises error for users with insufficient permissions'
it 'caches the unprocessed dashboard for subsequent calls' do
expect_any_instance_of(described_class)
.to receive(:get_raw_dashboard)
.once
.and_call_original
described_class.new(*service_params).get_dashboard
described_class.new(*service_params).get_dashboard
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment