Class: RequestLogAnalyzer::Request

Inherits:
Object
  • Object
show all
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.

Defined Under Namespace

Modules: Converters

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#initialize(file_format, attributes = {}) ⇒ Request

Initializes a new Request object. It will apply the the provided FileFormat module to this instance.



115
116
117
118
119
# File 'lib/request_log_analyzer/request.rb', line 115

def initialize(file_format, attributes = {})
  @lines       = []
  @attributes  = attributes
  @file_format = file_format
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



111
112
113
# File 'lib/request_log_analyzer/request.rb', line 111

def attributes
  @attributes
end

#file_formatObject (readonly)

Returns the value of attribute file_format.



111
112
113
# File 'lib/request_log_analyzer/request.rb', line 111

def file_format
  @file_format
end

#linesObject (readonly)

Returns the value of attribute lines.



111
112
113
# File 'lib/request_log_analyzer/request.rb', line 111

def lines
  @lines
end

Class Method Details

.create(file_format, *hashes) ⇒ Object

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.



123
124
125
126
127
# File 'lib/request_log_analyzer/request.rb', line 123

def self.create(file_format, *hashes)
  request = new(file_format)
  hashes.flatten.each { |hash| request << hash }
  request
end

Instance Method Details

#<<(hash) ⇒ Object

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.



164
165
166
# File 'lib/request_log_analyzer/request.rb', line 164

def <<(hash)
  hash[:line_definition] ? add_parsed_line(hash) : add_line_hash(hash)
end

#add_line_hash(value_hash) ⇒ Object

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.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/request_log_analyzer/request.rb', line 145

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

#add_parsed_line(parsed_line) ⇒ Object

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.



133
134
135
136
137
138
139
140
# File 'lib/request_log_analyzer/request.rb', line 133

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

#completed?Boolean

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.

Returns:

  • (Boolean)


199
200
201
202
203
204
205
206
207
# File 'lib/request_log_analyzer/request.rb', line 199

def completed?
  header_found, footer_found = false, false
  @lines.each do |line|
    line_def = file_format.line_definitions[line[:line_type]]
    header_found = true if line_def.header
    footer_found = true if line_def.footer
  end
  header_found && footer_found
end

#empty?Boolean

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

Returns:

  • (Boolean)


192
193
194
# File 'lib/request_log_analyzer/request.rb', line 192

def empty?
  @lines.length == 0
end

#every(field) ⇒ Object

Returns an array of all the “field” values that were captured for this request



186
187
188
# File 'lib/request_log_analyzer/request.rb', line 186

def every(field)
  @lines.reduce([]) { |result, fields| result << fields[field] if fields.key?(field); result }
end

#first(field) ⇒ Object 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



179
180
181
# File 'lib/request_log_analyzer/request.rb', line 179

def first(field)
  @attributes[field]
end

#first_linenoObject



218
219
220
# File 'lib/request_log_analyzer/request.rb', line 218

def first_lineno
  @lines.map { |line| line[:lineno] }.reject { |v| v.nil? }.min
end

#has_line_type?(line_type) ⇒ Boolean Also known as: =~

Checks whether the given line type was parsed from the log file for this request

Returns:

  • (Boolean)


169
170
171
172
# File 'lib/request_log_analyzer/request.rb', line 169

def has_line_type?(line_type)
  return true if @lines.length == 1 && @lines[0][:line_type] == line_type.to_sym
  @lines.find { |l| l[:line_type] == line_type.to_sym }
end

#last_linenoObject



222
223
224
# File 'lib/request_log_analyzer/request.rb', line 222

def last_lineno
  @lines.map { |line| line[:lineno] }.reject { |v| v.nil? }.max
end

#timestampObject

Returns the first timestamp encountered in a request.



214
215
216
# File 'lib/request_log_analyzer/request.rb', line 214

def timestamp
  first(:timestamp)
end

#validateObject

This function is called before a Requests is yielded.



210
211
# File 'lib/request_log_analyzer/request.rb', line 210

def validate
end