Sha256: f9f3d5498676acd6d9c732cd8b2c17523fe641d74833b0958f26f130638b2579

Contents?: true

Size: 1.41 KB

Versions: 40

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[:timeRange].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[:filter], @user, @resource.name)

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

      Groupdate.week_start = :monday

      value = value.send(timeRange, group_by_date_field, time_zone: client_timezone)

      value = value.send(@params[:aggregator].downcase, @params[:aggregateFieldName])
        .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[:groupByFieldName]}"
    end

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

  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
forest_liana-8.3.3 app/services/forest_liana/line_stat_getter.rb
forest_liana-8.3.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-8.3.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.1.10 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.1.9 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.1.8 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.1.7 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.1.6 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.1.5 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.1.4 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.1.3 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.1.2 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.1.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.1.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-8.2.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.0.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-9.0.0.beta.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-8.1.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-8.0.17 app/services/forest_liana/line_stat_getter.rb
forest_liana-8.0.16 app/services/forest_liana/line_stat_getter.rb