Module: TcpsnitchAnalyzer

Defined in:
lib/tcpsnitch_analyzer.rb,
lib/tcpsnitch_analyzer/opt_parser.rb,
lib/tcpsnitch_analyzer/proportion_stat.rb,
lib/tcpsnitch_analyzer/time_serie_stat.rb,
lib/tcpsnitch_analyzer/descriptive_stat.rb

Defined Under Namespace

Classes: DescriptiveStat, OptParser, ProportionStat, TimeSerieStat

Constant Summary collapse

EXECUTABLE =
'tcpsnitch_analyzer'
VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.error(msg) ⇒ Object



67
68
69
70
71
# File 'lib/tcpsnitch_analyzer.rb', line 67

def error(msg)
  puts "#{EXECUTABLE}: #{msg}."
  puts "Try '#{EXECUTABLE} -h' for more information."
  exit 1
end

.filter(hash, filter) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/tcpsnitch_analyzer.rb', line 73

def filter(hash, filter)
  if filter then
    hash[:type] == filter ? hash : nil
  else
    hash
  end
end

.keys_from_path(path) ⇒ Object



85
86
87
# File 'lib/tcpsnitch_analyzer.rb', line 85

def keys_from_path(path)
  path.split('.').collect(&:to_sym)
end

.node_val(hash, path) ⇒ Object



89
90
91
# File 'lib/tcpsnitch_analyzer.rb', line 89

def node_val(hash, path)
  val_for(hash, keys_from_path(path))
end

.process_files(options, files) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
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
63
64
65
# File 'lib/tcpsnitch_analyzer.rb', line 16

def process_files(options, files)
  # We DO NOT want to read the entire JSON files into memory.
  # We DO NOT want to build a Ruby object for the entire JSON array.
  #
  # Instead, we want to the file line by line, where each line consists of 
  # a single event. We then instantiate each event individually and discard 
  # them as we consume the file, thus giving O(1) memory consumption.
  matched_data = false

  files.each do |file|
    # IO.each should not read entire file in memory. To verify?
    File.open(file).each_with_index do |line, index|
      next if index == 0    # First line is opening '[' of JSON Array
      next if line.eql? "]" # Last line is closing ']' of JSON Array

      # Parse JSON object
      begin 
        hash = Oj.load(line.chomp(",\n")) # Remove ',\n' after JSON object
      rescue Exception => e 
        error(e)
      end
     
      # Skip if filter does not match
      next unless filter(hash, options.event_filter)  
      matched_data = true
      
      # Extract value
      begin
        val = node_val(hash, options.node_path)
      rescue Exception => e
        error("invalid -n argument: '#{options.node_path}'")
      end

      # Compute on value
      if options.analysis_type == TimeSerieStat
        options.analysis_type.add_point(node_val(hash, 'timestamp'), val)
      else
        options.analysis_type.add_val(val)
      end
    end
  end

  # Output results
  puts_options_header(options)
  if matched_data 
    options.analysis_type.print(options)
  else
    puts "No data point matching criterias."
  end
end

.puts_options_header(options) ⇒ Object



93
94
95
96
97
98
# File 'lib/tcpsnitch_analyzer.rb', line 93

def puts_options_header(options)
  puts "JSON node:".ljust(15) + "#{options.node_path}"
  event_filter = options.event_filter ? options.event_filter : "/" 
  puts "Type filter:".ljust(15) + event_filter
  puts ""
end

.val_for(hash, keys) ⇒ Object



81
82
83
# File 'lib/tcpsnitch_analyzer.rb', line 81

def val_for(hash, keys)
    keys.reduce(hash) { |h, key| h[key] }
end