class PIM::Statistics::Statistic

Attributes

name[R]

Public Class Methods

new(name, **params, &block) click to toggle source
# File statistics.rb, line 192
def initialize name, **params, &block
  @name = name
  @params = params
  @block = block
end

Public Instance Methods

calculate_statistic(old_item, new_item, features: []) click to toggle source
# File statistics.rb, line 198
def calculate_statistic old_item, new_item, features: []

  result = []

  # Add scopes, if statistic param is set AND the feature is enabled
  # Note: "scopes" are only supported with "new" statistics
  # which no longer need to re-calculate "old" statistics!
  # Thus, we only get the scopes from "new_item".
  if PIM::Utils.is_opt?(@params, :scopes) and PIM::Utils.includes_value?(:scopes, features)
    scopes = PIM.get_value(new_item, :scopes__)
  end

  # Always add the 'nil' scope
  scopes = Set.new([nil]) + Array(scopes)

  calculator = StatisticCalculator.new(old_item, new_item, @params, scopes, &@block)

  counts = calculator.counts
  counts.each_pair do |scope, key_sub_counts|
    key_sub_counts.each_pair do |key, sub_counts|
      sub_counts.each_pair do |dimension, count|
        next if count.nil? or count == 0
        sub_count = {}
        sub_count[:name] = name
        sub_count[:scope] = scope.to_s unless PIM.is_empty?(scope)
        sub_count[:key] = key
        sub_count[:dimension] = dimension unless PIM.is_empty?(dimension)
        sub_count[:count] = count
        result << sub_count
      end
    end
  end

  return result

end