Module: RequestLogAnalyzer::Request::Converters

Included in:
RequestLogAnalyzer::Request
Defined in:
lib/request_log_analyzer/request.rb

Instance Method Summary collapse

Instance Method Details

#convert_decimal(value, _capture_definition) ⇒ Object



28
29
30
# File 'lib/request_log_analyzer/request.rb', line 28

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.



93
94
95
96
97
98
99
100
# File 'lib/request_log_analyzer/request.rb', line 93

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 / 1_000_000.0
  when :msec, :millisec  then value.to_f / 1000.0
  else fail 'Unknown duration unit'
  end
end

#convert_epoch(value, _capture_definition) ⇒ Object

Convert an epoch to an integer



103
104
105
# File 'lib/request_log_analyzer/request.rb', line 103

def convert_epoch(value, _capture_definition)
  Time.at(value.to_i).strftime('%Y%m%d%H%M%S').to_i
end

#convert_eval(value, _capture_definition) ⇒ Object

Converts :eval field, which should evaluate to a hash.



59
60
61
62
63
# File 'lib/request_log_analyzer/request.rb', line 59

def convert_eval(value, _capture_definition)
  eval(sanitize_parameters(value)).reduce({}) { |h, (k, v)| h[k.to_sym] = v; h }
rescue StandardError, SyntaxError
  nil
end

#convert_float(value, _capture_definition) ⇒ Object



24
25
26
# File 'lib/request_log_analyzer/request.rb', line 24

def convert_float(value, _capture_definition)
  value.to_f
end

#convert_int(value, _capture_definition) ⇒ Object



32
33
34
# File 'lib/request_log_analyzer/request.rb', line 32

def convert_int(value, _capture_definition)
  value.to_i
end

#convert_integer(value, _capture_definition) ⇒ Object



36
37
38
# File 'lib/request_log_analyzer/request.rb', line 36

def convert_integer(value, _capture_definition)
  value.to_i
end

#convert_nillable_string(value, _definition) ⇒ Object



48
49
50
# File 'lib/request_log_analyzer/request.rb', line 48

def convert_nillable_string(value, _definition)
  value == '-' ? nil : value
end

#convert_path(value, _definition) ⇒ Object

This function can be overridden to rewrite the path for better categorization in the reports.



54
55
56
# File 'lib/request_log_analyzer/request.rb', line 54

def convert_path(value, _definition)
  value
end

#convert_string(value, _capture_definition) ⇒ Object



20
21
22
# File 'lib/request_log_analyzer/request.rb', line 20

def convert_string(value, _capture_definition)
  value
end

#convert_sym(value, _capture_definition) ⇒ Object



40
41
42
# File 'lib/request_log_analyzer/request.rb', line 40

def convert_sym(value, _capture_definition)
  value.to_sym
end

#convert_symbol(value, _capture_definition) ⇒ Object



44
45
46
# File 'lib/request_log_analyzer/request.rb', line 44

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.



74
75
76
# File 'lib/request_log_analyzer/request.rb', line 74

def convert_timestamp(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.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/request_log_analyzer/request.rb', line 79

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 * 1_000_000_000).round
  when :GiB, :gibibyte         then (value.to_f * (2**30)).round
  when :MB, :M, :megabyte      then (value.to_f * 1_000_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 fail '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.



14
15
16
17
18
# File 'lib/request_log_analyzer/request.rb', line 14

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

#sanitize_parameters(parameter_string) ⇒ Object

Removes certain string sequences which would be problematic for eval. TODO remove all characters not valid in ruby symbols



67
68
69
# File 'lib/request_log_analyzer/request.rb', line 67

def sanitize_parameters(parameter_string)
  parameter_string.gsub(/#</, '"').gsub(/>,/, '", ').gsub(/\\0/, '')
end