Class: RequestLogAnalyzer::Source::DatabaseLoader

Inherits:
Base
  • Object
show all
Defined in:
lib/request_log_analyzer/source/database_loader.rb

Overview

The Database class gets log data from the database.

Instance Attribute Summary collapse

Attributes inherited from Base

#current_request, #options, #parsed_lines, #parsed_requests, #skipped_lines, #skipped_requests

Instance Method Summary collapse

Methods inherited from Base

#finalize, #prepare

Constructor Details

#initialize(format, options = {}) ⇒ DatabaseLoader

Initializes the log file parser instance. It will apply the language specific FileFormat module to this instance. It will use the line definitions in this module to parse any input that it is given (see parse_io).

format

The current file format instance

options

A hash of options that are used by the parser



37
38
39
40
41
42
# File 'lib/request_log_analyzer/source/database_loader.rb', line 37

def initialize(format, options = {})
  super(format, options)
  @source_files     = options[:source_files]
  @parsed_requests  = 0
  @requests         = []
end

Instance Attribute Details

#file_formatObject (readonly)

Returns the value of attribute file_format.



29
30
31
# File 'lib/request_log_analyzer/source/database_loader.rb', line 29

def file_format
  @file_format
end

#requestsObject (readonly)

Returns the value of attribute requests.



29
30
31
# File 'lib/request_log_analyzer/source/database_loader.rb', line 29

def requests
  @requests
end

#source_filesObject (readonly)

Returns the value of attribute source_files.



29
30
31
# File 'lib/request_log_analyzer/source/database_loader.rb', line 29

def source_files
  @source_files
end

Instance Method Details

#each_request(options = {}, &block) ⇒ Object

Reads the input, which can either be a file, sequence of files or STDIN to parse lines specified in the FileFormat. This lines will be combined into Request instances, that will be yielded. The actual parsing occurs in the parse_io method.

options

A Hash of options that will be pased to parse_io.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/request_log_analyzer/source/database_loader.rb', line 48

def each_request(options = {}, &block) # :yields: request
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => @source_files)

  @progress_handler.call(:started, @source_files) if @progress_handler
    RequestLogAnalyzer::Source::Request.find(:all).each do |request|
      @parsed_requests += 1
      @progress_handler.call(:progress, @parsed_requests) if @progress_handler

      yield request.convert(self.file_format)
    end

  @progress_handler.call(:finished, @source_files) if @progress_handler
end

#progress=(proc) ⇒ Object

Add a block to this method to install a progress handler while parsing.

proc

The proc that will be called to handle progress update messages



64
65
66
# File 'lib/request_log_analyzer/source/database_loader.rb', line 64

def progress=(proc)
  @progress_handler = proc
end

#warn(type, message) ⇒ Object

This method is called by the parser if it encounteres any parsing problems. It will call the installed warning handler if any.

By default, RequestLogAnalyzer::Controller will install a warning handler that will pass the warnings to each aggregator so they can do something useful with it.

type

The warning type (a Symbol)

message

A message explaining the warning



83
84
85
# File 'lib/request_log_analyzer/source/database_loader.rb', line 83

def warn(type, message)
  @warning_handler.call(type, message, @current_io.lineno) if @warning_handler
end

#warning=(proc) ⇒ Object

Add a block to this method to install a warning handler while parsing,

proc

The proc that will be called to handle parse warning messages



70
71
72
# File 'lib/request_log_analyzer/source/database_loader.rb', line 70

def warning=(proc)
  @warning_handler = proc
end