Sha256: aaf323424a7c7941c03bd5f936e00acad164b8e64ccf3256697d99c3c26f3913

Contents?: true

Size: 967 Bytes

Versions: 2

Compression:

Stored size: 967 Bytes

Contents

module RailsIdle
  class Report
    CALCULATED_KEY = :calculated
    COUNTER_KEY    = RailsIdle::Storage::COUNTER_KEY
    
    def initialize(collector)
      @tree = TreeBuilder.new(collector).tree
    end
    
    def prepare
      calc_counter!(@tree)
      #sort!(@obj)
      @tree
    end
    
    private
    
    def calc_counter!(obj)
      return obj[COUNTER_KEY] if obj[COUNTER_KEY].is_a?(Fixnum)
      return obj[CALCULATED_KEY] if obj[CALCULATED_KEY].is_a?(Fixnum)
      obj[CALCULATED_KEY] = obj.inject(0) do |sum, (key, nested_obj)|
        sum + (key.is_a?(Symbol) ? 0 : calc_counter!(nested_obj))
      end
      obj[CALCULATED_KEY]
    end
    
    def sort!(obj)
      obj.each do |k, v|        
        obj[k] = sort!(v) if v.is_a?(Hash)
      end
      obj.sort_by do |k, v|
        if v.is_a?(Hash)
          v[CALCULATED_KEY] || v[COUNTER_KEY] || 0 
        else
          Float::INFINITY
        end
      end.to_h
    end
      
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails-idle-0.0.10 lib/rails-idle/report.rb
rails-idle-0.0.9 lib/rails-idle/report.rb