Class: MMTop::TermPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/mmtop/term_printer.rb

Instance Method Summary collapse

Constructor Details

#initializeTermPrinter

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_screenObject



134
135
136
# File 'lib/mmtop/term_printer.rb', line 134

def clear_screen
  print "\033[H\033[2J"
end

#column_fill(index) ⇒ Object



57
58
59
# File 'lib/mmtop/term_printer.rb', line 57

def column_fill(index)
  fill * table_header_columns[index].size
end

#column_value(indexes, str, fill = " ", align = :left) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mmtop/term_printer.rb', line 74

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



65
66
67
68
69
70
71
72
# File 'lib/mmtop/term_printer.rb', line 65

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

#cornerObject



18
19
20
# File 'lib/mmtop/term_printer.rb', line 18

def corner
  "+".dark_gray
end

#edgeObject



22
23
24
# File 'lib/mmtop/term_printer.rb', line 22

def edge
  "-".dark_gray
end

#fillObject



38
39
40
# File 'lib/mmtop/term_printer.rb', line 38

def fill
  "-".dark_gray
end

#format_process(process, sz) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mmtop/term_printer.rb', line 122

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



102
103
104
105
106
107
108
109
# File 'lib/mmtop/term_printer.rb', line 102

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mmtop/term_printer.rb', line 86

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



111
112
113
114
115
116
117
118
119
120
# File 'lib/mmtop/term_printer.rb', line 111

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_dimObject



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_sepObject



34
35
36
# File 'lib/mmtop/term_printer.rb', line 34

def info_sep
  " | ".dark_gray
end

#pipeObject



26
27
28
# File 'lib/mmtop/term_printer.rb', line 26

def pipe
  "|".dark_gray
end


46
47
48
49
50
51
# File 'lib/mmtop/term_printer.rb', line 46

def print_footer
  footer_banner = (" mmtop version #{MMTop::VERSION} ".dark_gray) + "...".dark_gray + " press \"p\" to pause ".dark_gray
  left_border_size = (@x - 2 - footer_banner.size) / 2 
  right_border_size = @x - 2 - footer_banner.size - left_border_size
  puts corner + (edge * left_border_size) + footer_banner + (edge * right_border_size) + corner
end


42
43
44
# File 'lib/mmtop/term_printer.rb', line 42

def print_header
  puts corner + edge * (@x - 2) + corner
end


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/mmtop/term_printer.rb', line 148

def print_host(info)
  return if (info.processlist.empty? && info.host.hide_if_empty?) || info.host.hide

  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


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/mmtop/term_printer.rb', line 187

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
  print_footer
end


138
139
140
141
142
143
144
145
146
# File 'lib/mmtop/term_printer.rb', line 138

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


174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/mmtop/term_printer.rb', line 174

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

#sepObject



30
31
32
# File 'lib/mmtop/term_printer.rb', line 30

def sep
  " | ".dark_gray
end

#sep_fillObject



61
62
63
# File 'lib/mmtop/term_printer.rb', line 61

def sep_fill
  fill * sep.size
end

#table_header_columnsObject



53
54
55
# File 'lib/mmtop/term_printer.rb', line 53

def table_header_columns
  @header_columns ||= ["hostname        ", "pid  ", "time", "#cx", "slave  ", "delay", "qps   ", "comment " + info_sep + Time.now.to_s]
end