module RequestLogAnalyzer::Request::Converters
Public Instance Methods
# File lib/request_log_analyzer/request.rb 28 def convert_decimal(value, _capture_definition) 29 value.to_f 30 end
Convert duration fields to float, and make sure the values are in seconds.
# File lib/request_log_analyzer/request.rb 93 def convert_duration(value, capture_definition) 94 case capture_definition[:unit] 95 when nil, :sec, :s then value.to_f 96 when :microsec, :musec then value.to_f / 1_000_000.0 97 when :msec, :millisec then value.to_f / 1000.0 98 else fail 'Unknown duration unit' 99 end 100 end
Convert an epoch to an integer
# File lib/request_log_analyzer/request.rb 103 def convert_epoch(value, _capture_definition) 104 Time.at(value.to_i).strftime('%Y%m%d%H%M%S').to_i 105 end
Converts :eval field, which should evaluate to a hash.
# File lib/request_log_analyzer/request.rb 59 def convert_eval(value, _capture_definition) 60 eval(sanitize_parameters(value)).reduce({}) { |h, (k, v)| h[k.to_sym] = v; h } 61 rescue StandardError, SyntaxError 62 nil 63 end
# File lib/request_log_analyzer/request.rb 24 def convert_float(value, _capture_definition) 25 value.to_f 26 end
# File lib/request_log_analyzer/request.rb 32 def convert_int(value, _capture_definition) 33 value.to_i 34 end
# File lib/request_log_analyzer/request.rb 36 def convert_integer(value, _capture_definition) 37 value.to_i 38 end
# File lib/request_log_analyzer/request.rb 48 def convert_nillable_string(value, _definition) 49 value == '-' ? nil : value 50 end
This function can be overridden to rewrite the path for better categorization in the reports.
# File lib/request_log_analyzer/request.rb 54 def convert_path(value, _definition) 55 value 56 end
# File lib/request_log_analyzer/request.rb 20 def convert_string(value, _capture_definition) 21 value 22 end
# File lib/request_log_analyzer/request.rb 40 def convert_sym(value, _capture_definition) 41 value.to_sym 42 end
# File lib/request_log_analyzer/request.rb 44 def convert_symbol(value, _capture_definition) 45 value.to_sym 46 end
Slow default method to parse timestamps. Reimplement this function in a file format specific Request
class to improve the timestamp parsing speed.
# File lib/request_log_analyzer/request.rb 74 def convert_timestamp(value, _capture_definition) 75 DateTime.parse(value).strftime('%Y%m%d%H%M%S').to_i 76 end
Converts traffic fields to (whole) bytes based on the given unit.
# File lib/request_log_analyzer/request.rb 79 def convert_traffic(value, capture_definition) 80 case capture_definition[:unit] 81 when nil, :b, :B, :byte then value.to_i 82 when :GB, :G, :gigabyte then (value.to_f * 1_000_000_000).round 83 when :GiB, :gibibyte then (value.to_f * (2**30)).round 84 when :MB, :M, :megabyte then (value.to_f * 1_000_000).round 85 when :MiB, :mebibyte then (value.to_f * (2**20)).round 86 when :KB, :K, :kilobyte, :kB then (value.to_f * 1000).round 87 when :KiB, :kibibyte then (value.to_f * (2**10)).round 88 else fail 'Unknown traffic unit' 89 end 90 end
Default converter function, which converts the parsed strings to a native Ruby type using the type indication in the line definition. It will use a custom connverter method if one is available.
# File lib/request_log_analyzer/request.rb 14 def convert_value(value, capture_definition) 15 return capture_definition[:default] if value.nil? 16 custom_converter_method = :"convert_#{capture_definition[:type]}" 17 send(custom_converter_method, value, capture_definition) 18 end
Removes certain string sequences which would be problematic for eval. TODO remove all characters not valid in ruby symbols
# File lib/request_log_analyzer/request.rb 67 def sanitize_parameters(parameter_string) 68 parameter_string.gsub(/#</, '"').gsub(/>,/, '", ').gsub(/\\0/, '') 69 end