Class: Base::RecordInserter

Inherits:
Object
  • Object
show all
Defined in:
lib/base/record_inserter.rb

Overview

Set of functions that can be used to easily log requests into a SQLite3 Database.

Direct Known Subclasses

RailsAnalyzer::RecordInserter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_file, options = {}) ⇒ RecordInserter

Initializer db_file The file which will be used for the SQLite3 Database storage.



15
16
17
18
19
20
21
22
# File 'lib/base/record_inserter.rb', line 15

def initialize(db_file, options = {})
  @database = SQLite3::Database.new(db_file)
  @insert_statements = nil
  @warning_count = 0
  create_tables_if_needed!

  self.initialize_hook(options) if self.respond_to?(:initialize_hook)
end

Instance Attribute Details

#current_requestObject (readonly)

Returns the value of attribute current_request.



10
11
12
# File 'lib/base/record_inserter.rb', line 10

def current_request
  @current_request
end

#databaseObject (readonly)

Returns the value of attribute database.



9
10
11
# File 'lib/base/record_inserter.rb', line 9

def database
  @database
end

#warning_countObject (readonly)

Returns the value of attribute warning_count.



11
12
13
# File 'lib/base/record_inserter.rb', line 11

def warning_count
  @warning_count
end

Class Method Details

.insert_batch_into(db_file, options = {}, &block) ⇒ Object

Insert a batch of files into the database. db_file The filename of the database file to use. Returns the created database.



56
57
58
59
60
# File 'lib/base/record_inserter.rb', line 56

def self.insert_batch_into(db_file, options = {}, &block)
  db = self.new(db_file)
  db.insert_batch(&block)
  return db
end

Instance Method Details

#calculate_db_durations!Object

Calculate the database durations of the requests currenty in the database. Used if a logfile does contain any database durations.



26
27
28
# File 'lib/base/record_inserter.rb', line 26

def calculate_db_durations!
  @database.execute('UPDATE "completed_queries" SET "database" = "duration" - "rendering" WHERE "database" IS NULL OR "database" = 0.0')
end

#count(type) ⇒ Object



62
63
64
# File 'lib/base/record_inserter.rb', line 62

def count(type)
  @database.get_first_value("SELECT COUNT(*) FROM \"#{type}_requests\"").to_i
end

#insert_batch(&block) ⇒ Object

Insert a batch of loglines into the database. Function prepares insert statements, yeilds and then closes and commits.



32
33
34
35
36
37
38
39
40
41
# File 'lib/base/record_inserter.rb', line 32

def insert_batch(&block)
  @database.transaction
  prepare_statements!
  block.call(self)
  close_prepared_statements!
  @database.commit
rescue Exception => e
  puts e.message
  @database.rollback
end

#insert_warning(line, warning) ⇒ Object



43
44
45
46
# File 'lib/base/record_inserter.rb', line 43

def insert_warning(line, warning)
  @database.execute("INSERT INTO parse_warnings (line, warning) VALUES (:line, :warning)", :line => line, :warning => warning)
  @warning_count += 1
end