class Tailstrom::Command::Stat

Public Class Methods

new(options) click to toggle source
# File lib/tailstrom/command/stat.rb, line 8
def initialize(options)
  @infile = options[:static_infile] || $stdin
  @counters = CounterCollection.new
  @options = { :async => !options[:static_infile] }.merge options
  @table = Table.new schema
end

Public Instance Methods

run() click to toggle source
# File lib/tailstrom/command/stat.rb, line 26
def run
  reader = TailReader.new @infile, @options
  reader.each_line do |line|
    @counters[line[:key]] << line[:value]
  end

  height = terminal_height
  printed_lines_sum = printed_lines = 0
  begin
    sleep @options[:interval]

    if printed_lines > 0 && (@multiline ||= @counters.size > 1)
      @table.puts
      printed_lines += 1
    end
    printed_lines = 0

    if (printed_lines_sum %= height) == 0
      @table.print_header
    end

    if @counters.size == 0
      @counters[:nil].count
    end

    printed_lines = print_counters
    printed_lines_sum += printed_lines

    @counters.clear
  end until reader.eof?
rescue Interrupt
  exit 0
end
schema() click to toggle source
# File lib/tailstrom/command/stat.rb, line 15
def schema
  [
    ({ :name => 'time', :width => 8 } if @options[:async]),
    { :name => 'count', :width => 7 },
    { :name => 'min', :width => 10 },
    { :name => 'max', :width => 10 },
    { :name => 'avg', :width => 10 },
    { :name => 'key', :width => 10, :align => :left }
  ].compact
end

Private Instance Methods

out_filter(key, counter) click to toggle source
# File lib/tailstrom/command/stat.rb, line 102
def out_filter(key, counter)
  if filter = @options[:out_filter]
    sum, avg, min, max, count =
      counter.sum, counter.avg, counter.min, counter.max, counter.count
    eval filter
  else
    true
  end
end
print_counters() click to toggle source
sorted_counters() click to toggle source
# File lib/tailstrom/command/stat.rb, line 86
def sorted_counters
  counters = @counters.to_a
  if sort = @options[:sort]
    counters = counters.sort_by do |key, c|
      sum, avg, min, max, count =
        c.sum, c.avg, c.min, c.max, c.count
      eval sort
    end
  else
    counters = counters.sort_by do |key, c|
      c.sum
    end
  end
  @options[:order] == :asc ? counters : counters.reverse
end
terminal_height() click to toggle source
# File lib/tailstrom/command/stat.rb, line 61
def terminal_height
  system 'which tput 2>&1 >/dev/null'
  if $? == 0
    `tput lines`.to_i - 4
  else
    10
  end
end