summaryrefslogtreecommitdiff
path: root/app/controllers/projects/performance_monitoring/dashboards_controller.rb
blob: 1255ec1dde2bc48a9bdeb7d98af73ad933e8d7f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true

module Projects
  module PerformanceMonitoring
    class DashboardsController < ::Projects::ApplicationController
      include BlobHelper

      before_action :check_repository_available!
      before_action :validate_required_params!

      rescue_from ActionController::ParameterMissing do |exception|
        respond_error(http_status: :bad_request, message: _('Request parameter %{param} is missing.') % { param: exception.param })
      end

      feature_category :metrics
      urgency :low

      def create
        return not_found if Feature.enabled?(:remove_monitor_metrics)

        result = ::Metrics::Dashboard::CloneDashboardService.new(project, current_user, dashboard_params).execute

        if result[:status] == :success
          respond_success(result)
        else
          respond_error(result)
        end
      end

      def update
        return not_found if Feature.enabled?(:remove_monitor_metrics)

        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

      private

      def respond_success(result)
        set_web_ide_link_notice(result.dig(:dashboard, :path))
        respond_to do |format|
          format.json { render status: result.delete(:http_status), json: result }
        end
      end

      def respond_error(result)
        respond_to do |format|
          format.json { render json: { error: result[:message] }, status: result[:http_status] }
        end
      end

      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
      end

      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

      def validate_required_params!
        params.require(%i[branch file_name dashboard commit_message])
      end

      def redirect_safe_branch_name
        repository.find_branch(params[:branch]).name
      end

      def dashboard_params
        params.permit(%i[branch file_name dashboard commit_message]).to_h
      end

      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
    end
  end
end