Class: RequestLogAnalyzer::FileFormat::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/request_log_analyzer/file_format.rb

Overview

Base class for all log file format definitions. This class provides functions for subclasses to define their LineDefinitions and to define a summary report.

A subclass of this class is instantiated when request-log-analyzer is started and this instance is shared with all components of the application so they can act on the specifics of the format

Constant Summary

Request =

Setup the default Request class.

::RequestLogAnalyzer::Request

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_definitions = OrderedHash.new, report_trackers = []) ⇒ Base



251
252
253
# File 'lib/request_log_analyzer/file_format.rb', line 251

def initialize(line_definitions = OrderedHash.new, report_trackers = [])
  @line_definitions, @report_trackers = line_definitions, report_trackers
end

Instance Attribute Details

#line_definitionsObject (readonly)

Returns the value of attribute line_definitions



203
204
205
# File 'lib/request_log_analyzer/file_format.rb', line 203

def line_definitions
  @line_definitions
end

#report_trackersObject (readonly)

Returns the value of attribute report_trackers



203
204
205
# File 'lib/request_log_analyzer/file_format.rb', line 203

def report_trackers
  @report_trackers
end

Class Method Details

.create(*args) ⇒ Object

Instantiation



246
247
248
249
# File 'lib/request_log_analyzer/file_format.rb', line 246

def self.create(*args)
  # Ignore arguments
  return self.new(line_definer.line_definitions, report_definer.trackers)
end

.format_definition(&block) ⇒ Object

Specifies multiple line definitions at once using a block



215
216
217
218
219
220
221
# File 'lib/request_log_analyzer/file_format.rb', line 215

def self.format_definition(&block)
  if block_given?
    yield self.line_definer
  else
    return self.line_definer
  end
end

.line_definerObject

Setup the default line definer.



230
231
232
# File 'lib/request_log_analyzer/file_format.rb', line 230

def self.line_definer
  @line_definer ||= ::RequestLogAnalyzer::LineDefinition::Definer.new
end

.line_definition(name, &block) ⇒ Object

Specifies a single line defintions.



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

def self.line_definition(name, &block)
  line_definer.define_line(name, &block)
end

.report(mode = :append) {|self.report_definer| ... } ⇒ Object

Specifies the summary report using a block.

Yields:



224
225
226
227
# File 'lib/request_log_analyzer/file_format.rb', line 224

def self.report(mode = :append, &block)
  self.report_definer.reset! if mode == :overwrite
  yield(self.report_definer)
end

.report_definerObject

Setup the default report definer.



235
236
237
# File 'lib/request_log_analyzer/file_format.rb', line 235

def self.report_definer
  @report_definer ||= ::RequestLogAnalyzer::Aggregator::Summarizer::Definer.new
end

Instance Method Details

#captures?(name) ⇒ Boolean

Returns true if this language captures the given symbol in one of its line definitions



289
290
291
# File 'lib/request_log_analyzer/file_format.rb', line 289

def captures?(name)
  line_definitions.any? { |(_, ld)| ld.captures?(name) }
end

#line_dividerObject



313
314
315
# File 'lib/request_log_analyzer/file_format.rb', line 313

def line_divider
  self.class.const_get(LINE_DIVIDER) if self.class.const_defined?(:LINE_DIVIDER)
end

#max_line_lengthObject

Returns the max line length for this file format if any.



309
310
311
# File 'lib/request_log_analyzer/file_format.rb', line 309

def max_line_length
  self.class.const_get(MAX_LINE_LENGTH) if self.class.const_defined?(:MAX_LINE_LENGTH)
end

#parse_line(line, &warning_handler) ⇒ Object

Parses a line by trying to parse it using every line definition in this file format



299
300
301
302
303
304
305
306
# File 'lib/request_log_analyzer/file_format.rb', line 299

def parse_line(line, &warning_handler)
  self.line_definitions.each do |lt, definition|
    match = definition.matches(line, &warning_handler)
    return match if match
  end

  return nil
end

#request(*hashes) ⇒ Object

Returns a Request instance with the given parsed lines that should be provided as hashes.



265
266
267
# File 'lib/request_log_analyzer/file_format.rb', line 265

def request(*hashes)
  request_class.create(self, *hashes)
end

#request_classObject

Returns the Request class of this file format



260
261
262
# File 'lib/request_log_analyzer/file_format.rb', line 260

def request_class
  self.class::Request
end

#setup_environment(controller) ⇒ Object

Function that a file format con implement to monkey patch the environment.

  • controller The environment is provided as a controller instance



295
296
# File 'lib/request_log_analyzer/file_format.rb', line 295

def setup_environment(controller)
end

#valid_line_definitions?Boolean

Checks whether the line definitions form a valid language. A file format should have at least a header and a footer line type



279
280
281
# File 'lib/request_log_analyzer/file_format.rb', line 279

def valid_line_definitions?
  line_definitions.any? { |(_, ld)| ld.header } && line_definitions.any? { |(_, ld)| ld.footer }
end

#valid_request_class?Boolean

Checks whether the request class inherits from the base Request class.



284
285
286
# File 'lib/request_log_analyzer/file_format.rb', line 284

def valid_request_class?
  request_class.ancestors.include?(RequestLogAnalyzer::Request)
end

#well_formed?Boolean Also known as: valid?

Checks whether the file format is valid so it can be safely used with RLA.



270
271
272
# File 'lib/request_log_analyzer/file_format.rb', line 270

def well_formed?
  valid_line_definitions? && valid_request_class?
end