Class: RawkLog::StatHash

Inherits:
Object
  • Object
show all
Defined in:
lib/rawk_log/stat_hash.rb

Instance Method Summary collapse

Constructor Details

#initializeStatHash

Returns a new instance of StatHash.



6
7
8
# File 'lib/rawk_log/stat_hash.rb', line 6

def initialize
  @stats = Hash.new
end

Instance Method Details

#add(key, time) ⇒ Object



14
15
16
17
# File 'lib/rawk_log/stat_hash.rb', line 14

def add(key,time)
  stat = @stats[key] || (@stats[key] = RawkLog::Stat.new(key))
  stat.add(time)
end

#empty?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/rawk_log/stat_hash.rb', line 10

def empty?
  @stats.empty?
end


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rawk_log/stat_hash.rb', line 19

def print(args={:sort_by=>'key',:ascending=>true,:limit=>nil})
  values = @stats.values
  return Stat::DEFAULT_LABEL_SIZE if values.empty?
  order = (args[:ascending] || args[:ascending].nil?) ? 1 : -1
  values.sort! {|a,b| 
    as = a.send(args[:sort_by])
    bs = b.send(args[:sort_by])
    (as && bs) ? order*(as<=>bs) : 0
  }
  #values.sort! {|a,b| a.key<=>b.key}
  limit = args[:limit]
  if limit
    values = values[0,limit]
  end
  @label_size = values.collect{|v| v.key.size }.max
  @label_size = Stat::DEFAULT_LABEL_SIZE if @label_size < Stat::DEFAULT_LABEL_SIZE
  puts values[0].header(@label_size)
  for stat in values
    puts stat.to_s(@label_size)
  end
  @label_size
end