Class: RequestLogAnalyzer::FileFormat::Postgresql::Request

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

Instance Attribute Summary

Attributes inherited from Request

#attributes, #file_format, #lines

Instance Method Summary (collapse)

Methods inherited from Request

#<<, #add_line_hash, #add_parsed_line, #completed?, create, #empty?, #every, #first, #first_lineno, #has_line_type?, #initialize, #last_lineno, #timestamp, #validate

Methods included from Request::Converters

#convert_decimal, #convert_duration, #convert_epoch, #convert_eval, #convert_float, #convert_int, #convert_integer, #convert_nillable_string, #convert_path, #convert_string, #convert_sym, #convert_symbol, #convert_traffic, #convert_value, #sanitize_parameters

Constructor Details

This class inherits a constructor from RequestLogAnalyzer::Request

Instance Method Details

- (Object) convert_sql(value, definition)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/request_log_analyzer/file_format/postgresql.rb', line 39

def convert_sql(value, definition)
  
  # Recreate the full SQL query by joining all the previous parts and this last line
  sql = every(:query_fragment).join("\n") + value

  # Sanitize an SQL query so that it can be used as a category field.
  # sql.gsub!(/\/\*.*\*\//, '')                                       # remove comments
  sql.gsub!(/\s+/, ' ')                                             # remove excessive whitespace
  sql.gsub!(/"([^"]+)"/, '\1')                                      # remove quotes from field names
  sql.gsub!(/'\d{4}-\d{2}-\d{2}'/, ':date')                         # replace dates
  sql.gsub!(/'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'/, ':datetime')   # replace timestamps
  sql.gsub!(/'[^']*'/, ':string')                                   # replace strings
  sql.gsub!(/\b\d+\b/, ':int')                                      # replace integers
  sql.gsub!(/(:int,)+:int/, ':ints')                                # replace multiple ints by a list
  sql.gsub!(/(:string,)+:string/, ':strings')                       # replace multiple strings by a list

  return sql.lstrip.rstrip
end

- (Object) convert_timestamp(value, definition)

Convert the timestamp to an integer



63
64
65
66
# File 'lib/request_log_analyzer/file_format/postgresql.rb', line 63

def convert_timestamp(value, definition)
  _, y, m, d, h, i, s = value.split(/(\d\d)-(\d\d)-(\d\d)\s+(\d?\d):(\d\d):(\d\d)/)
  ('20%s%s%s%s%s%s' % [y,m,d,h.rjust(2, '0'),i,s]).to_i
end

- (Object) host



58
59
60
# File 'lib/request_log_analyzer/file_format/postgresql.rb', line 58

def host
  self[:host] == '' || self[:host].nil? ? self[:ip] : self[:host]
end