tailstrom 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51c1542ccac5cf92967e2b680b6248c3ad8d331c
4
- data.tar.gz: 4d5bd73e287be37c0b8ebb7b66bfc6f420671794
3
+ metadata.gz: d6fec2ff65f0328d66e043a18c115cd3015b9caf
4
+ data.tar.gz: 647ff07f72e2450bc69405fb2705a3996c040e78
5
5
  SHA512:
6
- metadata.gz: e33172c883c53c51c92601836402177cbb0bebcecd8c330b0354567f5b5b34bd74e81b810ab142c7fce9e4ba48e45bc84c2539860ff985dd6986a53f48ea6a87
7
- data.tar.gz: 653713a2e282bf351260423bcc42cb0f08e590664f76dcab15b99b8b90f858a4664ac763405375658e65a41f0a468f709b8eaf721ee81613ce33c006225881e9
6
+ metadata.gz: c557df7bb847da8d28c3f0f95193719bd65a0c1222e4f18fc2d0ea724c378ce6b8de4d1ece305ec969090b9cfe8dd704d79466d8e0ccb4b25ad544467a5dd135
7
+ data.tar.gz: 8f0edf474fee9be1783f114287826bd5ecc6c641825bdc372b9a292c9ce3e68e81f3dfc630e43d397418986b89ef50512f2f47db39df2da2572835afc34c034d
data/bin/tailstrom CHANGED
@@ -8,10 +8,12 @@ options = {
8
8
  }
9
9
  OptionParser.new(ARGV) {|opt|
10
10
  opt.banner = "tail -f access.log | #{$0} <OPTIONS>"
11
- opt.on('-f [field]', Integer, 'value field') {|v| options[:field] = v - 1 }
11
+ opt.on('-f [num]', Integer, 'value field') {|v| options[:field] = v }
12
+ opt.on('-k [num]', Integer, 'key field') {|v| options[:key] = v }
12
13
  opt.on('-d [delimiter]', String) {|v| options[:delimiter] = v }
13
14
  opt.on('-i [interval]', Integer, 'interval for stat mode') {|v| options[:interval] = v }
14
15
  opt.on('-e [filter]', String) {|v| options[:filter] = v }
16
+ opt.on('--map [file]', String, 'mapping file') {|v| options[:map] = File.read(v) }
15
17
  opt.on('--stat', 'statistics mode (default)') { options[:mode] = :stat }
16
18
  opt.on('--print', 'print line mode') { options[:mode] = :print }
17
19
  }.parse!
data/example/map1.rb ADDED
@@ -0,0 +1,10 @@
1
+ value = col[2]
2
+ key = case col[3]
3
+ when %r(^/photos/)
4
+ 'photos'
5
+ when %r(^/users/)
6
+ 'users'
7
+ when %r(^/products/)
8
+ 'products'
9
+ end
10
+ #filter = 'value > 0'
@@ -1,4 +1,4 @@
1
- require 'tailstrom/counter'
1
+ require 'tailstrom/counter_collection'
2
2
  require 'tailstrom/table'
3
3
  require 'tailstrom/tail_reader'
4
4
 
@@ -6,15 +6,17 @@ module Tailstrom
6
6
  module Command
7
7
  class Stat
8
8
  SCHEMA = [
9
+ { :name => 'time', :width => 8 },
9
10
  { :name => 'count', :width => 7 },
10
- { :name => 'min', :width => 15 },
11
- { :name => 'max', :width => 15 },
12
- { :name => 'avg', :width => 15 }
11
+ { :name => 'min', :width => 10 },
12
+ { :name => 'max', :width => 10 },
13
+ { :name => 'avg', :width => 10 },
14
+ { :name => 'key', :width => 10, :align => :left }
13
15
  ]
14
16
 
15
17
  def initialize(options)
16
18
  @infile = $stdin
17
- @counter = Counter.new
19
+ @counters = CounterCollection.new
18
20
  @table = Table.new SCHEMA
19
21
  @options = options
20
22
  end
@@ -23,23 +25,34 @@ module Tailstrom
23
25
  Thread.start do
24
26
  reader = TailReader.new @infile, @options
25
27
  reader.each_line do |line|
26
- @counter << line[:value]
28
+ @counters[line[:key]] << line[:value]
27
29
  end
30
+ puts 'EOF'
28
31
  end
29
32
 
30
- @table.print_header
31
-
33
+ height = `put lines`.to_i - 4 rescue 10
34
+ @i = 0
32
35
  loop do
33
- if @counter
34
- @table.print_row(
35
- @counter.count,
36
- @counter.min,
37
- @counter.max,
38
- @counter.avg
39
- )
40
- end
41
- @counter.clear
42
36
  sleep @options[:interval]
37
+
38
+ if @i % height == 0
39
+ @table.print_header
40
+ end
41
+
42
+ @counters.to_a.sort_by {|key, c|
43
+ c.sum
44
+ }.reverse_each do |key, c|
45
+ key = (key == :nil ? nil : key)
46
+ time = Time.now.strftime("%H:%M:%S")
47
+ @table.print_row time, c.count, c.min, c.max, c.avg, key
48
+ end
49
+
50
+ if @counters.size > 1
51
+ @table.puts
52
+ end
53
+
54
+ @counters.clear
55
+ @i = @i + 1
43
56
  end
44
57
  rescue Interrupt
45
58
  exit 0
@@ -3,11 +3,11 @@ require 'tailstrom/counter'
3
3
  module Tailstrom
4
4
  class CounterCollection
5
5
  def initialize
6
- @counters = Hash.new {|h, k| h[k] = Counter.new }
6
+ clear
7
7
  end
8
8
 
9
9
  def clear
10
- @counters.values.each(&:clear)
10
+ @counters = Hash.new {|h, k| h[k] = Counter.new }
11
11
  end
12
12
 
13
13
  def [](key)
@@ -17,5 +17,17 @@ module Tailstrom
17
17
  def empty?
18
18
  @counters.empty?
19
19
  end
20
+
21
+ def each(&block)
22
+ @counters.each &block
23
+ end
24
+
25
+ def to_a
26
+ @counters.to_a
27
+ end
28
+
29
+ def size
30
+ @counters.size
31
+ end
20
32
  end
21
33
  end
@@ -10,9 +10,10 @@ module Tailstrom
10
10
  col_schema = @schema[i]
11
11
  num_str = col ? num_with_delim(col) : '-'
12
12
  print ' ' if i > 0
13
- printf "%#{col_schema[:width]}s", num_str
13
+ align = col_schema[:align].to_s == 'left' ? '-' : nil
14
+ printf "%#{align}#{col_schema[:width]}s", num_str
14
15
  end
15
- @out.puts
16
+ self.puts
16
17
  end
17
18
 
18
19
  def print_header
@@ -21,13 +22,16 @@ module Tailstrom
21
22
  if i > 0
22
23
  border += '-'
23
24
  head += ' '
24
- #border += '+'
25
- #head += '|'
26
25
  end
26
+ align = col[:align].to_s == 'left' ? '-' : nil
27
27
  border += '-' * col[:width]
28
- head += "%#{col[:width]}s" % col[:name]
28
+ head += "%#{align}#{col[:width]}s" % col[:name]
29
29
  end
30
- @out.puts border, head, border
30
+ self.puts border, head, border
31
+ end
32
+
33
+ def puts(*args)
34
+ @out.puts *args
31
35
  end
32
36
 
33
37
  private
@@ -7,22 +7,33 @@ module Tailstrom
7
7
 
8
8
  def each_line
9
9
  @infile.each_line do |line|
10
+ line.chomp!
10
11
  result = parse_line(line)
11
12
  yield result if result
12
13
  end
13
14
  end
14
15
 
15
16
  def parse_line(line)
16
- columns = line.split @options[:delimiter]
17
- value = @options[:field] ? columns[@options[:field]] : line
18
- value = value =~ /\./ ? value.to_f : value.to_i
17
+ col = line.split @options[:delimiter]
18
+ value = @options[:field] ? col[@options[:field]] : line
19
+ value = format_value value
20
+ key = @options[:key] ? col[@options[:key]] : :nil
21
+ filter = @options[:filter]
19
22
 
20
- if @options[:filter]
21
- v, cols = value, columns # shorthands
22
- return nil unless binding.eval(@options[:filter])
23
+ if @options[:map]
24
+ binding.eval(@options[:map])
25
+ value = format_value value
23
26
  end
24
27
 
25
- { :line => line, :columns => columns, :value => value }
28
+ if filter
29
+ return nil unless binding.eval(filter)
30
+ end
31
+
32
+ { :line => line, :columns => col, :key => key, :value => value }
33
+ end
34
+
35
+ def format_value(value)
36
+ value =~ /\./ ? value.to_f : value.to_i
26
37
  end
27
38
  end
28
39
  end
@@ -1,3 +1,3 @@
1
1
  module Tailstrom
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tailstrom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Issei Naruta
@@ -14,7 +14,6 @@ description: tailstrom is an utility for "tail -f"
14
14
  email:
15
15
  - naruta@cookpad.com
16
16
  executables:
17
- - dummylog
18
17
  - tailstrom
19
18
  extensions: []
20
19
  extra_rdoc_files: []
@@ -24,8 +23,9 @@ files:
24
23
  - LICENSE.txt
25
24
  - README.md
26
25
  - Rakefile
27
- - bin/dummylog
28
26
  - bin/tailstrom
27
+ - example/dummylog
28
+ - example/map1.rb
29
29
  - lib/tailstrom.rb
30
30
  - lib/tailstrom/command/print.rb
31
31
  - lib/tailstrom/command/stat.rb
File without changes