module TcpsnitchAnalyzer

Constants

EXECUTABLE
VERSION

Public Class Methods

error(msg) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/tcpsnitch_analyzer.rb, line 85
def keys_from_path(path)
  path.split('.').collect(&:to_sym)
end
node_val(hash, path) click to toggle source
# File lib/tcpsnitch_analyzer.rb, line 89
def node_val(hash, path)
  val_for(hash, keys_from_path(path))
end
process_files(options, files) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/tcpsnitch_analyzer.rb, line 81
def val_for(hash, keys)
    keys.reduce(hash) { |h, key| h[key] }
end