class Daikon::Monitor

Constants

ALL_COMMANDS
NEW_FORMAT
NO_ARG_COMMANDS
OLD_MORE_FORMAT
OLD_SINGLE_FORMAT
OTHER_COMMANDS
READ_COMMANDS
WRITE_COMMANDS

Attributes

data[RW]

Public Class Methods

new(redis = nil, logger = nil) click to toggle source
# File lib/daikon/monitor.rb, line 15
def initialize(redis = nil, logger = nil)
  @data   = data_hash
  @redis  = redis
  @logger = logger
  @mutex  = Mutex.new
end

Public Instance Methods

data_hash() click to toggle source
# File lib/daikon/monitor.rb, line 22
def data_hash
  {"commands"   => Hash.new(0),
   "keys"       => Hash.new(0),
   "namespaces" => Hash.new(0),
   "totals"     => Hash.new(0)}
end
incr_command(command) click to toggle source
# File lib/daikon/monitor.rb, line 92
def incr_command(command)
  data["commands"][command] += 1
end
incr_global_namespace() click to toggle source
# File lib/daikon/monitor.rb, line 88
def incr_global_namespace
  data["namespaces"]["global"] += 1
end
incr_key(key) click to toggle source
# File lib/daikon/monitor.rb, line 96
def incr_key(key)
  data["keys"][key] += 1
end
incr_namespace(key) click to toggle source
# File lib/daikon/monitor.rb, line 80
def incr_namespace(key)
  if marker = key =~ /:|-/
    data["namespaces"][key[0...marker]] += 1
  else
    incr_global_namespace
  end
end
incr_total(command) click to toggle source
# File lib/daikon/monitor.rb, line 100
def incr_total(command)
  data["totals"]["all"] += 1

  if READ_COMMANDS.member?(command)
    data["totals"]["read"] += 1
  elsif WRITE_COMMANDS.member?(command)
    data["totals"]["write"] += 1
  elsif OTHER_COMMANDS.member?(command)
    data["totals"]["other"] += 1
  end
end
lock(&block) click to toggle source
# File lib/daikon/monitor.rb, line 37
def lock(&block)
  @mutex.synchronize(&block)
end
parse(line) click to toggle source
# File lib/daikon/monitor.rb, line 51
def parse(line)
  if line =~ NEW_FORMAT
    push($1.split('" "'))
  elsif line =~ OLD_SINGLE_FORMAT || line =~ OLD_MORE_FORMAT
    push(line.split)
  end
end
push(split_command) click to toggle source
# File lib/daikon/monitor.rb, line 59
def push(split_command)
  command, key, *rest = split_command
  command.upcase!

  return unless ALL_COMMANDS.member?(command)

  lock do
    incr_command(command)
    incr_total(command)
    if key
      key.gsub!(".", "{PERIOD}") if key.include?('.')
      key.gsub!("$", "{DOLLAR}") if key.include?('$')

      incr_key(key)
      incr_namespace(key)
    else
      incr_global_namespace
    end
  end
end
rotate() click to toggle source
# File lib/daikon/monitor.rb, line 41
def rotate
  this_data = nil
  lock do
    this_data = self.data.dup
    self.data.replace(data_hash)
  end
  this_data["keys"] = Hash[*this_data["keys"].sort { |a, b| a.last <=> b.last }.reverse[0..99].flatten]
  this_data
end
start() click to toggle source
# File lib/daikon/monitor.rb, line 29
def start
  Thread.new do
    @redis.monitor do |line|
      parse(line)
    end
  end
end