tailstrom 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b35a6296aafaca9109eb5fa60b6adaa0e2d98a2f
4
- data.tar.gz: c4e00a60b880f28e030c88421e45795953beabb1
3
+ metadata.gz: 30ba940607a94b7a30111d0a1d5cfc64d4ffbe92
4
+ data.tar.gz: 55a929d508f09b314fd783a4ba3843b89b3c9c0e
5
5
  SHA512:
6
- metadata.gz: 9de2b148f1eb4fa79783a3a3028c4c3e6f948166eb07f6fae1bdc0eae3e5f57ff978bc2e6803c85afc91a49a424852798e9db4c29f1af026d66a266fbddedf08
7
- data.tar.gz: 6bcbfb29db916fbce4671daf296377a57fdf945f3b1e016c8afe3fba6c2f0740e44d5c4a3066b31cdaf7a89a748fd529bbc366b350968d9ec3503eeb16c58f2a
6
+ metadata.gz: 046e9476d9ed1d61b3ac8127fced3cfcd571de4b30d900aab9474da256fae61043a3a014ffb6fe0b40f96800280a6c92414293229cb01894e338dd5f92c453f0
7
+ data.tar.gz: 3da54d4b6b9c5d9270b6d65ee810536d7d1e65264e3c6dd54f83e37cfe52eac258443f4dafdd52e41272ea7103a20c1a18fe2aae3a341a66b044a6f4293cd01e
data/bin/tailstrom CHANGED
@@ -1,6 +1,23 @@
1
1
  #!/usr/bin/env ruby
2
- $: << File.expand_path('../../lib', __FILE__)
2
+ require 'optparse'
3
+
4
+ options = {
5
+ :delimiter => "\t",
6
+ :interval => 1,
7
+ :mode => :stat
8
+ }
9
+ OptionParser.new(ARGV) {|opt|
10
+ opt.banner = "tail -f access.log | #{$0} <OPTIONS>"
11
+ opt.on('-f [field]', Integer, 'value field') {|v| options[:field] = v - 1 }
12
+ opt.on('-d [delimiter]', String) {|v| options[:delimiter] = v }
13
+ opt.on('-i [interval]', Integer, 'interval for stat mode') {|v| options[:interval] = v }
14
+ opt.on('-e [filter]', String) {|v| options[:filter] = v }
15
+ opt.on('--stat', 'statistics mode (default)') { options[:mode] = :stat }
16
+ opt.on('--print', 'print line mode') { options[:mode] = :print }
17
+ }.parse!
3
18
 
4
- require 'tailstrom/command/stat'
5
- cmd = Tailstrom::Command::Stat.new(ARGV)
19
+ $: << File.expand_path('../../lib', __FILE__)
20
+ require "tailstrom/command/#{options[:mode]}"
21
+ cls = Module.const_get "Tailstrom::Command::#{options[:mode].capitalize}"
22
+ cmd = cls.new options
6
23
  cmd.run
@@ -0,0 +1,19 @@
1
+ require 'tailstrom/tail_reader'
2
+
3
+ module Tailstrom
4
+ module Command
5
+ class Print
6
+ def initialize(options)
7
+ @infile = $stdin
8
+ @options = options
9
+ end
10
+
11
+ def run
12
+ reader = TailReader.new @infile, @options
13
+ reader.each_line do |line|
14
+ puts line[:line]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,7 +1,6 @@
1
- require 'optparse'
2
- require 'tailstrom/counter_collection'
1
+ require 'tailstrom/counter'
3
2
  require 'tailstrom/table'
4
- require 'thread'
3
+ require 'tailstrom/tail_reader'
5
4
 
6
5
  module Tailstrom
7
6
  module Command
@@ -13,56 +12,38 @@ module Tailstrom
13
12
  { :name => 'avg', :width => 15 }
14
13
  ]
15
14
 
16
- def initialize(argv)
15
+ def initialize(options)
17
16
  @infile = $stdin
18
- @counters = CounterCollection.new
17
+ @counter = Counter.new
19
18
  @table = Table.new SCHEMA
20
- parse_option argv
19
+ @options = options
21
20
  end
22
21
 
23
22
  def run
24
- Thread.start {
25
- loop do
26
- while line = @infile.gets
27
- line = line.chomp
28
- parse_line line
29
- end
30
- sleep 0.1
23
+ Thread.start do
24
+ reader = TailReader.new @infile, @options
25
+ reader.each_line do |line|
26
+ @counter << line[:value]
31
27
  end
32
- }
28
+ end
33
29
 
34
30
  @table.print_header
35
31
 
36
32
  loop do
37
- unless @counters.empty?
38
- c = @counters[:all]
39
- @table.print_row c.count, c.min, c.max, c.avg
33
+ if @counter
34
+ @table.print_row(
35
+ @counter.count,
36
+ @counter.min,
37
+ @counter.max,
38
+ @counter.avg
39
+ )
40
40
  end
41
- @counters.clear
41
+ @counter.clear
42
42
  sleep @options[:interval]
43
43
  end
44
44
  rescue Interrupt
45
45
  exit 0
46
46
  end
47
-
48
- def parse_line(line)
49
- columns = line.split @options[:delimiter]
50
- value = @options[:field] ? columns[@options[:field]] : line
51
- value = value =~ /\./ ? value.to_f : value.to_i
52
- @counters[:all] << value
53
- end
54
-
55
- def parse_option(argv)
56
- @options = {
57
- :delimiter => "\t",
58
- :interval => 1
59
- }
60
- opt = OptionParser.new(argv)
61
- opt.on('-f [field]', Integer) {|v| @options[:field] = v - 1 }
62
- opt.on('-d [delimiter]', String) {|v| @options[:delimiter] = v }
63
- opt.on('-i [interval]', Integer) {|v| @options[:interval] = v }
64
- opt.parse!
65
- end
66
47
  end
67
48
  end
68
49
  end
@@ -1,10 +1,28 @@
1
1
  module Tailstrom
2
2
  class TailReader
3
- def initialize(infile)
4
- @inflie = infile
3
+ def initialize(infile, options={})
4
+ @infile = infile
5
+ @options = options
5
6
  end
6
7
 
7
- def run
8
+ def each_line
9
+ @infile.each_line do |line|
10
+ result = parse_line(line)
11
+ yield result if result
12
+ end
13
+ end
14
+
15
+ 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
19
+
20
+ if @options[:filter]
21
+ v, cols = value, columns # shorthands
22
+ return nil unless binding.eval(@options[:filter])
23
+ end
24
+
25
+ { :line => line, :columns => columns, :value => value }
8
26
  end
9
27
  end
10
28
  end
@@ -1,3 +1,3 @@
1
1
  module Tailstrom
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tailstrom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Issei Naruta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-02 00:00:00.000000000 Z
11
+ date: 2013-10-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: tailstrom is a utility for "tail -f"
14
14
  email:
@@ -27,6 +27,7 @@ files:
27
27
  - bin/dummylog
28
28
  - bin/tailstrom
29
29
  - lib/tailstrom.rb
30
+ - lib/tailstrom/command/print.rb
30
31
  - lib/tailstrom/command/stat.rb
31
32
  - lib/tailstrom/counter.rb
32
33
  - lib/tailstrom/counter_collection.rb