Class: Tailstrom::Command::Stat

Inherits:
Object
  • Object
show all
Defined in:
lib/tailstrom/command/stat.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Stat

Returns a new instance of Stat.



8
9
10
11
12
13
# 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

Instance Method Details

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

#schemaObject



15
16
17
18
19
20
21
22
23
24
# 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