Sha256: ea2b09441aa94ecdaeb097cba26aac976c22368fc3d94655aea4410ce520ae44

Contents?: true

Size: 1.41 KB

Versions: 21

Compression:

Stored size: 1.41 KB

Contents

module ForestLiana
  class ValueStatGetter < StatGetter
    attr_accessor :record

    def perform
      return if @params[:aggregator].blank?
      resource = optimize_record_loading(@resource, get_resource)

      filters = ForestLiana::ScopeManager.append_scope_for_user(@params[:filter], @user, @resource.name)

      unless filters.blank?
        filter_parser = FiltersParser.new(filters, resource, @params[:timezone], @params)
        resource = filter_parser.apply_filters
        raw_previous_interval = filter_parser.get_previous_interval_condition

        if raw_previous_interval
          previous_value = filter_parser.apply_filters_on_previous_interval(raw_previous_interval)
        end
      end

      @record = Model::Stat.new(value: {
        countCurrent: count(resource),
        countPrevious: previous_value ? count(previous_value) : nil
      })
    end

    private

    def count(value)
      uniq = @params[:aggregator].downcase == 'count'

      if Rails::VERSION::MAJOR >= 4
        if uniq
          # NOTICE: uniq is deprecated since Rails 5.0
          value = Rails::VERSION::MAJOR >= 5 ? value.distinct : value.uniq
        end
        value.send(@params[:aggregator].downcase, aggregate_field)
      else
        value.send(@params[:aggregator].downcase, aggregate_field, distinct: uniq)
      end
    end

    def aggregate_field
      @params[:aggregateFieldName] || @resource.primary_key
    end

  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
forest_liana-8.0.16 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.15 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.14 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.13 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.12 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.11 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.10 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.9 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.8 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.7 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.6 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.5 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.4 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.3 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.2 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.1 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.0 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.0.beta.4 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.0.beta.3 app/services/forest_liana/value_stat_getter.rb
forest_liana-8.0.0.beta.2 app/services/forest_liana/value_stat_getter.rb