Skip to content
代码片段 群组 项目
提交 89443455 编辑于 作者: Rajendra Kadam's avatar Rajendra Kadam
浏览文件

Remove panel preview service and builder controller

Remove routes for builder panel preview

Remove/update specs to reflect the changes

Changelog: removed
MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/126972
上级 348b8d68
No related branches found
No related tags found
无相关合并请求
# frozen_string_literal: true
module Projects
module Metrics
module Dashboards
class BuilderController < Projects::ApplicationController
before_action :authorize_metrics_dashboard!
feature_category :metrics
urgency :low
def panel_preview
return not_found if Feature.enabled?(:remove_monitor_metrics)
respond_to do |format|
format.json do
if rendered_panel.success?
render json: rendered_panel.payload
else
render json: { message: rendered_panel.message }, status: :unprocessable_entity
end
end
end
end
private
def rendered_panel
@panel_preview ||= ::Metrics::Dashboard::PanelPreviewService.new(project, panel_yaml, environment).execute
end
def panel_yaml
params.require(:panel_yaml)
end
def environment
@environment ||=
if params[:environment]
project.environments.find(params[:environment])
else
project.default_environment
end
end
end
end
end
end
...@@ -82,8 +82,7 @@ def project_and_environment_metrics_data(project, environment) ...@@ -82,8 +82,7 @@ def project_and_environment_metrics_data(project, environment)
{ {
'deployments_endpoint' => project_environment_deployments_path(project, environment, format: :json), 'deployments_endpoint' => project_environment_deployments_path(project, environment, format: :json),
'operations_settings_path' => project_settings_operations_path(project), 'operations_settings_path' => project_settings_operations_path(project),
'can_access_operations_settings' => can?(current_user, :admin_operations, project).to_s, 'can_access_operations_settings' => can?(current_user, :admin_operations, project).to_s
'panel_preview_endpoint' => project_metrics_dashboards_builder_path(project, format: :json)
} }
end end
......
# frozen_string_literal: true
# Ingest YAML fragment with metrics dashboard panel definition
# https://docs.gitlab.com/ee/operations/metrics/dashboards/yaml.html#panel-panels-properties
# process it and returns renderable json version
module Metrics
module Dashboard
class PanelPreviewService
SEQUENCE = [
::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter,
::Gitlab::Metrics::Dashboard::Stages::MetricEndpointInserter,
::Gitlab::Metrics::Dashboard::Stages::PanelIdsInserter,
::Gitlab::Metrics::Dashboard::Stages::UrlValidator
].freeze
HANDLED_PROCESSING_ERRORS = [
Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError,
Gitlab::Config::Loader::Yaml::NotHashError,
Gitlab::Config::Loader::Yaml::DataTooLargeError,
Gitlab::Config::Loader::FormatError
].freeze
def initialize(project, panel_yaml, environment)
@project = project
@panel_yaml = panel_yaml
@environment = environment
end
def execute
dashboard = ::Gitlab::Metrics::Dashboard::Processor.new(project, dashboard_structure, SEQUENCE, environment: environment).process
ServiceResponse.success(payload: dashboard[:panel_groups][0][:panels][0])
rescue *HANDLED_PROCESSING_ERRORS => error
ServiceResponse.error(message: error.message)
end
private
attr_accessor :project, :panel_yaml, :environment
def dashboard_structure
{
panel_groups: [
{
panels: [panel_hash]
}
]
}
end
def panel_hash
::Gitlab::Config::Loader::Yaml.new(panel_yaml).load_raw!
end
end
end
end
...@@ -30,12 +30,6 @@ ...@@ -30,12 +30,6 @@
scope '-' do scope '-' do
get 'archive/*id', format: true, constraints: { format: Gitlab::PathRegex.archive_formats_regex, id: /.+?/ }, to: 'repositories#archive', as: 'archive' get 'archive/*id', format: true, constraints: { format: Gitlab::PathRegex.archive_formats_regex, id: /.+?/ }, to: 'repositories#archive', as: 'archive'
namespace :metrics, module: :metrics do
namespace :dashboards do
post :builder, to: 'builder#panel_preview'
end
end
namespace :security do namespace :security do
resource :configuration, only: [:show], controller: :configuration do resource :configuration, only: [:show], controller: :configuration do
resource :sast, only: [:show], controller: :sast_configuration resource :sast, only: [:show], controller: :sast_configuration
......
...@@ -41,8 +41,7 @@ ...@@ -41,8 +41,7 @@
'custom_metrics_available' => 'true', 'custom_metrics_available' => 'true',
'custom_dashboard_base_path' => Gitlab::Metrics::Dashboard::RepoDashboardFinder::DASHBOARD_ROOT, 'custom_dashboard_base_path' => Gitlab::Metrics::Dashboard::RepoDashboardFinder::DASHBOARD_ROOT,
'operations_settings_path' => project_settings_operations_path(project), 'operations_settings_path' => project_settings_operations_path(project),
'can_access_operations_settings' => 'true', 'can_access_operations_settings' => 'true'
'panel_preview_endpoint' => project_metrics_dashboards_builder_path(project, format: :json)
) )
end end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Projects::Metrics::Dashboards::BuilderController', feature_category: :metrics do
let_it_be(:project) { create(:project) }
let_it_be(:environment) { create(:environment, project: project) }
let_it_be(:user) { create(:user) }
let_it_be(:valid_panel_yml) do
<<~YML
---
title: "Super Chart A1"
type: "area-chart"
y_label: "y_label"
weight: 1
max_value: 1
metrics:
- id: metric_a1
query_range: |+
avg(
sum(
container_memory_usage_bytes{
container_name!="POD",
pod_name=~"^{{ci_environment_slug}}-(.*)",
namespace="{{kube_namespace}}",
user_def_variable="{{user_def_variable}}"
}
) by (job)
) without (job)
/1024/1024/1024
unit: unit
label: Legend Label
YML
end
let_it_be(:invalid_panel_yml) do
<<~YML
---
title: "Super Chart A1"
type: "area-chart"
y_label: "y_label"
weight: 1
max_value: 1
YML
end
def send_request(params = {})
post namespace_project_metrics_dashboards_builder_path(namespace_id: project.namespace, project_id: project, format: :json, **params)
end
describe 'POST /:namespace/:project/-/metrics/dashboards/builder' do
before do
stub_feature_flags(remove_monitor_metrics: false)
end
context 'as anonymous user' do
it 'redirects user to sign in page' do
send_request
expect(response).to redirect_to(new_user_session_path)
end
end
context 'as user with guest access' do
before do
project.add_guest(user)
login_as(user)
end
it 'returns not found' do
send_request
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'as logged in user' do
before do
project.add_developer(user)
login_as(user)
end
context 'valid yaml panel is supplied' do
it 'returns success' do
send_request(panel_yaml: valid_panel_yml)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to include('title' => 'Super Chart A1', 'type' => 'area-chart')
end
end
context 'invalid yaml panel is supplied' do
it 'returns unprocessable entity' do
send_request(panel_yaml: invalid_panel_yml)
expect(response).to have_gitlab_http_status(:unprocessable_entity)
expect(json_response['message']).to eq('Each "panel" must define an array :metrics')
end
end
context 'invalid panel_yaml is not a yaml string' do
it 'returns unprocessable entity' do
send_request(panel_yaml: 1)
expect(response).to have_gitlab_http_status(:unprocessable_entity)
expect(json_response['message']).to eq('Invalid configuration format')
end
end
context 'when metrics dashboard feature is unavailable' do
before do
stub_feature_flags(remove_monitor_metrics: true)
end
it 'returns not found' do
send_request
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Metrics::Dashboard::PanelPreviewService, feature_category: :metrics do
let_it_be(:project) { create(:project) }
let_it_be(:environment) { create(:environment, project: project) }
let_it_be(:panel_yml) do
<<~YML
---
title: test panel
YML
end
let_it_be(:dashboard) do
{
panel_groups: [
{
panels: [{ 'title' => 'test panel' }]
}
]
}
end
describe '#execute' do
subject(:service_response) { described_class.new(project, panel_yml, environment).execute }
context "valid panel's yaml" do
before do
allow_next_instance_of(::Gitlab::Metrics::Dashboard::Processor) do |processor|
allow(processor).to receive(:process).and_return(dashboard)
end
end
it 'returns success service response' do
expect(service_response.success?).to be_truthy
end
it 'returns processed panel' do
expect(service_response.payload).to eq('title' => 'test panel')
end
it 'uses dashboard processor' do
sequence = [
::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter,
::Gitlab::Metrics::Dashboard::Stages::MetricEndpointInserter,
::Gitlab::Metrics::Dashboard::Stages::PanelIdsInserter,
::Gitlab::Metrics::Dashboard::Stages::UrlValidator
]
processor_params = [project, dashboard, sequence, environment: environment]
expect_next_instance_of(::Gitlab::Metrics::Dashboard::Processor, *processor_params) do |processor|
expect(processor).to receive(:process).and_return(dashboard)
end
service_response
end
end
context "invalid panel's yaml" do
[
Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError,
Gitlab::Config::Loader::Yaml::NotHashError,
Gitlab::Config::Loader::Yaml::DataTooLargeError,
Gitlab::Config::Loader::FormatError
].each do |error_class|
context "with #{error_class}" do
before do
allow_next_instance_of(::Gitlab::Metrics::Dashboard::Processor) do |processor|
allow(processor).to receive(:process).and_raise(error_class.new('error'))
end
end
it 'returns error service response' do
expect(service_response.error?).to be_truthy
end
it 'returns error message' do
expect(service_response.message).to eq('error')
end
end
end
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册