diff options
Diffstat (limited to 'app/controllers/concerns/metrics_dashboard.rb')
-rw-r--r-- | app/controllers/concerns/metrics_dashboard.rb | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/app/controllers/concerns/metrics_dashboard.rb b/app/controllers/concerns/metrics_dashboard.rb new file mode 100644 index 00000000000..62efdacb710 --- /dev/null +++ b/app/controllers/concerns/metrics_dashboard.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +# Provides an action which fetches a metrics dashboard according +# to the parameters specified by the controller. +module MetricsDashboard + extend ActiveSupport::Concern + + def metrics_dashboard + result = dashboard_finder.find( + project_for_dashboard, + current_user, + metrics_dashboard_params + ) + + if include_all_dashboards? + result[:all_dashboards] = dashboard_finder.find_all_paths(project_for_dashboard) + end + + respond_to do |format| + if result[:status] == :success + format.json { render dashboard_success_response(result) } + else + format.json { render dashboard_error_response(result) } + end + end + end + + private + + # 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 + + # Project is not defined for group and admin level clusters. + def project_for_dashboard + defined?(project) ? project : nil + end + + def dashboard_success_response(result) + { + status: :ok, + json: result.slice(:all_dashboards, :dashboard, :status) + } + end + + def dashboard_error_response(result) + { + status: result[:http_status], + json: result.slice(:all_dashboards, :message, :status) + } + end +end |