class RequestLogAnalyzer::Database::Base

Public Class Methods

column_type(type_indicator) click to toggle source

Function to determine the column type for a field TODO: make more robust / include in file-format definition

    # File lib/request_log_analyzer/database/base.rb
102 def self.column_type(type_indicator)
103   case type_indicator
104   when :eval then      :text
105   when :hash then      :text
106   when :text then      :text
107   when :string then    :string
108   when :sec then       :float
109   when :msec then      :float
110   when :duration then  :float
111   when :float then     :float
112   when :double then    :float
113   when :integer then   :integer
114   when :int then       :int
115   when :timestamp then :datetime
116   when :datetime then  :datetime
117   when :date then      :date
118   else             :string
119   end
120 end
create_table!() click to toggle source
   # File lib/request_log_analyzer/database/base.rb
72 def self.create_table!
73   fail 'No line_definition available to base table schema on!' unless line_definition
74 
75   unless table_exists?
76     database.connection.create_table(table_name.to_sym) do |t|
77 
78       # Default fields
79       t.column :request_id, :integer
80       t.column :source_id,  :integer
81       t.column :lineno,     :integer
82 
83       line_definition.captures.each do |capture|
84         column_name = capture[:name]
85         column_name = 'file_format' if column_name == 'format'
86         # Add a field for every capture
87         t.column(column_name, column_type(capture[:type]))
88 
89         # If the capture provides other field as well, create columns for them, too
90         capture[:provides].each { |field, field_type| t.column(field, column_type(field_type)) } if capture[:provides].is_a?(Hash)
91       end
92     end
93 
94     # Add indices to table for more speedy querying
95     database.connection.add_index(table_name.to_sym, [:request_id]) # rescue
96     database.connection.add_index(table_name.to_sym, [:source_id])  # rescue
97   end
98 end
drop_table!() click to toggle source
   # File lib/request_log_analyzer/database/base.rb
66 def self.drop_table!
67   database.connection.remove_index(table_name, [:source_id])  rescue nil
68   database.connection.remove_index(table_name, [:request_id]) rescue nil
69   database.connection.drop_table(table_name) if database.connection.table_exists?(table_name)
70 end
subclass_from_line_definition(definition, klass = Class.new(RequestLogAnalyzer::Database::Base)) click to toggle source
   # File lib/request_log_analyzer/database/base.rb
28 def self.subclass_from_line_definition(definition, klass = Class.new(RequestLogAnalyzer::Database::Base))
29   klass.table_name = "#{definition.name}_lines"
30 
31   klass.line_definition = definition
32 
33   # Set relations with requests and sources table
34   klass.belongs_to :request, class_name: RequestLogAnalyzer::Database::Request.name
35   klass.belongs_to :source, class_name: RequestLogAnalyzer::Database::Source.name
36 
37   # Serialize complex fields into the database
38   definition.captures.select { |c| c.key?(:provides) }.each do |capture|
39     klass.send(:serialize, capture[:name], Hash)
40   end
41 
42   RequestLogAnalyzer::Database::Request.has_many "#{definition.name}_lines".to_sym
43   RequestLogAnalyzer::Database::Source.has_many "#{definition.name}_lines".to_sym
44 
45   klass
46 end
subclass_from_table(table, klass = Class.new(RequestLogAnalyzer::Database::Base)) click to toggle source
   # File lib/request_log_analyzer/database/base.rb
48 def self.subclass_from_table(table, klass = Class.new(RequestLogAnalyzer::Database::Base))
49   fail "Table #{table} not found!" unless database.connection.table_exists?(table)
50 
51   klass.table_name = table
52 
53   if klass.column_names.include?('request_id')
54     klass.belongs_to :request, class_name: RequestLogAnalyzer::Database::Request.name
55     RequestLogAnalyzer::Database::Request.has_many table.to_sym
56   end
57 
58   if klass.column_names.include?('source_id')
59     klass.belongs_to :source, class_name: RequestLogAnalyzer::Database::Source.name
60     RequestLogAnalyzer::Database::Source.has_many table.to_sym
61   end
62 
63   klass
64 end

Public Instance Methods

<=>(other) click to toggle source
   # File lib/request_log_analyzer/database/base.rb
 4 def <=>(other)
 5   if (source_id.nil? && other.source_id.nil?) || (source_comparison = source_id <=> other.source_id) == 0
 6     lineno <=> other.lineno
 7   else
 8     source_comparison
 9   end
10 end
format(_arg) click to toggle source
   # File lib/request_log_analyzer/database/base.rb
17 def format(_arg)
18   attributes[:format]
19 end
format=(arg) click to toggle source

Handle format manually, because it is prohibidado in Rails 3.2.1

   # File lib/request_log_analyzer/database/base.rb
13 def format=(arg)
14   attributes[:format] = arg
15 end
line_type() click to toggle source
   # File lib/request_log_analyzer/database/base.rb
21 def line_type
22   self.class.name.underscore.gsub(/_line$/, '').to_sym
23 end