Class: Evidence::ActionParser

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

Instance Method Summary collapse

Constructor Details

#initialize(pid, message, action_patterns) ⇒ ActionParser

Returns a new instance of ActionParser.



4
5
6
7
8
# File 'lib/evidence/action_parser.rb', line 4

def initialize(pid, message, action_patterns)
  @pid, @message = pid, message
  @processes = Hash.new
  @start_action_pattern, @end_action_pattern = action_patterns[:start], action_patterns[:end]
end

Instance Method Details

#end_action?(msg) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/evidence/action_parser.rb', line 45

def end_action?(msg)
  msg =~ @end_action_pattern
end

#parse_action_logs(logs) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/evidence/action_parser.rb', line 37

def parse_action_logs(logs)
  {
    request: request(@message[logs[0]]),
    response: response(@message[logs[-1]]),
    logs: logs
  }
end

#request(msg) ⇒ Object



53
54
55
# File 'lib/evidence/action_parser.rb', line 53

def request(msg)
  to_hash(@start_action_pattern.match(msg))
end

#response(msg) ⇒ Object



57
58
59
# File 'lib/evidence/action_parser.rb', line 57

def response(msg)
  to_hash(@end_action_pattern.match(msg))
end

#start_action?(msg) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/evidence/action_parser.rb', line 49

def start_action?(msg)
  msg =~ @start_action_pattern
end

#to_hash(m) ⇒ Object



61
62
63
# File 'lib/evidence/action_parser.rb', line 61

def to_hash(m)
  Hash[m.names.map(&:to_sym).zip(m.captures)]
end

#to_procObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/evidence/action_parser.rb', line 10

def to_proc
  lambda do |log|
    pid = @pid[log]
    msg = @message[log]
    if @processes.has_key?(pid)
      if start_action?(msg)
        warn "[WARN] Found start action following another start action: #{msg}"
        warn "[WARN] Ignore logs: #{@processes.delete(pid).inspect}"
        @processes[pid] = [log]
        nil
      else
        @processes[pid] << log
        if end_action?(msg)
          parse_action_logs(@processes.delete(pid))
        end
      end
    else
      if start_action?(msg)
        @processes[pid] = [log]
      else
        warn "[WARN] Ignore a log that is not start action and also not after a start action: #{log.inspect}"
      end
      nil
    end
  end
end