Class: U3d::LogAnalyzer

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

Overview

Analyzes log by filtering output along a set of rules rubocop:disable Metrics/ClassLength, Metrics/PerceivedComplexity, Metrics/BlockNesting

Constant Summary collapse

RULES_PATH =
File.expand_path('../../config/log_rules.json', __dir__)
MEMORY_SIZE =
10

Instance Method Summary collapse

Constructor Details

#initializeLogAnalyzer

Returns a new instance of LogAnalyzer.



35
36
37
38
39
40
41
42
# File 'lib/u3d/log_analyzer.rb', line 35

def initialize
  @lines_memory = Array.new(MEMORY_SIZE)
  @active_phase = nil
  @active_rule = nil
  @context = {}
  @rule_lines_buffer = []
  @generic_rules, @phases = load_rules
end

Instance Method Details

#load_rulesObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/u3d/log_analyzer.rb', line 44

def load_rules
  generic_rules = {}
  phases = {}

  data = JSON.parse(File.read(rules_path))

  if data['GENERAL'] && data['GENERAL']['active']
    data['GENERAL']['rules'].each do |rn, r|
      generic_rules[rn] = r if parse_rule(r)
    end
  end
  data.delete('GENERAL')
  data.each do |name, phase|
    # Phase parsing
    next unless phase['active']
    next if phase['phase_start_pattern'].nil?

    phase['phase_start_pattern'] = Regexp.new phase['phase_start_pattern']
    phase['phase_end_pattern'] = Regexp.new phase['phase_end_pattern'] if phase['phase_end_pattern']
    phase.delete('comment')
    # Rules parsing
    temp_rules = {}
    unless phase['silent'] == true
      phase['rules'].each do |rn, r|
        temp_rules[rn] = r if parse_rule(r)
      end
    end
    phase['rules'] = temp_rules
    phases[name] = phase
  end
  return generic_rules, phases
end

#parse_line(line) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/u3d/log_analyzer.rb', line 77

def parse_line(line)
  # Insert new line and remove last stored line
  @lines_memory.push(line).shift

  # Check if phase is changing
  @phases.each do |name, phase|
    next if name == @active_phase
    next unless line =~ phase['phase_start_pattern']

    finish_phase if @active_phase
    @active_phase = name
    UI.verbose("--- Beginning #{name} phase ---")
    break
  end

  apply_ruleset = lambda do |ruleset, header|
    # Apply the active rule
    if @active_rule && ruleset[@active_rule]
      rule = ruleset[@active_rule]
      pattern = rule['end_pattern']

      # Is it the end of the rule?
      if line =~ pattern
        unless @rule_lines_buffer.empty?
          @rule_lines_buffer.each do |l|
            UI.send(rule['type'], "[#{header}] " + l)
          end
        end
        if rule['end_message'] != false
          if rule['end_message']
            match = line.match(pattern)
            params = match.names.map(&:to_sym).zip(match.captures).to_h
            message = inject(rule['end_message'], params: params)
          else
            message = line.chomp
          end
          message = "[#{header}] " + message
          UI.send(rule['type'], message)
        end
        @active_rule = nil
        @context.clear
        @rule_lines_buffer.clear

      # It's not the end of the rules, should the line be stored?
      elsif rule['store_lines']
        match = false
        rule['ignore_lines']&.each do |pat|
          if line =~ pat
            match = true
            break
          end
        end
        @rule_lines_buffer << line.chomp unless match
      end
    end

    # If there is no active rule, try to apply a new one
    if @active_rule.nil?
      ruleset.each do |rn, r|
        pattern = r['start_pattern']
        next unless line =~ pattern

        @active_rule = rn if r['end_pattern']
        match = line.match(pattern)
        @context = match.names.map(&:to_sym).zip(match.captures).to_h
        if r['fetch_line_at_index'] || r['fetch_first_line_not_matching']
          if r['fetch_line_at_index']
            fetched_line = @lines_memory.reverse[r['fetch_line_at_index']]
          else
            fetched_line = nil
            @lines_memory.reverse.each do |l|
              match = false
              r['fetch_first_line_not_matching'].each do |pat|
                next unless l =~ pat

                match = true
                break
              end
              next if match

              fetched_line = l
              break
            end
          end
          if fetched_line
            if r['fetched_line_pattern']
              match = fetched_line.match(r['fetched_line_pattern'])
              @context.merge!(match.names.map(&:to_sym).zip(match.captures).to_h)
            end
            if r['fetched_line_message'] != false
              message = if r['fetched_line_message']
                          inject(r['fetched_line_message'])
                        else
                          fetched_line.chomp
                        end
              message = "[#{header}] " + message
              UI.send(r['type'], message)
            end
          end
        end
        if r['start_message'] != false
          message = if r['start_message']
                      inject(r['start_message'])
                    else
                      line.chomp
                    end
          message = "[#{header}] " + message
          UI.send(r['type'], message)
        end
        break
      end
    end
  end

  if @active_phase
    apply_ruleset.call(@phases[@active_phase]['rules'], @active_phase)
    finish_phase if @phases[@active_phase]['phase_end_pattern'] && @phases[@active_phase]['phase_end_pattern'] =~ line
  end
  apply_ruleset.call(@generic_rules, 'GENERAL')
end