lib/tailstrom/command/stat.rb in tailstrom-0.0.3 vs lib/tailstrom/command/stat.rb in tailstrom-0.0.4

- old
+ new

@@ -1,46 +1,59 @@ -require 'tailstrom/counter' +require 'tailstrom/counter_collection' require 'tailstrom/table' require 'tailstrom/tail_reader' module Tailstrom module Command class Stat SCHEMA = [ + { :name => 'time', :width => 8 }, { :name => 'count', :width => 7 }, - { :name => 'min', :width => 15 }, - { :name => 'max', :width => 15 }, - { :name => 'avg', :width => 15 } + { :name => 'min', :width => 10 }, + { :name => 'max', :width => 10 }, + { :name => 'avg', :width => 10 }, + { :name => 'key', :width => 10, :align => :left } ] def initialize(options) @infile = $stdin - @counter = Counter.new + @counters = CounterCollection.new @table = Table.new SCHEMA @options = options end def run Thread.start do reader = TailReader.new @infile, @options reader.each_line do |line| - @counter << line[:value] + @counters[line[:key]] << line[:value] end + puts 'EOF' end - @table.print_header - + height = `put lines`.to_i - 4 rescue 10 + @i = 0 loop do - if @counter - @table.print_row( - @counter.count, - @counter.min, - @counter.max, - @counter.avg - ) - end - @counter.clear sleep @options[:interval] + + if @i % height == 0 + @table.print_header + end + + @counters.to_a.sort_by {|key, c| + c.sum + }.reverse_each do |key, c| + key = (key == :nil ? nil : key) + time = Time.now.strftime("%H:%M:%S") + @table.print_row time, c.count, c.min, c.max, c.avg, key + end + + if @counters.size > 1 + @table.puts + end + + @counters.clear + @i = @i + 1 end rescue Interrupt exit 0 end end