lib/redis-stat.rb in redis-stat-0.2.0 vs lib/redis-stat.rb in redis-stat-0.2.1

- old
+ new

@@ -10,33 +10,42 @@ class RedisStat DEFAULT_TERM_WIDTH = 180 DEFAULT_TERM_HEIGHT = 25 def initialize options = {} - @options = RedisStat::Option::DEFAULT.merge options - @hosts = @options[:hosts] + options = RedisStat::Option::DEFAULT.merge options + @hosts = options[:hosts] @redises = @hosts.map { |e| host, port = e.split(':') Redis.new(Hash[ {:host => host, :port => port}.select { |k, v| v } ]) } - @max_count = @options[:count] + @interval = options[:interval] + @max_count = options[:count] + @colors = options[:colors] || COLORS + @csv = options[:csv] + @auth = options[:auth] + @measures = MEASURES[ options[:verbose] ? :verbose : :default ] @count = 0 - @colors = @options[:colors] || COLORS end def start output_stream @os = output_stream trap('INT') { Thread.main.raise Interrupt } - csv = File.open(@options[:csv], 'w') if @options[:csv] - update_term_size! - - begin - # Warm-up - @redises.each { |r| r.info } + csv = File.open(@csv, 'w') if @csv + update_term_size! + # Warm-up / authenticate only when needed + @redises.each do |r| + begin + r.info + rescue Redis::CommandError + r.auth @auth if @auth + end + end + @started_at = Time.now prev_info = nil loop do info = {}.insensitive class << info @@ -58,11 +67,11 @@ output info, prev_info, csv prev_info = info @count += 1 break if @max_count && @count >= @max_count - sleep @options[:interval] + sleep @interval end @os.puts rescue Interrupt @os.puts @os.puts ansi(:yellow, :bold) { "Interrupted." } @@ -211,10 +220,10 @@ } @table.separator! end def process info, prev_info - MEASURES[@options[:verbose] ? :verbose : :default].map { |key| + @measures.map { |key| [ key, process_how(info, prev_info, key) ] }.select { |pair| pair.last } end def process_how info, prev_info, key