Class: LogAnalyzer::Analyzer
- Inherits:
-
Object
- Object
- LogAnalyzer::Analyzer
- Defined in:
- lib/log_analyzer/analyzer.rb
Constant Summary collapse
- DEFAULT_TABLE_WIDTH =
width
120- MATCHER =
/Rendered (.*) \((.*)ms\)/.freeze
- DANGER_DEFAULT =
ms
800- WARNING_DEFAULT =
ms
400- INFO_DEFAULT =
ms
100
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
- #avg_label(avg) ⇒ Object
-
#initialize(filename:) ⇒ Analyzer
constructor
A new instance of Analyzer.
- #order(by: :time) ⇒ Object
- #run ⇒ Object
- #visualize(limit: 100) ⇒ Object
Constructor Details
#initialize(filename:) ⇒ Analyzer
Returns a new instance of Analyzer.
13 14 15 16 |
# File 'lib/log_analyzer/analyzer.rb', line 13 def initialize(filename:) @filename = filename @stats = {} end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
10 11 12 |
# File 'lib/log_analyzer/analyzer.rb', line 10 def filename @filename end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
11 12 13 |
# File 'lib/log_analyzer/analyzer.rb', line 11 def stats @stats end |
Instance Method Details
#avg_label(avg) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/log_analyzer/analyzer.rb', line 58 def avg_label(avg) str = avg.to_s if avg > DANGER_DEFAULT str.white.on_red elsif avg > WARNING_DEFAULT str.red elsif avg > INFO_DEFAULT str.yellow else str.green end end |
#order(by: :time) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/log_analyzer/analyzer.rb', line 29 def order(by: :time) case by.to_sym when :name @stats = @stats.sort{|a, b| a[0] <=> b[0] } when :time @stats = @stats.sort{|a, b| a[1].avg <=> b[1].avg } when :count @stats = @stats.sort{|a, b| a[1].count <=> b[1].count } end end |
#run ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/log_analyzer/analyzer.rb', line 18 def run IO.foreach(filename).each do |line| if line =~ MATCHER if $1 && $2 @stats[$1] ||= Stat.new @stats[$1].push($2) end end end end |
#visualize(limit: 100) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/log_analyzer/analyzer.rb', line 40 def visualize(limit: 100) length = (0..DEFAULT_TABLE_WIDTH - 20).freeze table = Terminal::Table.new \ headings: ['Path', 'Count', 'AVG (ms)', 'Max', 'Min'], width: DEFAULT_TABLE_WIDTH do |t| stats.each do |path, stat| t.add_row [ path[length], stat.count, avg_label(stat.avg), stat.max, stat.min, ] end end puts table end |