Sha256: 8f4cb4fe6e86209e6a799a1c293ad17ecb1ccb1c22e36fd00e5aa28956496640

Contents?: true

Size: 1.6 KB

Versions: 14

Compression:

Stored size: 1.6 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%W-%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 })
      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

14 entries across 14 versions & 1 rubygems

Version Path
forest_liana-1.9.7 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.9.6 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.9.5 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.9.4 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.9.3 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.9.2 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.9.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.9.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.8.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.8.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.7.10 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.7.9 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.7.8 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.7.7 app/services/forest_liana/line_stat_getter.rb