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
188 189 190 191 192 193 194 |
# File 'lib/readis/monitor.rb', line 188 def colorize(name, string) if [:colorized] [Term::ANSIColor.send(name), string, Term::ANSIColor.reset].join else string end end |
#filtered?(command) ⇒ Boolean
180 181 182 183 184 185 186 |
# File 'lib/readis/monitor.rb', line 180 def filtered?(command) if list = [:includes] !list.include?(command) elsif list = [: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 |
# 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 *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, timestamp)} #{out.join('')}" end |
#format_argument(string) ⇒ Object
150 151 152 |
# File 'lib/readis/monitor.rb', line 150 def format_argument(string) colorize(:magenta, string + " ") end |
#format_channel(string) ⇒ Object
142 143 144 |
# File 'lib/readis/monitor.rb', line 142 def format_channel(string) colorize(:cyan, string + " ") end |
#format_command(string) ⇒ Object
Wraps the command in the appropriate ANSI color codes
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/readis/monitor.rb', line 130 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
154 155 156 |
# File 'lib/readis/monitor.rb', line 154 def format_field(string) colorize(:blue, string + " ") end |
#format_key(string) ⇒ Object
146 147 148 |
# File 'lib/readis/monitor.rb', line 146 def format_key(string) colorize(:cyan, string + " ") end |
#format_message(string) ⇒ Object
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/readis/monitor.rb', line 158 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
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/readis/monitor.rb', line 169 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 ||= 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. = "\nUsage: readis monitor [options]\n\n BANNER\n optparser.on(\"-c\", \"--color\", \"enable syntax coloring\") do |color|\n options[:colorized] = true\n end\n optparser.on(\"-i\", \"--include COMMANDS\",\n \"comma separated list of Redis commands to include\") do |includes|\n options[:includes] = includes.split(\",\").map {|c| c.upcase}\n end\n optparser.on(\"-e\", \"--exclude COMMANDS\",\n \"comma separated list of Redis commands to exclude\") do |excludes|\n options[:excludes] = excludes.split(\",\").map {|c| c.upcase}\n end\n # TODO: options for\n # * compact vs. spacey\n # * timestamp on/off\n optparser\nend\n" |
#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 |