Class: ForestLiana::QueryStatGetter

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/query_stat_getter.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ QueryStatGetter

Returns a new instance of QueryStatGetter.



11
12
13
# File 'app/services/forest_liana/query_stat_getter.rb', line 11

def initialize(params)
  @params = params
end

Instance Attribute Details

#recordObject

Returns the value of attribute record.



3
4
5
# File 'app/services/forest_liana/query_stat_getter.rb', line 3

def record
  @record
end

Instance Method Details

#performObject



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/services/forest_liana/query_stat_getter.rb', line 15

def perform
  context_variables = Utils::ContextVariables.new(nil, nil, @params['contextVariables'])
  raw_query = Utils::ContextVariablesInjector.inject_context_in_value(@params['query'].strip, context_variables)

  LiveQueryChecker.new(raw_query, 'Live Query Chart').validate()

  if @params['record_id']
    raw_query.gsub!('?', @params['record_id'].to_s)
  end
  result = ActiveRecord::Base.connection.execute(raw_query)

  case @params['type']
  when CHART_TYPE_VALUE
    if result.count
      result_line = ForestLiana::AdapterHelper.format_live_query_value_result(result)
      if result_line
        if !result_line.key?('value')
          raise error_message(result_line, "'value'")
        else
          @record = Model::Stat.new(value: {
            countCurrent: result_line['value'],
            countPrevious: result_line['previous']
          })
        end
      else
        @record = Model::Stat.new(value: { countCurrent: 0, countPrevious: 0 })
      end
    end
  when CHART_TYPE_PIE, CHART_TYPE_LEADERBOARD
    if result.count
      values = ForestLiana::AdapterHelper.format_live_query_pie_result(result)

      values.each do |result_line|
        if !result_line.key?('value') || !result_line.key?('key')
          raise error_message(result_line, "'key', 'value'")
        end
      end

      @record = Model::Stat.new(value: values)
    end
  when CHART_TYPE_LINE
    if result.count
      values = ForestLiana::AdapterHelper.format_live_query_line_result(result)

      values.each do |result_line|
        if !result_line.key?('value') || !result_line.key?('key')
          raise error_message(result_line, "'key', 'value'")
        end
      end

      result_formatted = values.map do |result_line|
        { label: result_line['key'], values: { value: result_line['value'] }}
      end

      @record = Model::Stat.new(value: result_formatted)
    end
  when CHART_TYPE_OBJECTIVE
    if result.count
      result_line = ForestLiana::AdapterHelper.format_live_query_value_result(result)
      if result_line
        if !result_line.key?('value') || !result_line.key?('objective')
          raise error_message(result_line, "'value', 'objective'")
        else
          @record = Model::Stat.new(value: {
            value: result_line['value'],
            objective: result_line['objective']
          })
        end
      else
        @record = Model::Stat.new(value: { value: 0, objective: 0 })
      end
    end
  end
end