lib/tailstrom/command/stat.rb in tailstrom-0.0.1 vs lib/tailstrom/command/stat.rb in tailstrom-0.0.2

- old
+ new

@@ -1,9 +1,8 @@ -require 'optparse' -require 'tailstrom/counter_collection' +require 'tailstrom/counter' require 'tailstrom/table' -require 'thread' +require 'tailstrom/tail_reader' module Tailstrom module Command class Stat SCHEMA = [ @@ -11,58 +10,40 @@ { :name => 'min', :width => 15 }, { :name => 'max', :width => 15 }, { :name => 'avg', :width => 15 } ] - def initialize(argv) + def initialize(options) @infile = $stdin - @counters = CounterCollection.new + @counter = Counter.new @table = Table.new SCHEMA - parse_option argv + @options = options end def run - Thread.start { - loop do - while line = @infile.gets - line = line.chomp - parse_line line - end - sleep 0.1 + Thread.start do + reader = TailReader.new @infile, @options + reader.each_line do |line| + @counter << line[:value] end - } + end @table.print_header loop do - unless @counters.empty? - c = @counters[:all] - @table.print_row c.count, c.min, c.max, c.avg + if @counter + @table.print_row( + @counter.count, + @counter.min, + @counter.max, + @counter.avg + ) end - @counters.clear + @counter.clear sleep @options[:interval] end rescue Interrupt exit 0 - end - - def parse_line(line) - columns = line.split @options[:delimiter] - value = @options[:field] ? columns[@options[:field]] : line - value = value =~ /\./ ? value.to_f : value.to_i - @counters[:all] << value - end - - def parse_option(argv) - @options = { - :delimiter => "\t", - :interval => 1 - } - opt = OptionParser.new(argv) - opt.on('-f [field]', Integer) {|v| @options[:field] = v - 1 } - opt.on('-d [delimiter]', String) {|v| @options[:delimiter] = v } - opt.on('-i [interval]', Integer) {|v| @options[:interval] = v } - opt.parse! end end end end