summaryrefslogtreecommitdiff
path: root/app/controllers/concerns/metrics/dashboard/prometheus_api_proxy.rb
blob: ea9fd2de9611f08ca8d94d80dae6967393f2a102 (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
# frozen_string_literal: true

module Metrics::Dashboard::PrometheusApiProxy
  extend ActiveSupport::Concern
  include RenderServiceResults

  included do
    before_action :authorize_read_prometheus!, only: [:prometheus_proxy]
  end

  def prometheus_proxy
    variable_substitution_result =
      proxy_variable_substitution_service.new(proxyable, permit_params).execute

    return error_response(variable_substitution_result) if variable_substitution_result[:status] == :error

    prometheus_result = ::Prometheus::ProxyService.new(
      proxyable,
      proxy_method,
      proxy_path,
      variable_substitution_result[:params]
    ).execute

    return continue_polling_response if prometheus_result.nil?
    return error_response(prometheus_result) if prometheus_result[:status] == :error

    success_response(prometheus_result)
  end

  private

  def proxyable
    raise NotImplementedError, "#{self.class} must implement method: #{__callee__}"
  end

  def proxy_variable_substitution_service
    raise NotImplementedError, "#{self.class} must implement method: #{__callee__}"
  end

  def permit_params
    params.permit!
  end

  def proxy_method
    request.method
  end

  def proxy_path
    params[:proxy_path]
  end
end