Module: RequestLogAnalyzer::Request::Converters
- Included in:
- RequestLogAnalyzer::Request
- Defined in:
- lib/request_log_analyzer/request.rb
Instance Method Summary collapse
- #convert_decimal(value, capture_definition) ⇒ Object
-
#convert_duration(value, capture_definition) ⇒ Object
Convert duration fields to float, and make sure the values are in seconds.
-
#convert_eval(value, capture_definition) ⇒ Object
Converts :eval field, which should evaluate to a hash.
- #convert_float(value, capture_definition) ⇒ Object
- #convert_int(value, capture_definition) ⇒ Object
- #convert_integer(value, capture_definition) ⇒ Object
- #convert_string(value, capture_definition) ⇒ Object
- #convert_sym(value, capture_definition) ⇒ Object
- #convert_symbol(value, capture_definition) ⇒ Object
-
#convert_timestamp(value, capture_definition) ⇒ Object
Slow default method to parse timestamps.
-
#convert_traffic(value, capture_definition) ⇒ Object
Converts traffic fields to (whole) bytes based on the given unit.
-
#convert_value(value, capture_definition) ⇒ Object
Default converter function, which converts the parsed strings to a native Ruby type using the type indication in the line definition.
Instance Method Details
#convert_decimal(value, capture_definition) ⇒ Object
24 |
# File 'lib/request_log_analyzer/request.rb', line 24 def convert_decimal(value, capture_definition); value.to_f; end |
#convert_duration(value, capture_definition) ⇒ Object
Convert duration fields to float, and make sure the values are in seconds.
61 62 63 64 65 66 67 68 |
# File 'lib/request_log_analyzer/request.rb', line 61 def convert_duration(value, capture_definition) case capture_definition[:unit] when nil, :sec, :s then value.to_f when :microsec, :musec then value.to_f / 1000000.0 when :msec, :millisec then value.to_f / 1000.0 else raise "Unknown duration unit" end end |
#convert_eval(value, capture_definition) ⇒ Object
Converts :eval field, which should evaluate to a hash.
33 34 35 36 37 |
# File 'lib/request_log_analyzer/request.rb', line 33 def convert_eval(value, capture_definition) eval(value).inject({}) { |h, (k, v)| h[k.to_sym] = v; h} rescue SyntaxError nil end |
#convert_float(value, capture_definition) ⇒ Object
25 |
# File 'lib/request_log_analyzer/request.rb', line 25 def convert_float(value, capture_definition); value.to_f; end |
#convert_int(value, capture_definition) ⇒ Object
27 |
# File 'lib/request_log_analyzer/request.rb', line 27 def convert_int(value, capture_definition); value.to_i; end |
#convert_integer(value, capture_definition) ⇒ Object
28 |
# File 'lib/request_log_analyzer/request.rb', line 28 def convert_integer(value, capture_definition); value.to_i; end |
#convert_string(value, capture_definition) ⇒ Object
23 |
# File 'lib/request_log_analyzer/request.rb', line 23 def convert_string(value, capture_definition); value; end |
#convert_sym(value, capture_definition) ⇒ Object
29 |
# File 'lib/request_log_analyzer/request.rb', line 29 def convert_sym(value, capture_definition); value.to_sym; end |
#convert_symbol(value, capture_definition) ⇒ Object
30 |
# File 'lib/request_log_analyzer/request.rb', line 30 def convert_symbol(value, capture_definition); value.to_sym; end |
#convert_timestamp(value, capture_definition) ⇒ Object
Slow default method to parse timestamps. Reimplement this function in a file format specific Request class to improve the timestamp parsing speed.
42 43 44 |
# File 'lib/request_log_analyzer/request.rb', line 42 def (value, capture_definition) DateTime.parse(value).strftime('%Y%m%d%H%M%S').to_i end |
#convert_traffic(value, capture_definition) ⇒ Object
Converts traffic fields to (whole) bytes based on the given unit.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/request_log_analyzer/request.rb', line 47 def convert_traffic(value, capture_definition) case capture_definition[:unit] when nil, :b, :B, :byte then value.to_i when :GB, :G, :gigabyte then (value.to_f * 1000_000_000).round when :GiB, :gibibyte then (value.to_f * (2 ** 30)).round when :MB, :M, :megabyte then (value.to_f * 1000_000).round when :MiB, :mebibyte then (value.to_f * (2 ** 20)).round when :KB, :K, :kilobyte, :kB then (value.to_f * 1000).round when :KiB, :kibibyte then (value.to_f * (2 ** 10)).round else raise "Unknown traffic unit" end end |
#convert_value(value, capture_definition) ⇒ Object
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.
17 18 19 20 21 |
# File 'lib/request_log_analyzer/request.rb', line 17 def convert_value(value, capture_definition) return capture_definition[:default] if value.nil? custom_converter_method = :"convert_#{capture_definition[:type]}" send(custom_converter_method, value, capture_definition) end |