Class: Evidence::ActionParser
- Inherits:
-
Object
- Object
- Evidence::ActionParser
- Defined in:
- lib/evidence/action_parser.rb
Instance Method Summary collapse
- #end_action?(msg) ⇒ Boolean
-
#initialize(pid, message, action_patterns) ⇒ ActionParser
constructor
A new instance of ActionParser.
- #parse_action_logs(logs) ⇒ Object
- #request(msg) ⇒ Object
- #response(msg) ⇒ Object
- #start_action?(msg) ⇒ Boolean
- #to_hash(m) ⇒ Object
- #to_proc ⇒ Object
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, , action_patterns) @pid, @message = pid, @processes = Hash.new @start_action_pattern, @end_action_pattern = action_patterns[:start], action_patterns[:end] end |
Instance Method Details
#end_action?(msg) ⇒ 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
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_proc ⇒ Object
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 |