Class: ForestLiana::PieStatGetter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, params) ⇒ PieStatGetter

Returns a new instance of PieStatGetter.



5
6
7
8
# File 'app/services/forest_liana/pie_stat_getter.rb', line 5

def initialize(resource, params)
  @resource = resource
  @params = params
end

Instance Attribute Details

#recordObject

Returns the value of attribute record.



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

def record
  @record
end

Instance Method Details

#performObject



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
# File 'app/services/forest_liana/pie_stat_getter.rb', line 10

def perform
  if @params[:group_by_field]
    value = @resource

    @params[:filters].try(:each) do |filter|
      operator, filter_value = OperatorValueParser.parse(filter[:value])
      value = OperatorValueParser.add_where(value, filter[:field], operator,
                                            filter_value, @resource)
    end

    # NOTICE: The generated alias for a count is "count_all", for a sum the
    #         alias looks like "sum_#{aggregate_field}"
    field = 'all'
    if @params[:aggregate].downcase == 'sum'
      field = @params[:aggregate_field].downcase
    end

    value = value
      .group(@params[:group_by_field])
      .order("#{@params[:aggregate].downcase}_#{field} DESC")
      .send(@params[:aggregate].downcase, @params[:aggregate_field])
      .map do |k, v|
        { key: k, value: v }
      end

    @record = Model::Stat.new(value: value)
  end
end