Class: Grok::Discovery

Inherits:
Object
  • Object
show all
Defined in:
lib/grok/pure/discovery.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grok) ⇒ Discovery

Returns a new instance of Discovery.



8
9
10
11
12
13
# File 'lib/grok/pure/discovery.rb', line 8

def initialize(grok)
  @grok = grok
  @logger = Cabin::Channel.new
  @logger.subscribe(Logger.new(STDOUT))
  @logger.level = :warn
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/grok/pure/discovery.rb', line 6

def logger
  @logger
end

Instance Method Details

#discover(text) ⇒ Object

def initialize



15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/grok/pure/discovery.rb', line 15

def discover(text)
  text = text.clone
  # TODO(sissel): Sort patterns by complexity, most complex first.
  #   - For each pattern, compile it in a grok by itself.
  #   - Make a dictionary of { "name" => Grok } for each pattern
  #   - Sort groks by complexity of the Grok#expanded_pattern
  groks = {}
  @grok.patterns.each do |name, expression| 
    grok = Grok.new
    # Copy in the same grok patterns from the parent
    grok.patterns.merge!(@grok.patterns)
    grok.compile("%{#{name}}")
    groks[name] = grok
  end

  patterns = groks.sort { |a, b| compare(a, b) }

  done = false
  while !done
    done = true # will reset this if we are not done later.
    patterns.each do |name, grok|
      # Skip patterns that lack complexity (SPACE, NOTSPACE, DATA, etc)
      next if complexity(grok.expanded_pattern) < 20
      m = grok.match(text)
      # Skip non-matches
      next unless m 
      part = text[m.start ... m.end]
      # Only include things that have word boundaries (not just words)
      next if part !~ /.\b./
      # Skip over parts that appear to include %{pattern} already
      next if part =~ /%{[^}+]}/
      acting = true
      text[m.start ... m.end] = "%{#{name}}"

      # Start the loop over again
      done = false
      break
    end
  end

  return text
end