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

Methods inherited from StatGetter

#initialize

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

def record
  @record
end

Instance Method Details

#client_timezoneObject


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

def client_timezone
  # As stated here https://github.com/ankane/groupdate#for-sqlite
  # groupdate does not handle timezone for SQLite
  return false if 'SQLite' == ActiveRecord::Base.connection.adapter_name
  @params[:timezone]
end

#get_formatObject


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

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

#performObject


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/forest_liana/line_stat_getter.rb', line 25

def perform
  value = get_resource().joins(@includes)

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

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

  Groupdate.week_start = :monday

  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