module PIM::Statistics

Public Class Methods

calculate(&block) click to toggle source

Deprecated! Used for legacy statistics only! Please convert to ‘calculate_statistics’!

# File statistics.rb, line 351
def self.calculate &block

  # Create statistics manager without user statistics
  manager = create_statistics_manager

  # Calculate statistics
  result = manager.calculate_statistics $oldItem, $item

  # Create and calculate old user statistics
  old_result = OldStatisticsResult.new
  block.call $oldItem, $item, old_result
  result += old_result.as_json

  PIM.return_json(result)

end
calculate_statistics(&block) click to toggle source

Method to be called in data model specific statistics

# File statistics.rb, line 338
def self.calculate_statistics &block

  # Create statistics manager
  manager = create_statistics_manager &block

  # Calculate statistics
  result = manager.calculate_statistics $oldItem, $item

  PIM.return_json(result)

end
create_statistics_manager(&block) click to toggle source
# File statistics.rb, line 368
def self.create_statistics_manager &block

  # Create new statistics manager
  manager = StatisticsManager.new

  # Create system statistics
  manager.create_statistics do

    statistic_with_template '__categories_statistic' do

      dimension_keys = get_array_param(params, :dimensions)

      if old_item
        category = old_item['category__']
        dimension_values = get_item_values(old_item, dimension_keys)
        decr(category, *dimension_values)
      end
      if new_item
        category = new_item['category__']
        dimension_values = get_item_values(new_item, dimension_keys)
        incr(category, *dimension_values)
      end

    end

    statistic_with_template '__compliance_statistic' do

      dimension_keys = get_array_param(params, :dimensions)

      if old_item
        compliance = case
          when old_item['validation_dirty__'] then 'dirty'
          else
            is_empty?(old_item['validation__']) ? 'compliant' : 'non_compliant'
        end
        dimension_values = get_item_values(old_item, dimension_keys)
        decr(compliance, *dimension_values)
        decr('total', *dimension_values)
        decr('published', *dimension_values) if old_item['published__']
      end
      if new_item
        compliance = case
          when new_item['validation_dirty__'] then 'dirty'
          else
            is_empty?(new_item['validation__']) ? 'compliant' : 'non_compliant'
        end
        dimension_values = get_item_values(new_item, dimension_keys)
        incr(compliance, *dimension_values)
        incr('total', *dimension_values)
        incr('published', *dimension_values) if new_item['published__']
      end

    end

    statistic_with_template '__validation_overview', '__validation_errors_statistic' do

      dimension_keys = get_array_param(params, :dimensions)

      if old_item
        validations = old_item['validation__'] || []
        dimension_values = get_item_values(old_item, dimension_keys)
        decr_all(validations, *dimension_values)
      end
      if new_item
        validations = new_item['validation__'] || []
        dimension_values = get_item_values(new_item, dimension_keys)
        incr_all(validations, *dimension_values)
      end

    end

    statistic_with_template '__validation_warnings', '__validation_warnings_statistic' do

      dimension_keys = get_array_param(params, :dimensions)

      if old_item
        validations = old_item['validation_warnings__'] || []
        dimension_values = get_item_values(old_item, dimension_keys)
        decr_all(validations, *dimension_values)
      end
      if new_item
        validations = new_item['validation_warnings__'] || []
        dimension_values = get_item_values(new_item, dimension_keys)
        incr_all(validations, *dimension_values)
      end

    end

    statistic_with_template '__review_errors', '__review_errors_statistic' do

      dimension_keys = get_array_param(params, :dimensions)

      if old_item
        validations = old_item['reviewErrors__'] || []
        validations = validations.map do |validation|
          validation = validation.split(':', 4)[-1]
        end
        dimension_values = get_item_values(old_item, dimension_keys)
        decr_all(validations, *dimension_values)
      end
      if new_item
        validations = new_item['reviewErrors__'] || []
        validations = validations.map do |validation|
          validation = validation.split(':', 4)[-1]
        end
        dimension_values = get_item_values(new_item, dimension_keys)
        incr_all(validations, *dimension_values)
      end

    end

    statistic_with_template '__review_warnings', '__review_warnings_statistic' do

      dimension_keys = get_array_param(params, :dimensions)

      if old_item
        validations = old_item['reviewWarnings__'] || []
        validations = validations.map do |validation|
          validation = validation.split(':', 4)[-1]
        end
        dimension_values = get_item_values(old_item, dimension_keys)
        decr_all(validations, *dimension_values)
      end
      if new_item
        validations = new_item['reviewWarnings__'] || []
        validations = validations.map do |validation|
          validation = validation.split(':', 4)[-1]
        end
        dimension_values = get_item_values(new_item, dimension_keys)
        incr_all(validations, *dimension_values)
      end

    end

    statistic_with_template '__published_to', '__published_to_statistic' do

      dimension_keys = get_array_param(params, :dimensions)

      if old_item
        publications = old_item['publications__'] || []
        if not publications.empty?
          dimension_values = get_item_values(old_item, dimension_keys)
          decr_all(publications, *dimension_values)
        end
      end
      if new_item
        publications = new_item['publications__'] || []
        if not publications.empty?
          dimension_values = get_item_values(new_item, dimension_keys)
          incr_all(publications, *dimension_values)
        end
      end

    end

    statistic_with_template '__received_from', '__received_from_statistic' do

      dimension_keys = get_array_param(params, :dimensions)

      if old_item
        supplier = old_item['supplier__']
        if not PIM.is_empty?(supplier)
          dimension_values = get_item_values(old_item, dimension_keys)
          decr(supplier, *dimension_values)
        end
      end
      if new_item
        supplier = new_item['supplier__']
        if not PIM.is_empty?(supplier)
          dimension_values = get_item_values(new_item, dimension_keys)
          incr(supplier, *dimension_values)
        end
      end

    end

    statistic_with_template '__publications_active_statistic' do

      dimension_keys = get_array_param(params, :dimensions)

      if old_item
        publications_active = old_item['publications_active__'] || []
        if not publications_active.empty?
          dimension_values = get_item_values(old_item, dimension_keys)
          decr_all(publications_active, *dimension_values)
        end
      end
      if new_item
        publications_active = new_item['publications_active__'] || []
        if not publications_active.empty?
          dimension_values = get_item_values(new_item, dimension_keys)
          incr_all(publications_active, *dimension_values)
        end
      end

    end

    statistic_with_template '__publications_dirty_statistic' do

      dimension_keys = get_array_param(params, :dimensions)

      if old_item
        publications_dirty = old_item['publications_dirty__'] || []
        if not publications_dirty.empty?
          dimension_values = get_item_values(old_item, dimension_keys)
          decr_all(publications_dirty, *dimension_values)
        end
      end
      if new_item
        publications_dirty = new_item['publications_dirty__'] || []
        if not publications_dirty.empty?
          dimension_values = get_item_values(new_item, dimension_keys)
          incr_all(publications_dirty, *dimension_values)
        end
      end

    end

    statistic_with_template '__publications_statistic' do

      # Note that 'target' can be channelId:subDestination (or channelId or organizationId),
      # it is not possible to add any other dimension, because these are added using ":"!
      # The only way to fix it, is to either "swap" key and dimension or replace the ":"
      # in our target.

      if old_item

        published = old_item["publications__"] || []
        published.each do |target|
          decr("published", target)
        end

        publications_active = old_item["publications_active__"] || []
        publications_active.each do |target|
          decr("active", target)
        end

        publications_dirty = old_item["publications_dirty__"] || []
        publications_dirty.each do |target|
          decr("dirty", target)
        end

      end
      if new_item

        published = new_item["publications__"] || []
        published.each do |target|
          incr("published", target)
        end

        publications_active = new_item["publications_active__"] || []
        publications_active.each do |target|
          incr("active", target)
        end

        publications_dirty = new_item["publications_dirty__"] || []
        publications_dirty.each do |target|
          incr("dirty", target)
        end

      end

    end

    statistic_with_template '__review_statistic' do

      if old_item

        # Add statistic by publication
        publications = old_item["publications__"] || []
        publications.each do |publication|
          decr("total_published", publication) # channelId:subDestination OR channelId OR organizationId
        end

        # Add statistics by review status
        reviews = old_item["reviews__"] || []
        reviews.each do |review|
          type, reviewer, sub_destination, review_status = review.split(":")
            reviewer = "#{reviewer}:#{sub_destination}" unless sub_destination.nil? or sub_destination.empty?
            decr(review_status, reviewer) # reviewer = channelId:subDestination OR channelId
            decr("total_reviews", reviewer)
        end

      end

      if new_item

        # Add statistic by publication
        publications = new_item["publications__"] || []
        publications.each do |publication|
          incr("total_published", publication) # channelId:subDestination OR channelId OR organizationId
        end

        # Add statistics by review status
        reviews = new_item["reviews__"] || []
        reviews.each do |review|
          type, reviewer, sub_destination, review_status = review.split(":")
            reviewer = "#{reviewer}:#{sub_destination}" unless sub_destination.nil? or sub_destination.empty?
            incr(review_status, reviewer) # reviewer = channelId:subDestination OR channelId
            incr("total_reviews", reviewer)
        end

      end

    end

  end

  # Create user statistics, if specified
  manager.create_statistics &block if block

  manager

end