Class: Memory::ValueAggregate

Inherits:
Object
  • Object
show all
Defined in:
lib/memory/aggregate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, &block) ⇒ ValueAggregate

Returns a new instance of ValueAggregate.



102
103
104
105
106
107
# File 'lib/memory/aggregate.rb', line 102

def initialize(title, &block)
  @title = title
  @metric = block
  
  @aggregates = Hash.new{|h,k| h[k] = Aggregate.new(k.inspect, &@metric)}
end

Instance Attribute Details

#aggregatesObject (readonly)

Returns the value of attribute aggregates.



111
112
113
# File 'lib/memory/aggregate.rb', line 111

def aggregates
  @aggregates
end

#metricObject (readonly)

Returns the value of attribute metric.



110
111
112
# File 'lib/memory/aggregate.rb', line 110

def metric
  @metric
end

#titleObject (readonly)

Returns the value of attribute title.



109
110
111
# File 'lib/memory/aggregate.rb', line 109

def title
  @title
end

Instance Method Details

#<<(allocation) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/memory/aggregate.rb', line 113

def << allocation
  if value = allocation.value
    aggregate = @aggregates[value]
    
    aggregate << allocation
  end
end

#aggregates_by(key) ⇒ Object



121
122
123
# File 'lib/memory/aggregate.rb', line 121

def aggregates_by(key)
  @aggregates.sort_by{|value, aggregate| [aggregate.total[key], value]}
end

#as_jsonObject



133
134
135
136
137
138
# File 'lib/memory/aggregate.rb', line 133

def as_json
  {
    title: @title,
    aggregates: @aggregates.map{|k, v| [k, v.as_json]}
  }
end


125
126
127
128
129
130
131
# File 'lib/memory/aggregate.rb', line 125

def print(io = $stderr, limit: 10, level: 2)
  io.puts "#{'#' * level} #{@title}", nil
  
  aggregates_by(:count).last(limit).reverse_each do |value, aggregate|
    aggregate.print(io, level: level+1)
  end
end