Sha256: c19618b4e4db2ecb7000fe456c6abef537fab36c66eb8dc5f2984f233f3c7642

Contents?: true

Size: 1.64 KB

Versions: 28

Compression:

Stored size: 1.64 KB

Contents

module ForestLiana
  class LineStatGetter < StatGetter
    attr_accessor :record

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

    def client_timezone
      ActiveSupport::TimeZone[@timezone_offset].name
    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().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,
        week_start: :mon
      })

      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

28 entries across 28 versions & 1 rubygems

Version Path
forest_liana-2.5.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.4.9 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.4.8 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.4.7 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.4.6 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.4.5 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.4.4 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.4.3 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.4.2 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.4.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.4.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.3.5 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.3.4 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.3.3 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.3.2 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.3.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.3.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.2.2 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.2.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.2.0 app/services/forest_liana/line_stat_getter.rb