class RequestLogAnalyzer::Request
The Request
class represents a parsed request from the log file. Instances are created by the LogParser and are passed to the different aggregators, so they can do their aggregating work.
This class provides several methods to access the data that was parsed from the log files. Request#first(field_name)
returns the first (only) value corresponding to the given field Request#every(field_name)
returns all values corresponding to the given field name as array.
Attributes
Public Class Methods
Creates a new request that was parsed from the log with the given FileFormat
. The hashes that are passed to this function are added as lines to this request.
# File lib/request_log_analyzer/request.rb 123 def self.create(file_format, *hashes) 124 request = new(file_format) 125 hashes.flatten.each { |hash| request << hash } 126 request 127 end
Initializes a new Request
object. It will apply the the provided FileFormat
module to this instance.
# File lib/request_log_analyzer/request.rb 115 def initialize(file_format, attributes = {}) 116 @lines = [] 117 @attributes = attributes 118 @file_format = file_format 119 end
Public Instance Methods
Adds another line to the request. This method switches automatically between the add_line_hash
and add_parsed_line
based on the keys of the provided hash.
# File lib/request_log_analyzer/request.rb 164 def <<(hash) 165 hash[:line_definition] ? add_parsed_line(hash) : add_line_hash(hash) 166 end
Adds another line to the request using a plain hash.
The line should be provides as a hash of the fields parsed from the line.
# File lib/request_log_analyzer/request.rb 145 def add_line_hash(value_hash) 146 @lines << value_hash 147 if value_hash[:compound] 148 value_hash.each do |key, value| 149 if value_hash[:compound].include?(key) 150 @attributes[key] = [] if @attributes[key].nil? 151 @attributes[key] = [@attributes[key]] unless @attributes[key].is_a?(Array) 152 @attributes[key] << value 153 else 154 @attributes[key] = value unless key == :compound || @attributes[key] 155 end 156 end 157 else 158 @attributes = value_hash.merge(@attributes) 159 end 160 end
Adds another line to the request when it is parsed in the LogParser.
The line should be provided as a hash with the attributes line_definition, :captures, :lineno and :source set. This function is called from LogParser.
# File lib/request_log_analyzer/request.rb 133 def add_parsed_line(parsed_line) 134 value_hash = parsed_line[:line_definition].convert_captured_values(parsed_line[:captures], self) 135 value_hash[:line_type] = parsed_line[:line_definition].name 136 value_hash[:lineno] = parsed_line[:lineno] 137 value_hash[:source] = parsed_line[:source] 138 value_hash[:compound] = parsed_line[:line_definition].compound 139 add_line_hash(value_hash) 140 end
Checks whether this request is completed. A completed request contains both a parsed header line and a parsed footer line. Not that calling this function in single line mode will always return false.
# File lib/request_log_analyzer/request.rb 199 def completed? 200 header_found, footer_found = false, false 201 @lines.each do |line| 202 line_def = file_format.line_definitions[line[:line_type]] 203 header_found = true if line_def.header 204 footer_found = true if line_def.footer 205 end 206 header_found && footer_found 207 end
Returns true if this request does not yet contain any parsed lines. This should only occur during parsing. An empty request should never be sent to the aggregators
# File lib/request_log_analyzer/request.rb 192 def empty? 193 @lines.length == 0 194 end
Returns an array of all the “field” values that were captured for this request
# File lib/request_log_analyzer/request.rb 186 def every(field) 187 @lines.reduce([]) { |result, fields| result << fields[field] if fields.key?(field); result } 188 end
Returns the value that was captured for the “field” of this request. This function will return the first value that was captured if the field was captured in multiple lines
# File lib/request_log_analyzer/request.rb 179 def first(field) 180 @attributes[field] 181 end
# File lib/request_log_analyzer/request.rb 218 def first_lineno 219 @lines.map { |line| line[:lineno] }.reject { |v| v.nil? }.min 220 end
Checks whether the given line type was parsed from the log file for this request
# File lib/request_log_analyzer/request.rb 169 def has_line_type?(line_type) 170 return true if @lines.length == 1 && @lines[0][:line_type] == line_type.to_sym 171 @lines.find { |l| l[:line_type] == line_type.to_sym } 172 end
# File lib/request_log_analyzer/request.rb 222 def last_lineno 223 @lines.map { |line| line[:lineno] }.reject { |v| v.nil? }.max 224 end
Returns the first timestamp encountered in a request.
# File lib/request_log_analyzer/request.rb 214 def timestamp 215 first(:timestamp) 216 end
This function is called before a Requests is yielded.
# File lib/request_log_analyzer/request.rb 210 def validate 211 end