Class: RequestLogAnalyzer::Output::FixedWidth

Inherits:
Base
  • Object
show all
Defined in:
lib/request_log_analyzer/output/fixed_width.rb

Overview

Fixed Width output class. Outputs a fixed width ASCII or UF8 report.

Defined Under Namespace

Modules: Color, Monochrome

Constant Summary collapse

CHARACTERS =
{
  ascii: { horizontal_line: '-', vertical_line: '|', block: '=' },
  utf: { horizontal_line: '━', vertical_line: '┃', block: '░' }
}

Instance Attribute Summary collapse

Attributes inherited from Base

#io, #options, #style

Instance Method Summary collapse

Methods inherited from Base

#report_tracker, #slice_results, #with_style

Constructor Details

#initialize(io, options = {}) ⇒ FixedWidth

Initialize a report io iO Object (file, STDOUT, etc.) options

* <tt>:characters</tt> :utf for UTF8 or :ascii for ANSI compatible output. Defaults to :utf.
* <tt>:color</tt> If true, ASCII colorization is used, else Monochrome. Defaults to Monochrome.
* <tt>:width</tt> Output width in characters. Defaults to 80.


64
65
66
67
68
69
70
71
72
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 64

def initialize(io, options = {})
  super(io, options)
  @options[:width]      ||= 80
  @options[:characters] ||= :utf
  @characters = CHARACTERS[@options[:characters]]

  color_module = @options[:color] ? Color : Monochrome
  (class << self; self; end).send(:include, color_module)
end

Instance Attribute Details

#charactersObject (readonly)

Returns the value of attribute characters.



51
52
53
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 51

def characters
  @characters
end

Instance Method Details

Generate a footer for a report



123
124
125
126
127
128
129
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 123

def footer
  puts
  puts 'Need an expert to analyze your application?'
  puts "Mail to #{link('[email protected]')} or visit us at #{link('http://railsdoctors.com')}."
  line(:green)
  puts "Thanks for using #{colorize('request-log-analyzer', :white, :bold)}!"
end

#headerObject

Generate a header for a report



113
114
115
116
117
118
119
120
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 113

def header
  if io.is_a?(File)
    puts colorize('Request-log-analyzer summary report', :white, :bold)
    line(:green)
    puts "Version #{RequestLogAnalyzer::VERSION} - written by Willem van Bergen and Bart ten Brinke"
    puts "Website: #{link('http://github.com/wvanbergen/request-log-analyzer')}"
  end
end

#line(*font) ⇒ Object

Write a line



97
98
99
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 97

def line(*font)
  puts colorize(characters[:horizontal_line] * @options[:width], *font)
end

Write a link text The text in the link, or the URL itself if no text is given url The url to link to.



104
105
106
107
108
109
110
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 104

def link(text, url = nil)
  if url.nil?
    colorize(text, :red, :bold)
  else
    "#{text} (#{colorize(url, :blue, :bold)})"
  end
end

Write a string to the output object. str The string to write.



76
77
78
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 76

def print(str)
  @io << str
end

#puts(str = '') ⇒ Object

Write a string to the output object with a newline at the end. str The string to write.



84
85
86
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 84

def puts(str = '')
  @io << str << "\n"
end

#table(*columns) {|rows| ... } ⇒ Object

Generate a report table and push it into the output object. *colums<tt> Columns hash <tt>&block: A block yeilding the rows.

Yields:

  • (rows)


134
135
136
137
138
139
140
141
142
143
144
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 134

def table(*columns, &_block)
  rows = []
  yield(rows)

  # determine maximum cell widths
  max_cell_widths = rows.reduce(Array.new(columns.length, 0)) do |result, row|
    lengths = row.map { |column| column.to_s.length }
    result.each_with_index { |length, index| result[index] = ([length, lengths[index]].max rescue length) }
  end
  columns.each_with_index { |col, index| col[:actual_width] ||= max_cell_widths[index] }

  # determine actual column width
  column_widths = columns.map do |column|
    if column[:width] == :rest
      nil
    elsif column[:width]
      column[:width]
    elsif column[:min_width]
      [column[:min_width], column[:actual_width]].max
    elsif column[:max_width]
      [column[:max_width], column[:actual_width]].min
    else
      column[:actual_width]
    end
  end

  if column_widths.include?(nil)
    width_left = options[:width] - ((columns.length - 1) * (style[:cell_separator] ? 3 : 1)) - column_widths.compact.reduce(0) { |sum, col| sum + col }
    column_widths[column_widths.index(nil)] = width_left
  end

  line(:green) if @style[:top_line]

  # Print table header
  if table_has_header?(columns)
    column_titles = []
    columns.each_with_index do |column, index|
      width = column_widths[index]
      alignment = (column[:align] == :right ? '' : '-')
      column_titles.push(colorize("%#{alignment}#{width}s" % column[:title].to_s[0...width], :bold))
    end

    puts column_titles.join(style[:cell_separator] ? " #{characters[:vertical_line]} " : ' ')
    line(:green)
  end

  # Print the rows
  rows.each do |row|
    row_values = []
    columns.each_with_index do |column, index|
      width = column_widths[index]
      case column[:type]
      when :ratio
        if width > 4
          if column[:treshold] && column[:treshold] < row[index].to_f
            bar = ''
            bar << characters[:block] * (width.to_f * column[:treshold]).round
            bar << colorize(characters[:block] * (width.to_f * (row[index].to_f - column[:treshold])).round, :red)
            row_values.push(bar)
          else
            # Create a bar by combining block characters
            row_values.push(characters[:block] * (width.to_f * row[index].to_f).round)
          end
        else
          # Too few characters for a ratio bar. Display nothing
          row_values.push('')
        end
      else
        alignment = (columns[index][:align] == :right ? '' : '-')
        cell_value = "%#{alignment}#{width}s" % row[index].to_s[0...width]
        cell_value = colorize(cell_value, :bold, :brown) if columns[index][:highlight]
        row_values.push(cell_value)
      end
    end
    puts row_values.join(style[:cell_separator] ? " #{characters[:vertical_line]} " : ' ')
  end
end

#title(title) ⇒ Object

Write the title of a report title The title to write



90
91
92
93
94
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 90

def title(title)
  puts
  puts colorize(title, :bold, :white)
  line(:green)
end