debian-mirror-gitlab/app/controllers/concerns/metrics_dashboard.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
3.4 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
# Provides an action which fetches a metrics dashboard according
# to the parameters specified by the controller.
module MetricsDashboard
2019-12-26 22:10:19 +05:30
include RenderServiceResults
include ChecksCollaboration
2020-03-13 15:44:24 +05:30
include EnvironmentsHelper
2019-12-26 22:10:19 +05:30
2019-12-21 20:55:43 +05:30
extend ActiveSupport::Concern
def metrics_dashboard
result = dashboard_finder.find(
project_for_dashboard,
current_user,
2020-07-28 23:09:34 +05:30
decoded_params
2019-12-21 20:55:43 +05:30
)
2020-03-13 15:44:24 +05:30
if result
result[:all_dashboards] = all_dashboards if include_all_dashboards?
2020-05-24 23:13:21 +05:30
result[:metrics_data] = metrics_data(project_for_dashboard, environment_for_dashboard)
2019-12-21 20:55:43 +05:30
end
respond_to do |format|
2019-12-26 22:10:19 +05:30
if result.nil?
format.json { continue_polling_response }
elsif result[:status] == :success
2019-12-21 20:55:43 +05:30
format.json { render dashboard_success_response(result) }
else
format.json { render dashboard_error_response(result) }
end
end
end
private
2019-12-26 22:10:19 +05:30
def all_dashboards
2020-05-24 23:13:21 +05:30
dashboard_finder
.find_all_paths(project_for_dashboard)
2023-04-23 21:23:45 +05:30
.map { |dashboard| amend_dashboard(dashboard) }
2019-12-26 22:10:19 +05:30
end
def amend_dashboard(dashboard)
2020-07-28 23:09:34 +05:30
project_dashboard = project_for_dashboard && !dashboard[:out_of_the_box_dashboard]
2019-12-26 22:10:19 +05:30
dashboard[:can_edit] = project_dashboard ? can_edit?(dashboard) : false
dashboard[:project_blob_path] = project_dashboard ? dashboard_project_blob_path(dashboard) : nil
2020-05-24 23:13:21 +05:30
dashboard[:starred] = starred_dashboards.include?(dashboard[:path])
dashboard[:user_starred_path] = project_for_dashboard ? user_starred_path(project_for_dashboard, dashboard[:path]) : nil
2019-12-26 22:10:19 +05:30
dashboard
end
2020-05-24 23:13:21 +05:30
def user_starred_path(project, path)
expose_path(api_v4_projects_metrics_user_starred_dashboards_path(id: project.id, params: { dashboard_path: path }))
end
2019-12-26 22:10:19 +05:30
def dashboard_project_blob_path(dashboard)
project_blob_path(project_for_dashboard, File.join(project_for_dashboard.default_branch, dashboard.fetch(:path, "")))
end
def can_edit?(dashboard)
can_collaborate_with_project?(project_for_dashboard, ref: project_for_dashboard.default_branch)
end
2019-12-21 20:55:43 +05:30
# Override in class to provide arguments to the finder.
def metrics_dashboard_params
{}
end
# Override in class if response requires complete list of
# dashboards in addition to requested dashboard body.
def include_all_dashboards?
false
end
def dashboard_finder
::Gitlab::Metrics::Dashboard::Finder
end
2020-05-24 23:13:21 +05:30
def starred_dashboards
2023-07-09 08:55:56 +05:30
@starred_dashboards ||=
2020-05-24 23:13:21 +05:30
if project_for_dashboard.present?
::Metrics::UsersStarredDashboardsFinder
.new(user: current_user, project: project_for_dashboard)
.execute
.map(&:dashboard_path)
.to_set
else
Set.new
end
end
2019-12-21 20:55:43 +05:30
# Project is not defined for group and admin level clusters.
def project_for_dashboard
defined?(project) ? project : nil
end
2020-03-13 15:44:24 +05:30
def environment_for_dashboard
defined?(environment) ? environment : nil
end
2019-12-21 20:55:43 +05:30
def dashboard_success_response(result)
{
status: :ok,
2020-03-13 15:44:24 +05:30
json: result.slice(:all_dashboards, :dashboard, :status, :metrics_data)
2019-12-21 20:55:43 +05:30
}
end
def dashboard_error_response(result)
{
2019-12-26 22:10:19 +05:30
status: result[:http_status] || :bad_request,
2019-12-21 20:55:43 +05:30
json: result.slice(:all_dashboards, :message, :status)
}
end
2020-07-28 23:09:34 +05:30
def decoded_params
params = metrics_dashboard_params
2023-03-04 22:38:38 +05:30
params[:dashboard_path] = CGI.unescape(params[:dashboard_path]) if params[:dashboard_path]
2020-07-28 23:09:34 +05:30
params
end
2019-12-21 20:55:43 +05:30
end