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

Direct Known Subclasses

AmazonS3, Apache, Merb, Rails

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



116
117
118
# File 'lib/request_log_analyzer/file_format.rb', line 116

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

Instance Attribute Details

#line_definitionsObject (readonly)

Returns the value of attribute line_definitions.



54
55
56
# File 'lib/request_log_analyzer/file_format.rb', line 54

def line_definitions
  @line_definitions
end

#report_trackersObject (readonly)

Returns the value of attribute report_trackers.



54
55
56
# File 'lib/request_log_analyzer/file_format.rb', line 54

def report_trackers
  @report_trackers
end

Class Method Details

.create(*args) ⇒ Object

Instantiation



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

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



93
94
95
96
97
98
99
# File 'lib/request_log_analyzer/file_format.rb', line 93

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

.inherited(subclass) ⇒ Object

Registers the line definer instance for a subclass.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/request_log_analyzer/file_format.rb', line 61

def self.inherited(subclass)
  if subclass.superclass == RequestLogAnalyzer::FileFormat::Base

    # Create aline and report definer for this class
    subclass.class_eval do 
      instance_variable_set(:@line_definer, RequestLogAnalyzer::LineDefinition::Definer.new)
      instance_variable_set(:@report_definer, RequestLogAnalyzer::Aggregator::Summarizer::Definer.new)
      class << self; attr_accessor :line_definer, :report_definer; end
    end        

    # Create a custom Request class for this file format
    subclass.const_set('Request', Class.new(RequestLogAnalyzer::Request)) unless subclass.const_defined?('Request')
  else

    # Copy the line and report definer from the parent class.
    subclass.class_eval do 
      instance_variable_set(:@line_definer, superclass.line_definer.clone)
      instance_variable_set(:@report_definer, superclass.report_definer.clone)
      class << self; attr_accessor :line_definer, :report_definer; end
    end

    # Create a custom Request class based on the superclass's Request class
    subclass.const_set('Request', Class.new(subclass.superclass::Request)) unless subclass.const_defined?('Request')
  end
end

.line_definition(name, &block) ⇒ Object

Specifies a single line defintions.



88
89
90
# File 'lib/request_log_analyzer/file_format.rb', line 88

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

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

Specifies the summary report using a block.

Yields:

  • (self.report_definer)


102
103
104
105
# File 'lib/request_log_analyzer/file_format.rb', line 102

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

Instance Method Details

#captures?(name) ⇒ Boolean

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

Returns:

  • (Boolean)


141
142
143
# File 'lib/request_log_analyzer/file_format.rb', line 141

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

#parse_line(line, &warning_handler) ⇒ Object

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



151
152
153
154
155
156
157
158
# File 'lib/request_log_analyzer/file_format.rb', line 151

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.



130
131
132
# File 'lib/request_log_analyzer/file_format.rb', line 130

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

#request_classObject

Returns the Request class of this file format



125
126
127
# File 'lib/request_log_analyzer/file_format.rb', line 125

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



147
148
# File 'lib/request_log_analyzer/file_format.rb', line 147

def setup_environment(controller)
end

#valid?Boolean

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

Returns:

  • (Boolean)


136
137
138
# File 'lib/request_log_analyzer/file_format.rb', line 136

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