Sha256: 8d8a1493c2fd09531552d9608a09bba0e9a71c0e0c78d50ec2320d0c778cdbfb

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

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 => 10 },
        { :name => 'max', :width => 10 },
        { :name => 'avg', :width => 10 },
        { :name => 'key', :width => 10, :align => :left }
      ]

      def initialize(options)
        @infile = $stdin
        @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|
            @counters[line[:key]] << line[:value]
          end
          puts 'EOF'
        end

        height = `put lines`.to_i - 4 rescue 10
        @i = 0
        loop do
          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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tailstrom-0.0.4 lib/tailstrom/command/stat.rb