Class: Beaver::Beaver

Inherits:
Object
  • Object
show all
Defined in:
lib/beaver/beaver.rb

Overview

The Beaver class, which keeps track of the files you’re parsing, the Beaver::Dam objects you’ve defined, and parses and filters the matching Beaver::Request objects.

beaver = Beaver.new do
  hit :help, :path => '/help' do
    puts "#{ip} needed help"
  end
end

# Method 1 - logs will be parsed and filtered line-by-line, then discarded. Performance should be constant regardless of the number of logs.
beaver.stream

# Method 2 - all of the logs will be parsed at once and stored in "beaver.requests". Then each request will be filtered.
# This does not scale as well, but is necessary *if you want to hang onto the parsed requests*.
beaver.parse.filter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &blk) ⇒ Beaver

Pass in globs or file paths. The final argument may be an options Hash. These options will be applied as matchers to all hits. See Beaver::Dam for available options.



86
87
88
89
90
91
92
93
# File 'lib/beaver/beaver.rb', line 86

def initialize(*args, &blk)
  @global_matchers = args.last.is_a?(Hash) ? args.pop : {}
  @logs = args.map { |a| Dir.glob(a) }
  @logs.flatten!
  @stdin, @tty = false, false
  @requests, @dams, @sums = [], {}, {}
  instance_eval(&blk) if block_given?
end

Instance Attribute Details

#damsObject (readonly)

The Beaver::Dam objects you’re defined



80
81
82
# File 'lib/beaver/beaver.rb', line 80

def dams
  @dams
end

#logsObject (readonly)

The log files to parse



74
75
76
# File 'lib/beaver/beaver.rb', line 74

def logs
  @logs
end

#requestsObject (readonly)

The Beaver::Request objects matched in the given log files (only availble with Beaver#parse)



82
83
84
# File 'lib/beaver/beaver.rb', line 82

def requests
  @requests
end

#stdinObject

Parse stdin (ignores tty)



76
77
78
# File 'lib/beaver/beaver.rb', line 76

def stdin
  @stdin
end

#ttyObject

Enables parsing from tty if @stdin in also true



78
79
80
# File 'lib/beaver/beaver.rb', line 78

def tty
  @tty
end

Instance Method Details

#dam(name, hit_options = {}, &callback) ⇒ Object

Define a sumarry for a Dam



104
105
106
107
108
109
# File 'lib/beaver/beaver.rb', line 104

def dam(name, hit_options={}, &callback)
  $stderr.puts "WARNING Overwriting existing dam '#{name}'" if @sums.has_key? name
  @sums[name] = callback
  # Optionally create a new hit
  hit(name, hit_options) if @dams[name].nil?
end

#filterObject

Filter @requests through the dams. (Beaver#parse must be run before this, or there will be no @requests.) Requests will be kept around after the run.



129
130
131
132
133
134
135
136
137
# File 'lib/beaver/beaver.rb', line 129

def filter
  for request in @requests
    hit_dams(request) do |dam|
      dam.hits << request
    end
  end
  summarize_dams
  self
end

#hit(dam_name, matchers = {}, &callback) ⇒ Object

Creates a new Dam and appends it to this Beaver. name should be a unique symbol. See Beaver::Dam for available options.



97
98
99
100
101
# File 'lib/beaver/beaver.rb', line 97

def hit(dam_name, matchers={}, &callback)
  $stderr.puts "WARNING Overwriting existing hit '#{dam_name}'" if @dams.has_key? dam_name
  matchers = @global_matchers.merge matchers
  @dams[dam_name] = Dam.new(dam_name, matchers, &callback)
end

#parseObject

Parse the logs into @requests. This does not run them through the dams. To do that call Beaver#filter afterwards.



140
141
142
143
144
# File 'lib/beaver/beaver.rb', line 140

def parse
  @requests.clear
  _parse { |request| @requests << request }
  self
end

#stdin!Object

Tells this Beaver to look in STDIN for log content to parse. NOTE This ignores tty input unless you also call Beaver#tty!.

Must be called before “stream” or “parse” to have any effect. Returns “self,” so it is chainable. Can also be used in the DSL.



149
150
151
152
# File 'lib/beaver/beaver.rb', line 149

def stdin!
  @stdin = true
  self
end

#streamObject

Parses the logs and immediately filters them through the dams. Requests are not retained, so this should scale better to very large sets of logs.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/beaver/beaver.rb', line 113

def stream
  # Match the request against each dam, and run the dam's callback
  _parse do |request|
    hit_dams(request) do |dam|
      dam.hits << request if @sums[dam.name]
    end
  end
  # Run the callback for each summary
  summarize_dams do |dam|
    dam.hits.clear # Clean up
  end
  self
end

#tty!Object

Tells this Beaver to look in STDIN for tty input.

Must be called before “stream” or “parse” to have any effect. Returns “self,” so it is chainable. Can also be used in the DSL.



157
158
159
160
161
# File 'lib/beaver/beaver.rb', line 157

def tty!
  stdin!
  @tty = true
  self
end