Class: ForestLiana::ValueStatGetter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, params) ⇒ ValueStatGetter



5
6
7
8
# File 'app/services/forest_liana/value_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/value_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/forest_liana/value_stat_getter.rb', line 10

def perform
  return if @params[:aggregate].blank?
  valueCurrent = @resource.unscoped
  valuePrevious = @resource.unscoped
  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)
    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)
        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)
        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