2020-01-09 15:07:42 +00:00
# frozen_string_literal: true
module Projects
module PerformanceMonitoring
class DashboardsController < :: Projects :: ApplicationController
include BlobHelper
before_action :check_repository_available!
before_action :validate_required_params!
2020-01-16 18:08:46 +00:00
rescue_from ActionController :: ParameterMissing do | exception |
respond_error ( http_status : :bad_request , message : _ ( 'Request parameter %{param} is missing.' ) % { param : exception . param } )
end
2020-01-09 15:07:42 +00:00
2020-10-08 18:08:32 +00:00
feature_category :metrics
2020-01-09 15:07:42 +00:00
def create
2020-01-16 18:08:46 +00:00
result = :: Metrics :: Dashboard :: CloneDashboardService . new ( project , current_user , dashboard_params ) . execute
2020-01-09 15:07:42 +00:00
if result [ :status ] == :success
2020-01-16 18:08:46 +00:00
respond_success ( result )
2020-01-09 15:07:42 +00:00
else
2020-01-16 18:08:46 +00:00
respond_error ( result )
2020-01-09 15:07:42 +00:00
end
end
2020-02-26 15:08:56 +00:00
def update
result = :: Metrics :: Dashboard :: UpdateDashboardService . new ( project , current_user , dashboard_params . merge ( file_content_params ) ) . execute
if result [ :status ] == :success
respond_update_success ( result )
else
respond_error ( result )
end
end
2020-01-09 15:07:42 +00:00
private
2020-01-16 18:08:46 +00:00
def respond_success ( result )
set_web_ide_link_notice ( result . dig ( :dashboard , :path ) )
2020-01-09 15:07:42 +00:00
respond_to do | format |
2020-01-16 18:08:46 +00:00
format . json { render status : result . delete ( :http_status ) , json : result }
2020-01-09 15:07:42 +00:00
end
end
2020-01-16 18:08:46 +00:00
def respond_error ( result )
2020-01-09 15:07:42 +00:00
respond_to do | format |
2020-01-16 18:08:46 +00:00
format . json { render json : { error : result [ :message ] } , status : result [ :http_status ] }
2020-01-09 15:07:42 +00:00
end
end
2020-01-16 18:08:46 +00:00
def set_web_ide_link_notice ( new_dashboard_path )
web_ide_link_start = " <a href= \" #{ ide_edit_path ( project , redirect_safe_branch_name , new_dashboard_path ) } \" > "
message = _ ( " Your dashboard has been copied. You can %{web_ide_link_start}edit it here%{web_ide_link_end}. " ) % { web_ide_link_start : web_ide_link_start , web_ide_link_end : " </a> " }
flash [ :notice ] = message . html_safe
2020-01-09 15:07:42 +00:00
end
2020-02-26 15:08:56 +00:00
def respond_update_success ( result )
set_web_ide_link_update_notice ( result . dig ( :dashboard , :path ) )
respond_to do | format |
format . json { render status : result . delete ( :http_status ) , json : result }
end
end
def set_web_ide_link_update_notice ( new_dashboard_path )
web_ide_link_start = " <a href= \" #{ ide_edit_path ( project , redirect_safe_branch_name , new_dashboard_path ) } \" > "
message = _ ( " Your dashboard has been updated. You can %{web_ide_link_start}edit it here%{web_ide_link_end}. " ) % { web_ide_link_start : web_ide_link_start , web_ide_link_end : " </a> " }
flash [ :notice ] = message . html_safe
end
2020-01-09 15:07:42 +00:00
def validate_required_params!
2020-01-16 18:08:46 +00:00
params . require ( % i ( branch file_name dashboard commit_message ) )
2020-01-09 15:07:42 +00:00
end
def redirect_safe_branch_name
repository . find_branch ( params [ :branch ] ) . name
end
2020-01-16 18:08:46 +00:00
def dashboard_params
params . permit ( % i ( branch file_name dashboard commit_message ) ) . to_h
end
2020-02-26 15:08:56 +00:00
def file_content_params
params . permit (
file_content : [
:dashboard ,
panel_groups : [
:group ,
:priority ,
panels : [
:type ,
:title ,
:y_label ,
:weight ,
metrics : [
:id ,
:unit ,
:label ,
:query ,
:query_range
]
]
]
]
)
end
2020-01-09 15:07:42 +00:00
end
end
end