Class: ForestLiana::LineStatGetter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, params) ⇒ LineStatGetter

Returns a new instance of LineStatGetter.



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

def initialize(resource, params)
  @timezone_offset = params[:timezone].to_i
  super(resource, params)
end

Instance Attribute Details

#recordObject

Returns the value of attribute record.



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

def record
  @record
end

Instance Method Details

#client_timezoneObject



10
11
12
# File 'app/services/forest_liana/line_stat_getter.rb', line 10

def client_timezone
  ActiveSupport::TimeZone[@timezone_offset].name
end

#get_formatObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/forest_liana/line_stat_getter.rb', line 14

def get_format
  case @params[:time_range].try(:downcase)
    when 'day'
      '%d/%m/%Y'
    when 'week'
      'W%W-%Y'
    when 'month'
      '%b %Y'
    when 'year'
      '%Y'
  end
end

#performObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/forest_liana/line_stat_getter.rb', line 27

def perform
  value = @resource.unscoped.eager_load(includes)

  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

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

  value = value.send(time_range, group_by_date_field, { time_zone: client_timezone })
  value = value.send(@params[:aggregate].downcase, @params[:aggregate_field])
    .map do |k, v|
      { label: k.strftime(get_format), values: { value: v }}
    end

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