Module: RequestLogAnalyzer::FileFormat
- Defined in:
- lib/request_log_analyzer/file_format.rb,
lib/request_log_analyzer/file_format/w3c.rb,
lib/request_log_analyzer/file_format/merb.rb,
lib/request_log_analyzer/file_format/rack.rb,
lib/request_log_analyzer/file_format/mysql.rb,
lib/request_log_analyzer/file_format/nginx.rb,
lib/request_log_analyzer/file_format/rails.rb,
lib/request_log_analyzer/file_format/apache.rb,
lib/request_log_analyzer/file_format/rails3.rb,
lib/request_log_analyzer/file_format/haproxy.rb,
lib/request_log_analyzer/file_format/amazon_s3.rb,
lib/request_log_analyzer/file_format/postgresql.rb,
lib/request_log_analyzer/file_format/delayed_job.rb,
lib/request_log_analyzer/file_format/delayed_job2.rb,
lib/request_log_analyzer/file_format/delayed_job3.rb,
lib/request_log_analyzer/file_format/delayed_job4.rb,
lib/request_log_analyzer/file_format/delayed_job21.rb,
lib/request_log_analyzer/file_format/rails_development.rb
Defined Under Namespace
Modules: CommonRegularExpressions Classes: AmazonS3, Apache, Base, DelayedJob, DelayedJob2, DelayedJob21, DelayedJob3, DelayedJob4, Haproxy, Merb, Mysql, Nginx, Oink, Postgresql, Rack, Rails, Rails3, RailsDevelopment, W3c
Class Method Summary collapse
-
.all_formats ⇒ Object
Returns an array of all FileFormat instances that are shipped with request-log-analyzer by default.
-
.autodetect(file, line_count = 50) ⇒ Object
Autodetects the filetype of a given file.
-
.autodetect_score(parser) ⇒ Object
Calculates a file format auto detection score based on the parser statistics.
-
.load(file_format, *args) ⇒ Object
Loads a FileFormat::Base subclass instance.
Class Method Details
.all_formats ⇒ Object
Returns an array of all FileFormat instances that are shipped with request-log-analyzer by default.
65 66 67 68 69 |
# File 'lib/request_log_analyzer/file_format.rb', line 65 def self.all_formats @all_formats ||= Dir[File.('file_format/*.rb', File.dirname(__FILE__))].map do |file| load(File.basename(file, '.rb')) end end |
.autodetect(file, line_count = 50) ⇒ Object
Autodetects the filetype of a given file.
Returns a FileFormat instance, by parsing the first couple of lines of the provided file with avery known file format and return the most promosing file format based on the parser statistics. The autodetect_score method is used to score the fitness of a format.
file-
The file to detect the file format for.
line_count-
The number of lines to take into consideration
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/request_log_analyzer/file_format.rb', line 79 def self.autodetect(file, line_count = 50) parsers = all_formats.map { |f| RequestLogAnalyzer::Source::LogParser.new(f, parse_strategy: 'cautious') } File.open(file, 'rb') do |io| while io.lineno < line_count && (line = io.gets) parsers.each { |parser| parser.parse_line(line) } end end parsers.select { |p| autodetect_score(p) > 0 }.max { |a, b| autodetect_score(a) <=> autodetect_score(b) }.file_format rescue nil end |
.autodetect_score(parser) ⇒ Object
Calculates a file format auto detection score based on the parser statistics.
This method returns a score as an integer. Usually, the score will increase as more lines are parsed. Usually, a file_format with a score of zero or lower should not be considered.
parser-
The parsed that was use to parse the initial lines of the log file.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/request_log_analyzer/file_format.rb', line 98 def self.autodetect_score(parser) score = 0 score -= parser.file_format.line_definitions.length score -= parser.warnings * 3 score += parser.parsed_lines * 1 score += parser.parsed_requests * 10 # As Apache matches several simular formats, subtracting 1 will make a specific matcher have a higher score score -= 1 if parser.file_format.class == RequestLogAnalyzer::FileFormat::Apache score end |
.load(file_format, *args) ⇒ Object
Loads a FileFormat::Base subclass instance. You can provide:
-
A FileFormat instance (which will return itself)
-
A FileFormat class (of which an imstance will be returned)
-
A filename (from which the FileFormat class is loaded)
-
A symbol of a built-in file format (e.g. :rails)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/request_log_analyzer/file_format.rb', line 29 def self.load(file_format, *args) klass = nil if file_format.is_a?(RequestLogAnalyzer::FileFormat::Base) # this already is a file format! return itself return @current_file_format = file_format elsif file_format.is_a?(Class) && file_format.ancestors.include?(RequestLogAnalyzer::FileFormat::Base) # a usable class is provided. Use this format class. klass = file_format elsif file_format.is_a?(String) && File.exist?(file_format) && File.file?(file_format) # load a format from a ruby file require File.(file_format) const = RequestLogAnalyzer.to_camelcase(File.basename(file_format, '.rb')) if RequestLogAnalyzer::FileFormat.const_defined?(const) klass = RequestLogAnalyzer::FileFormat.const_get(const) elsif Object.const_defined?(const) klass = Object.const_get(const) else fail "Cannot load class #{const} from #{file_format}!" end else # load a provided file format klass = RequestLogAnalyzer::FileFormat.const_get(RequestLogAnalyzer.to_camelcase(file_format)) end # check the returned klass to see if it can be used fail "Could not load a file format from #{file_format.inspect}" if klass.nil? fail "Invalid FileFormat class from #{file_format.inspect}" unless klass.is_a?(Class) && klass.ancestors.include?(RequestLogAnalyzer::FileFormat::Base) @current_file_format = klass.create(*args) # return an instance of the class end |