Class: ForestLiana::StatsController

Inherits:
ApplicationController show all
Includes:
Ability
Defined in:
app/controllers/forest_liana/stats_controller.rb

Constant Summary collapse

CHART_TYPE_VALUE =
'Value'
CHART_TYPE_PIE =
'Pie'
CHART_TYPE_LINE =
'Line'
CHART_TYPE_LEADERBOARD =
'Leaderboard'
CHART_TYPE_OBJECTIVE =
'Objective'

Constants included from Ability

Ability::ALLOWED_PERMISSION_LEVELS

Constants included from Ability::Permission

Ability::Permission::TTL

Instance Method Summary collapse

Methods included from Ability

#forest_authorize!

Methods included from Ability::Permission

#is_chart_authorized?, #is_crud_authorized?, #is_smart_action_authorized?

Methods included from Ability::Fetch

#get_permissions

Methods inherited from ApplicationController

#authenticate_user_from_jwt, #deactivate_count_response, #forest_user, #internal_server_error, papertrail?, #serialize_model, #serialize_models

Methods inherited from BaseController

#route_not_found

Instance Method Details

#getObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/forest_liana/stats_controller.rb', line 17

def get
  forest_authorize!('chart', forest_user, nil, {parameters: params})
  case params[:type]
  when CHART_TYPE_VALUE
    stat = ValueStatGetter.new(@resource, params, forest_user)
  when CHART_TYPE_PIE
    stat = PieStatGetter.new(@resource, params, forest_user)
  when CHART_TYPE_LINE
    stat = LineStatGetter.new(@resource, params, forest_user)
  when CHART_TYPE_OBJECTIVE
    stat = ObjectiveStatGetter.new(@resource, params, forest_user)
  when CHART_TYPE_LEADERBOARD
    stat = LeaderboardStatGetter.new(@resource, params, forest_user)
  end

  stat.perform
  if stat.record
    render json: serialize_model(stat.record), serializer: nil
  else
    render json: {status: 404}, status: :not_found, serializer: nil
  end
end

#get_with_live_queryObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/forest_liana/stats_controller.rb', line 40

def get_with_live_query
  forest_authorize!('chart', forest_user, nil, {parameters: params})
  begin
    stat = QueryStatGetter.new(params)
    stat.perform

    if stat.record
      render json: serialize_model(stat.record), serializer: nil
    else
      render json: {status: 404}, status: :not_found, serializer: nil
    end
  rescue ForestLiana::Errors::LiveQueryError => error
    render json: { errors: [{ status: 422, detail: error.message }] },
      status: :unprocessable_entity, serializer: nil
  rescue => error
    FOREST_REPORTER.report error
    FOREST_LOGGER.error "Live Query error: #{error.message}"
    render json: { errors: [{ status: 422, detail: error.message }] },
      status: :unprocessable_entity, serializer: nil
  end
end

#paramsObject



62
63
64
# File 'app/controllers/forest_liana/stats_controller.rb', line 62

def params
  super.permit!
end