Class: RequestLogAnalyzer::Request
- Inherits:
-
Object
- Object
- RequestLogAnalyzer::Request
- Includes:
- Converters
- Defined in:
- lib/request_log_analyzer/request.rb
Overview
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.
Direct Known Subclasses
FileFormat::AmazonS3::Request, FileFormat::Apache::Request, FileFormat::Haproxy::Request, FileFormat::Merb::Request, FileFormat::Mysql::Request, FileFormat::Postgresql::Request, FileFormat::Rails3::Request, FileFormat::Rails::Request, FileFormat::W3c::Request
Defined Under Namespace
Modules: Converters
Instance Attribute Summary (collapse)
-
- (Object) attributes
readonly
Returns the value of attribute attributes.
-
- (Object) file_format
readonly
Returns the value of attribute file_format.
-
- (Object) lines
readonly
Returns the value of attribute lines.
Class Method Summary (collapse)
-
+ (Object) create(file_format, *hashes)
Creates a new request that was parsed from the log with the given FileFormat.
Instance Method Summary (collapse)
-
- (Object) <<(hash)
Adds another line to the request.
-
- (Object) add_line_hash(value_hash)
Adds another line to the request using a plain hash.
-
- (Object) add_parsed_line(parsed_line)
Adds another line to the request when it is parsed in the LogParser.
-
- (Boolean) completed?
Checks whether this request is completed.
-
- (Boolean) empty?
Returns true if this request does not yet contain any parsed lines.
-
- (Object) every(field)
Returns an array of all the “field” values that were captured for this request.
-
- (Object) first(field)
(also: #[])
Returns the value that was captured for the “field” of this request.
- - (Object) first_lineno
-
- (Boolean) has_line_type?(line_type)
(also: #=~)
Checks whether the given line type was parsed from the log file for this request.
-
- (Request) initialize(file_format, attributes = {})
constructor
Initializes a new Request object.
- - (Object) last_lineno
-
- (Object) timestamp
Returns the first timestamp encountered in a request.
-
- (Object) validate
This function is called before a Requests is yielded.
Methods included from Converters
#convert_decimal, #convert_duration, #convert_epoch, #convert_eval, #convert_float, #convert_int, #convert_integer, #convert_nillable_string, #convert_path, #convert_string, #convert_sym, #convert_symbol, #convert_timestamp, #convert_traffic, #convert_value, #sanitize_parameters
Constructor Details
- (Request) initialize(file_format, attributes = {})
Initializes a new Request object. It will apply the the provided FileFormat module to this instance.
96 97 98 99 100 |
# File 'lib/request_log_analyzer/request.rb', line 96 def initialize(file_format, attributes = {}) @lines = [] @attributes = attributes @file_format = file_format end |
Instance Attribute Details
- (Object) attributes (readonly)
Returns the value of attribute attributes
92 93 94 |
# File 'lib/request_log_analyzer/request.rb', line 92 def attributes @attributes end |
- (Object) file_format (readonly)
Returns the value of attribute file_format
92 93 94 |
# File 'lib/request_log_analyzer/request.rb', line 92 def file_format @file_format end |
- (Object) lines (readonly)
Returns the value of attribute lines
92 93 94 |
# File 'lib/request_log_analyzer/request.rb', line 92 def lines @lines end |
Class Method Details
+ (Object) create(file_format, *hashes)
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.
104 105 106 107 108 |
# File 'lib/request_log_analyzer/request.rb', line 104 def self.create(file_format, *hashes) request = self.new(file_format) hashes.flatten.each { |hash| request << hash } return request end |
Instance Method Details
- (Object) <<(hash)
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.
145 146 147 |
# File 'lib/request_log_analyzer/request.rb', line 145 def <<(hash) hash[:line_definition] ? add_parsed_line(hash) : add_line_hash(hash) end |
- (Object) add_line_hash(value_hash)
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.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/request_log_analyzer/request.rb', line 126 def add_line_hash(value_hash) @lines << value_hash if value_hash[:compound] value_hash.each do |key, value| if value_hash[:compound].include?(key) @attributes[key] = [] if @attributes[key].nil? @attributes[key] = [@attributes[key]] unless @attributes[key].is_a?(Array) @attributes[key] << value else @attributes[key] = value unless key == :compound || @attributes[key] end end else @attributes = value_hash.merge(@attributes) end end |
- (Object) add_parsed_line(parsed_line)
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.
114 115 116 117 118 119 120 121 |
# File 'lib/request_log_analyzer/request.rb', line 114 def add_parsed_line (parsed_line) value_hash = parsed_line[:line_definition].convert_captured_values(parsed_line[:captures], self) value_hash[:line_type] = parsed_line[:line_definition].name value_hash[:lineno] = parsed_line[:lineno] value_hash[:source] = parsed_line[:source] value_hash[:compound] = parsed_line[:line_definition].compound add_line_hash(value_hash) end |
- (Boolean) completed?
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.
180 181 182 183 184 185 186 187 188 |
# File 'lib/request_log_analyzer/request.rb', line 180 def completed? header_found, = false, false @lines.each do |line| line_def = file_format.line_definitions[line[:line_type]] header_found = true if line_def.header = true if line_def. end header_found && end |
- (Boolean) empty?
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
173 174 175 |
# File 'lib/request_log_analyzer/request.rb', line 173 def empty? @lines.length == 0 end |
- (Object) every(field)
Returns an array of all the “field” values that were captured for this request
167 168 169 |
# File 'lib/request_log_analyzer/request.rb', line 167 def every(field) @lines.inject([]) { |result, fields| result << fields[field] if fields.has_key?(field); result } end |
- (Object) first(field) Also known as: []
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
160 161 162 |
# File 'lib/request_log_analyzer/request.rb', line 160 def first(field) @attributes[field] end |
- (Object) first_lineno
199 200 201 |
# File 'lib/request_log_analyzer/request.rb', line 199 def first_lineno @lines.map { |line| line[:lineno] }.reject { |v| v.nil? }.min end |
- (Boolean) has_line_type?(line_type) Also known as: =~
Checks whether the given line type was parsed from the log file for this request
150 151 152 153 |
# File 'lib/request_log_analyzer/request.rb', line 150 def has_line_type?(line_type) return true if @lines.length == 1 && @lines[0][:line_type] == line_type.to_sym @lines.detect { |l| l[:line_type] == line_type.to_sym } end |
- (Object) last_lineno
203 204 205 |
# File 'lib/request_log_analyzer/request.rb', line 203 def last_lineno @lines.map { |line| line[:lineno] }.reject { |v| v.nil? }.max end |
- (Object) timestamp
Returns the first timestamp encountered in a request.
195 196 197 |
# File 'lib/request_log_analyzer/request.rb', line 195 def first(:timestamp) end |
- (Object) validate
This function is called before a Requests is yielded.
191 192 |
# File 'lib/request_log_analyzer/request.rb', line 191 def validate end |