Class: ForestLiana::ValueStatGetter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from StatGetter

#initialize

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/value_stat_getter.rb', line 3

def record
  @record
end

Instance Method Details

#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
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/forest_liana/value_stat_getter.rb', line 5

def perform
  return if @params[:aggregate].blank?
  valueCurrent = @resource.unscoped.eager_load(includes)
  valuePrevious = @resource.unscoped.eager_load(includes)
  filter_date_interval = false

  if @params[:filterType] && @params[:filters]
    conditions = []
    filter_operator = " #{@params[:filterType]} ".upcase

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

    valueCurrent = valueCurrent.where(conditions.join(filter_operator))

    # NOTICE: Search for previous interval value only if the filterType is
    #         'AND', it would not be pertinent for a 'OR' filterType.
    if @params[:filterType] == 'and'
      conditions = []
      @params[:filters].try(:each) do |filter|
        operator, filter_value = OperatorValueParser.parse(filter[:value])
        operator_date_interval_parser = OperatorDateIntervalParser
          .new(filter_value, @params[:timezone])
        if operator_date_interval_parser.has_previous_interval()
          field_name = OperatorValueParser.get_field_name(filter[:field], @resource)
          filter = operator_date_interval_parser
            .get_interval_date_filter_for_previous_interval()
          conditions << "#{field_name} #{filter}"
          filter_date_interval = true
        else
          conditions << OperatorValueParser.get_condition(filter[:field],
            operator, filter_value, @resource, @params[:timezone])
        end
      end

      valuePrevious = valuePrevious.where(conditions.join(filter_operator))
    end
  end

  @record = Model::Stat.new(value: {
    countCurrent: count(valueCurrent),
    countPrevious: filter_date_interval ? count(valuePrevious) : nil
  })
end