Class: Readis::Monitor
Instance Method Summary collapse
- #colorize(name, string) ⇒ Object
- #filtered?(command) ⇒ Boolean
- #format(line) ⇒ Object
- #format_argument(string) ⇒ Object
- #format_channel(string) ⇒ Object
-
#format_command(string) ⇒ Object
Wraps the command in the appropriate ANSI color codes.
- #format_field(string) ⇒ Object
- #format_key(string) ⇒ Object
- #format_message(string) ⇒ Object
- #format_value(string) ⇒ Object
-
#initialize(*args) ⇒ Monitor
constructor
A new instance of Monitor.
- #options ⇒ Object
- #parser ⇒ Object
- #run ⇒ Object
Methods inherited from Readis
Constructor Details
#initialize(*args) ⇒ Monitor
Returns a new instance of Monitor.
4 5 6 7 8 9 |
# File 'lib/readis/monitor.rb', line 4 def initialize(*args) super if [:includes] && [:excludes] raise ArgumentError, "Define either --includes or --excludes, but not both." end end |
Instance Method Details
#colorize(name, string) ⇒ Object
197 198 199 200 201 202 203 |
# File 'lib/readis/monitor.rb', line 197 def colorize(name, string) if @options[:colorized] [Term::ANSIColor.send(name), string, Term::ANSIColor.reset].join else string end end |
#filtered?(command) ⇒ Boolean
189 190 191 192 193 194 195 |
# File 'lib/readis/monitor.rb', line 189 def filtered?(command) if list = @options[:includes] !list.include?(command) elsif list = @options[:excludes] list.include?(command) end end |
#format(line) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/readis/monitor.rb', line 49 def format(line) if = line.slice!(/^[\d.]+ /) .strip! end if client = line.slice!(/\[\d+ [\d:\.]+\] "/) client = client.slice(3..-4) end parts = line.split('" "').map {|p| p.chomp('"') } command = parts[0].upcase return if filtered?(command) out = [ format_command(command) ] = %w(PUBLISH) one_key_one_value = %w(APPEND GETSET LPUSHX RPUSHX SET SETNX) one_key_one_argument = %w(DECRBY EXPIRE EXPIREAT GETBIT INCRBY LINDEX MOVE RENAME RENAMENX) one_key_one_field = %w(HEXISTS HGET) one_key_one_field_one_value = %w(HSET HSETNX) # TODO: add the rest of the redis commands case command when * # one channel, one message out << format_channel(parts[1]) out << (parts[2]) when *one_key_one_value # one key, one value out << format_key(parts[1]) out << format_value(parts[2]) when *one_key_one_argument # one key, one arg out << format_key(parts[1]) out << format_argument(parts[2]) when "BLPOP" # variable number of keys, last part is an arg parts.slice(1..-2).each do |part| out << format_key(part) end out << format_argument(parts[-1]) when "LPUSH", "RPUSH", "ZREM" # one key, rest are values out << format_key(parts[1]) parts.slice(2..-1).each do |part| out << format_value(part) end when "ZRANGE", "ZRANGEBYSCORE", "ZREMRANGEBYSCORE", "ZREMRANGEBYRANK", "ZCOUNT", "ZREVRANK", "ZREVRANGE" # one key, rest are arguments out << format_key(parts[1]) parts.slice(2..-1).each do |part| out << format_argument(part) end when "HMSET" # key, field, value, [field, value, ...] out << format_key(parts[1]) out << "\n" + format_field(parts[2]) out << format_value(parts[3]) parts.slice(4..-1).each_slice(2) do |a| out << "\n" + format_field(a[0]) out << format_value(a[1]) end when *one_key_one_field_one_value # key, field, value out << format_key(parts[1]) out << format_field(parts[2]) out << format_value(parts[3]) when *one_key_one_field # key, field out << format_key(parts[1]) out << format_field(parts[2]) when "ZADD", "ZINCRBY" # ZADD score member [score] [member] out << format_key(parts[1]) parts.slice(2..-1).each_slice(2) do |pair| out << format_argument(pair.first) out << format_value(pair.last) end else # all keys if rest = parts.slice(1..-1) parts.slice(1..-1).each do |part| out << format_key(part) end end end "#{colorize(:underscore, )} #{out.join('')}" end |
#format_argument(string) ⇒ Object
159 160 161 |
# File 'lib/readis/monitor.rb', line 159 def format_argument(string) colorize(:magenta, string + " ") end |
#format_channel(string) ⇒ Object
151 152 153 |
# File 'lib/readis/monitor.rb', line 151 def format_channel(string) colorize(:cyan, string + " ") end |
#format_command(string) ⇒ Object
Wraps the command in the appropriate ANSI color codes
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/readis/monitor.rb', line 139 def format_command(string) case string when "MULTI", "EXEC", "WATCH" colorize(:red, string + " ") when /SUBSCRIBE/, "PUBLISH" colorize(:yellow, string + " ") else colorize(:green, string + " ") end end |
#format_field(string) ⇒ Object
163 164 165 |
# File 'lib/readis/monitor.rb', line 163 def format_field(string) colorize(:blue, string + " ") end |
#format_key(string) ⇒ Object
155 156 157 |
# File 'lib/readis/monitor.rb', line 155 def format_key(string) colorize(:cyan, string + " ") end |
#format_message(string) ⇒ Object
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/readis/monitor.rb', line 167 def (string) begin string.gsub!(/\\/, "") object = JSON.parse(string) out = "\n" + JSON.pretty_generate(object) rescue out = string + " " end out end |
#format_value(string) ⇒ Object
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/readis/monitor.rb', line 178 def format_value(string) begin string.gsub!(/\\/, "") object = JSON.parse(string) out = "\n" + JSON.pretty_generate(object) rescue out = string + " " end out end |
#options ⇒ Object
19 20 21 22 23 |
# File 'lib/readis/monitor.rb', line 19 def @options ||= super.merge( :colorized => false ) end |
#parser ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/readis/monitor.rb', line 25 def parser optparser = super optparser. = <<-BANNER Usage: readis monitor [options] BANNER optparser.on("-c", "--color", "enable syntax coloring") do |color| [:colorized] = true end optparser.on("-i", "--include COMMANDS", "comma separated list of Redis commands to include") do |includes| [:includes] = includes.split(",").map {|c| c.upcase} end optparser.on("-e", "--exclude COMMANDS", "comma separated list of Redis commands to exclude") do |excludes| [:excludes] = excludes.split(",").map {|c| c.upcase} end # TODO: options for # * compact vs. spacey # * timestamp on/off optparser end |
#run ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/readis/monitor.rb', line 11 def run @redis.monitor do |line| if formatted = format(line) puts formatted end end end |