Class: ForestLiana::StatsController

Inherits:
ApplicationController show all
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 inherited from ApplicationController

ApplicationController::REGEX_COOKIE_SESSION_TOKEN

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_user_from_jwt, #forest_user, #get_smart_action_context, #internal_server_error, papertrail?, #route_not_found, #serialize_model, #serialize_models

Instance Method Details

#getObject



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

def get
  case params[:type]
  when CHART_TYPE_VALUE
    stat = ValueStatGetter.new(@resource, params)
  when CHART_TYPE_PIE
    stat = PieStatGetter.new(@resource, params)
  when CHART_TYPE_LINE
    stat = LineStatGetter.new(@resource, params)
  when CHART_TYPE_OBJECTIVE
    stat = ObjectiveStatGetter.new(@resource, params)
  when CHART_TYPE_LEADERBOARD
    stat = LeaderboardStatGetter.new(@resource, params)
  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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/forest_liana/stats_controller.rb', line 37

def get_with_live_query
  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_LOGGER.error "Live Query error: #{error.message}"
    render json: { errors: [{ status: 422, detail: error.message }] },
      status: :unprocessable_entity, serializer: nil
  end
end