Sha256: 50837f73513c82c32cd28841d694f6e3e21455f0c8a557d4433c9d64d784d426

Contents?: true

Size: 1.56 KB

Versions: 60

Compression:

Stored size: 1.56 KB

Contents

module ForestLiana
  class LineStatGetter < StatGetter
    attr_accessor :record

    def initialize(resource, params)
      super(resource, params)
    end

    def client_timezone
      @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().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

60 entries across 60 versions & 1 rubygems

Version Path
forest_liana-3.0.0.pre.beta.5 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.14.7 app/services/forest_liana/line_stat_getter.rb
forest_liana-3.0.0.pre.beta.3 app/services/forest_liana/line_stat_getter.rb
forest_liana-3.0.0.pre.beta.2 app/services/forest_liana/line_stat_getter.rb
forest_liana-3.0.0.pre.beta.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-3.0.0.pre.beta.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.14.6 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.14.5 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.14.4 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.14.3 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.14.2 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.14.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.14.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.13.7 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.13.6 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.13.5 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.13.4 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.13.3 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.13.2 app/services/forest_liana/line_stat_getter.rb
forest_liana-2.13.1 app/services/forest_liana/line_stat_getter.rb