Class: MMTop::TermPrinter
- Inherits:
-
Object
- Object
- MMTop::TermPrinter
- Defined in:
- lib/mmtop/term_printer.rb
Instance Method Summary collapse
- #clear_screen ⇒ Object
- #column_fill(index) ⇒ Object
- #column_value(indexes, str, fill = " ", align = :left) ⇒ Object
- #column_value_with_fill(index, max_size, str, fill, align) ⇒ Object
- #corner ⇒ Object
- #edge ⇒ Object
- #fill ⇒ Object
- #format_process(process, sz) ⇒ Object
- #format_slave_delay(status) ⇒ Object
- #format_slave_status(status) ⇒ Object
- #format_time(x) ⇒ Object
- #get_dim ⇒ Object
- #info_sep ⇒ Object
-
#initialize ⇒ TermPrinter
constructor
A new instance of TermPrinter.
- #pipe ⇒ Object
- #print_footer ⇒ Object
- #print_header ⇒ Object
- #print_host(info) ⇒ Object
- #print_info(host_infos) ⇒ Object
- #print_process(p) ⇒ Object
- #print_table_header ⇒ Object
- #reset! ⇒ Object
- #sep ⇒ Object
- #sep_fill ⇒ Object
- #table_header_columns ⇒ Object
Constructor Details
#initialize ⇒ TermPrinter
Returns a new instance of TermPrinter.
5 6 7 |
# File 'lib/mmtop/term_printer.rb', line 5 def initialize $stdout.sync = true end |
Instance Method Details
#clear_screen ⇒ Object
131 132 133 |
# File 'lib/mmtop/term_printer.rb', line 131 def clear_screen print "\033[H\033[2J" end |
#column_fill(index) ⇒ Object
54 55 56 |
# File 'lib/mmtop/term_printer.rb', line 54 def column_fill(index) fill * table_header_columns[index].size end |
#column_value(indexes, str, fill = " ", align = :left) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mmtop/term_printer.rb', line 71 def column_value(indexes, str, fill=" ", align=:left) indexes = Array(indexes) max_size = indexes.inject(0) { |s, idx| s + table_header_columns[idx].size } max_size += sep_fill.size * (indexes.size - 1) if str.size < max_size column_value_with_fill(indexes.first, max_size, str, fill, align) else str[0..max_size - 1] end end |
#column_value_with_fill(index, max_size, str, fill, align) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/mmtop/term_printer.rb', line 62 def column_value_with_fill(index, max_size, str, fill, align) fill_str = fill * (max_size - str.size) if align == :left str + fill_str else fill_str + str end end |
#corner ⇒ Object
18 19 20 |
# File 'lib/mmtop/term_printer.rb', line 18 def corner "+".dark_gray end |
#edge ⇒ Object
22 23 24 |
# File 'lib/mmtop/term_printer.rb', line 22 def edge "-".dark_gray end |
#fill ⇒ Object
38 39 40 |
# File 'lib/mmtop/term_printer.rb', line 38 def fill "-".dark_gray end |
#format_process(process, sz) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mmtop/term_printer.rb', line 119 def format_process(process, sz) query = process.sql ? process.sql[0..sz-2] : '' case process.time when 0..2 query when 2..10 query.white.bold else query.red end end |
#format_slave_delay(status) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/mmtop/term_printer.rb', line 99 def format_slave_delay(status) return "" unless status if status[:Seconds_Behind_Master].nil? "N/A" else format_time(status[:Seconds_Behind_Master]) end end |
#format_slave_status(status) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mmtop/term_printer.rb', line 83 def format_slave_status(status) if status.nil? || status.keys.empty? "" else if status[:Slave_IO_Running] == 'Yes' && status[:Slave_SQL_Running] == 'Yes' "OK" elsif status[:Slave_IO_Running] == 'No' && status[:Slave_SQL_Running] == 'No' "STOPPED" elsif status[:Slave_IO_Running] == 'Yes' "!SQL" else "!IO" end end end |
#format_time(x) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/mmtop/term_printer.rb', line 108 def format_time(x) case x when 0..100 x.to_s when 100..3599 (x / 60).to_s + "m" else (x / 3600).to_s + "h" end end |
#get_dim ⇒ Object
14 15 16 |
# File 'lib/mmtop/term_printer.rb', line 14 def get_dim @y, @x = %x{stty size}.split.collect { |x| x.to_i } end |
#info_sep ⇒ Object
34 35 36 |
# File 'lib/mmtop/term_printer.rb', line 34 def info_sep " | ".dark_gray end |
#pipe ⇒ Object
26 27 28 |
# File 'lib/mmtop/term_printer.rb', line 26 def pipe "|".dark_gray end |
#print_footer ⇒ Object
46 47 48 |
# File 'lib/mmtop/term_printer.rb', line 46 def puts corner + edge * (@x - 2) + corner end |
#print_header ⇒ Object
42 43 44 |
# File 'lib/mmtop/term_printer.rb', line 42 def print_header puts corner + edge * (@x - 2) + corner end |
#print_host(info) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/mmtop/term_printer.rb', line 145 def print_host(info) return if info.processlist.empty? && info.host.hide_if_empty? display_name = info.host.display_name if info.host.dead? display_name = (display_name + "!").red end str = pipe + " " + column_value([0, 1, 2], display_name + " ", "-".dark_gray) str += info_sep + column_value(3, info.connections.size.to_s) str += info_sep + column_value(4, format_slave_status(info.slave_status)) str += info_sep + column_value(5, format_slave_delay(info.slave_status)) str += info_sep + column_value(6, info.stats[:qps].to_s) str += info_sep + column_value(7, info.host.comment || '') str += info_sep fill_count = (@x - str.size - 1) str += "-".dark_gray * fill_count if fill_count > 0 str += pipe puts str info.processlist.each do |p| print_process p end end |
#print_info(host_infos) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/mmtop/term_printer.rb', line 184 def print_info(host_infos) #max_comment_size = host_infos.map { |i| (i.host.comment && i.host.comment.size).to_i }.max #comment_index = 7 #table_header_columns[comment_index] += (' ' * (max_comment_size - table_header_columns[comment_index].size)) if max_comment_size > table_header_columns[comment_index].size clear_screen reset! print_header print_table_header print_header host_infos.each do |info| print_host(info) end end |
#print_process(p) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/mmtop/term_printer.rb', line 135 def print_process(p) str = pipe + " " + column_value(0, p.client, ' ', :right) str += info_sep + column_value(1, p.id ? p.id.to_s : '') str += info_sep + column_value(2, format_time(p.time)) str += info_sep str += format_process(p, @x - str.size - 1) str += " " * (@x - str.size - 1) + pipe puts str end |
#print_table_header ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/mmtop/term_printer.rb', line 171 def print_table_header if table_header_columns.join(sep).size + 4 > @x table_header_columns[-1] = '' end str = pipe + " " + table_header_columns.join(sep) fill_len = (@x - str.size) - 1 print str print ' ' * fill_len if fill_len > 0 puts pipe end |
#reset! ⇒ Object
9 10 11 12 |
# File 'lib/mmtop/term_printer.rb', line 9 def reset! @header_columns = nil get_dim end |
#sep ⇒ Object
30 31 32 |
# File 'lib/mmtop/term_printer.rb', line 30 def sep " | ".dark_gray end |
#sep_fill ⇒ Object
58 59 60 |
# File 'lib/mmtop/term_printer.rb', line 58 def sep_fill fill * sep.size end |
#table_header_columns ⇒ Object
50 51 52 |
# File 'lib/mmtop/term_printer.rb', line 50 def table_header_columns @header_columns ||= ["hostname ", "pid ", "time", "#cx", "slave ", "delay", "qps ", "comment " + info_sep + Time.now.to_s] end |