Class: ForestLiana::PieStatGetter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from StatGetter

#get_resource, #initialize, #validate_params

Methods inherited from BaseGetter

#get_collection, #get_resource, #includes_for_serialization

Constructor Details

This class inherits a constructor from ForestLiana::StatGetter

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

#groupByFieldNameObject



40
41
42
43
44
45
46
47
48
# File 'app/services/forest_liana/pie_stat_getter.rb', line 40

def groupByFieldName
  if @params[:groupByFieldName].include? ':'
    association, field = @params[:groupByFieldName].split ':'
    resource = @resource.reflect_on_association(association.to_sym)
    "#{resource.table_name}.#{field}"
  else
    "#{@resource.table_name}.#{@params[:groupByFieldName]}"
  end
end

#orderObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/forest_liana/pie_stat_getter.rb', line 50

def order
  order = 'DESC'

  # NOTICE: The generated alias for a count is "count_all", for a sum the
  #         alias looks like "sum_#{aggregateFieldName}"
  if @params[:aggregator].downcase == 'sum'
    field = @params[:aggregateFieldName].downcase
  else
    # `count_id` is required only for rails v5
    field = Rails::VERSION::MAJOR == 5 || @includes.size > 0 ? 'id' : 'all'
  end
  "#{@params[:aggregator].downcase}_#{field} #{order}"
end

#performObject



5
6
7
8
9
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
38
# File 'app/services/forest_liana/pie_stat_getter.rb', line 5

def perform
  if @params[:groupByFieldName]
    timezone_offset = @params[:timezone].to_i
    resource = optimize_record_loading(@resource, get_resource)

    filters = ForestLiana::ScopeManager.append_scope_for_user(@params[:filter], @user, @resource.name, @params['contextVariables'])

    unless filters.blank?
      resource = FiltersParser.new(filters, resource, @params[:timezone], @params).apply_filters
    end

    result = resource
      .group(groupByFieldName)
      .order(order)
      .send(@params[:aggregator].downcase, @params[:aggregateFieldName])
      .map do |key, value|
        # NOTICE: Display the enum name instead of an integer if it is an
        #         "Enum" field type on old Rails version (before Rails
        #         5.1.3).
        if @resource.respond_to?(:defined_enums) &&
          @resource.defined_enums.has_key?(@params[:groupByFieldName]) &&
          key.is_a?(Integer)
          key = @resource.defined_enums[@params[:groupByFieldName]].invert[key]
        elsif @resource.columns_hash[@params[:groupByFieldName]] &&
          @resource.columns_hash[@params[:groupByFieldName]].type == :datetime
          key = (key + timezone_offset.hours).strftime('%d/%m/%Y %T')
        end

        { key: key, value: value }
      end

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