summaryrefslogtreecommitdiff
path: root/app/controllers/projects/grafana_api_controller.rb
blob: 9cd511f6a11241a5da4fa82b02afcfb0a959a3a1 (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
# frozen_string_literal: true

class Projects::GrafanaApiController < Projects::ApplicationController
  include RenderServiceResults
  include MetricsDashboard

  before_action :authorize_read_grafana!, only: :proxy

  feature_category :metrics
  urgency :low

  def proxy
    result = ::Grafana::ProxyService.new(
      project,
      params[:datasource_id],
      params[:proxy_path],
      prometheus_params
    ).execute

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

    success_response(result)
  end

  private

  def metrics_dashboard_params
    params.permit(:embedded, :grafana_url)
  end

  def query_params
    params.permit(:query, :start_time, :end_time, :step)
  end

  def prometheus_params
    query_params.to_h
      .except(:start_time, :end_time)
      .merge(
        start: query_params[:start_time],
        end: query_params[:end_time]
      )
  end
end