Class: Skadi::DashboardController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/skadi/dashboard_controller.rb

Instance Method Summary collapse

Instance Method Details

#dataObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/skadi/dashboard_controller.rb', line 21

def data
  return head :forbidden unless can_view

  query_filters = params.permit(:date_from, :date_to, :page)

  if can_edit && params[:configuration].present?
    return custom_chart_data(query_filters)
  end

  unless params[:chart_id].present?
    return render json: { error: "The chart_id parameter must be specified" }, status: :unprocessable_content
  end

  return render json: @dashboard.chart_data(params[:chart_id], query_filters.to_h)
rescue ActiveRecord::StatementInvalid => e
  message = can_dangerously_use_sql ? e.message : "Something went wrong fetching the data. Please contact a site admin."
  render json: { error: message }, status: :unprocessable_content
rescue Skadi::Dashboard::Error => e
  render json: { error: e.message }, status: :unprocessable_content
end

#showObject



10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/skadi/dashboard_controller.rb', line 10

def show
  return head :forbidden unless can_view

  render :show, locals: {
    dashboard_configuration: @dashboard.configuration,
    dataset_schema: Skadi::Schema.frontend_schema,
    can_edit: can_edit,
    can_dangerously_use_sql: can_dangerously_use_sql,
  }
end

#updateObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/skadi/dashboard_controller.rb', line 42

def update
  return head :forbidden unless can_view && can_edit

  # The dashboard validator will strongly check the structure of :configuration
  @dashboard.configuration = params.to_unsafe_h[:configuration]

  if @dashboard.save
    head :ok
  else
    render json: { error: "Dashboard validation failed. #{@dashboard.errors.full_messages.join("\n")}" }, status: :unprocessable_content
  end
end