Sha256: 1d3b3bed616342a068ef6a51ac28973b5b6d1fead7d4f0b0ea725ce6a5710d96

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

module ForestLiana
  class LineStatGetter < StatGetter
    attr_accessor :record

    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

    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

    def perform
      value = get_resource()

      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

    private

    def group_by_date_field
      "#{@resource.table_name}.#{@params[:group_by_date_field]}"
    end

    def time_range
      "group_by_#{@params[:time_range].try(:downcase) || 'month'}"
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
forest_liana-7.8.2 app/services/forest_liana/line_stat_getter.rb
forest_liana-7.8.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-7.8.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-7.7.3 app/services/forest_liana/line_stat_getter.rb