summaryrefslogtreecommitdiff
path: root/app/controllers/projects/metrics_dashboard_controller.rb
blob: c95594d87c0161b590bccb774fb396de6f5bd922 (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
# frozen_string_literal: true
module Projects
  class MetricsDashboardController < Projects::ApplicationController
    # Metrics dashboard code is in the process of being decoupled from environments
    # and is getting moved to this controller. Some code may be duplicated from
    # app/controllers/projects/environments_controller.rb
    # See https://gitlab.com/gitlab-org/gitlab/-/issues/226002 for more details.

    include Gitlab::Utils::StrongMemoize

    before_action :authorize_metrics_dashboard!
    before_action :render_404, only: :show, if: -> do
      Feature.enabled?(:remove_monitor_metrics)
    end

    feature_category :metrics
    urgency :low

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

      if environment
        render 'projects/environments/metrics'
      elsif default_environment
        redirect_to project_metrics_dashboard_path(
          project,
          # Reverse merge the query parameters so that a query parameter named dashboard_path doesn't
          # override the dashboard_path path parameter.
          **permitted_params.to_h.symbolize_keys
            .merge(environment: default_environment.id)
            .reverse_merge(request.query_parameters.symbolize_keys)
        )
      else
        render 'projects/environments/empty_metrics'
      end
    end

    private

    def permitted_params
      @permitted_params ||= params.permit(:dashboard_path, :environment, :page)
    end

    def environment
      strong_memoize(:environment) do
        env = permitted_params[:environment]
        project.environments.find(env) if env
      end
    end

    def default_environment
      strong_memoize(:default_environment) do
        project.default_environment
      end
    end
  end
end