Class: LogAnalyzer::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/log_analyzer/analyzer.rb

Constant Summary collapse

DEFAULT_TABLE_WIDTH =

width

120
CONTENT_LENGTH =
(0..DEFAULT_TABLE_WIDTH - 20).freeze
10
HEADER =
['Type', 'View', 'Count', 'AVG (ms)', 'Max', 'Min'].freeze
MATCHER =
/Rendered (.*\/.*) \((.*)ms\)/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#filenameObject (readonly)

Returns the value of attribute filename.



10
11
12
# File 'lib/log_analyzer/analyzer.rb', line 10

def filename
  @filename
end

#statsObject (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

#order(by: :time) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/log_analyzer/analyzer.rb', line 33

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 :rtime
    @stats = @stats.sort{|a, b| b[1].avg <=> a[1].avg }
  when :count
    @stats = @stats.sort{|a, b| a[1].count <=> b[1].count }
  end
end

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/log_analyzer/analyzer.rb', line 18

def run
  IO.foreach(filename).each do |line|
    if line.scrub =~ MATCHER
      if $1 && $2
        view = $1
        @stats[view] ||= Stat.new(type: Utils.find_type(view))
        @stats[view].push($2)
      end
    end
  end
rescue Errno::ENOENT
  puts "File <#{filename}> is not found or inaccessible.".red
  exit
end

#to_csv(out_filename, short: false) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/log_analyzer/analyzer.rb', line 92

def to_csv(out_filename, short: false)
  CSV.open("#{Dir.pwd}/#{out_filename}", 'w') do |csv|
    #CSV header
    csv << HEADER
    #CSV content
    stats.each do |path, stat|
      csv << csv_format_row(path, stat, short)
    end
  end
end

#to_pdf(out_filename, short: false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/log_analyzer/analyzer.rb', line 68

def to_pdf(out_filename, short: false)
  this = self
  data = [HEADER]
  this.stats.each do |path, stat|
    next unless LogAnalyzer.configuration.filters.include?(stat.type)
    data << pdf_row(path, stat, short)
  end
  Prawn::Document.generate(out_filename) do
    width     = 72
    text "LogAnalyzer Report", align: :center, size: 18
    text Time.now.strftime("Generated on %B %d, %Y at %I:%M%p") , align: :center, size: 7
    move_down 12
    table data,
      width: bounds.width,
      header: true,
      row_colors: ["FFFFFF", "FFFFCC"],
      cell_style: { padding: [2, 2, 2, 2], size: 7 } do |t|
        t.columns(0).style(align: :center)
    end
    move_down 20
    text %(Generated by <link href="https://github.com/igorkasyanchuk/log_analyzer">https://github.com/igorkasyanchuk/log_analyzer</link>.), inline_format: true, size: 5, color: 'CCCCCC'
  end
end

#to_xls(out_filename, short: false) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/log_analyzer/analyzer.rb', line 103

def to_xls(out_filename, short: false)
  xls = Spreadsheet::Workbook.new
  xls.create_worksheet
  #XLS header
  xls.worksheet(0).insert_row(0, HEADER)
  #XLS content
  stats.each do |path, stat|
    xls.worksheet(0).insert_row(
      xls.worksheet(0).last_row_index + 1,
      xls_format_row(path, stat, short)
    )
  end
  xls.write("#{Dir.pwd}/#{out_filename}")
end

#visualize(limit: 100, short: false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/log_analyzer/analyzer.rb', line 46

def visualize(limit: 100, short: false)
  table      = new_table
  rows_count = 0

  # preparing report
  stats.each do |path, stat|
    next unless LogAnalyzer.configuration.filters.include?(stat.type)

    table.add_row console_row(path, stat, short)

    rows_count += 1
  end

  # adding a footer
  if rows_count > ROWS_FOR_FOOTER
    table.add_separator
    table.add_row(HEADER)
  end

  puts(table)
end