Class: RequestLogAnalyzer::Aggregator::Summarizer

Inherits:
Base
  • Object
show all
Defined in:
lib/request_log_analyzer/aggregator/summarizer.rb

Defined Under Namespace

Classes: Definer

Instance Attribute Summary collapse

Attributes inherited from Base

#options, #source

Instance Method Summary collapse

Methods inherited from Base

#source_change

Constructor Details

#initialize(source, options = {}) ⇒ Summarizer

Initialize summarizer. Generate trackers from speciefied source.file_format.report_trackers and set them up



66
67
68
69
70
71
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 66

def initialize(source, options = {})
  super(source, options)
  @warnings_encountered = {}
  @trackers = source.file_format.report_trackers
  setup
end

Instance Attribute Details

#trackersObject (readonly)

Returns the value of attribute trackers.



61
62
63
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 61

def trackers
  @trackers
end

#warnings_encounteredObject (readonly)

Returns the value of attribute warnings_encountered.



62
63
64
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 62

def warnings_encountered
  @warnings_encountered
end

Instance Method Details

#aggregate(request) ⇒ Object

Pass all requests to trackers and let them update if necessary. request The request to pass.



84
85
86
87
88
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 84

def aggregate(request)
  @trackers.each do |tracker|
    tracker.update(request) if tracker.should_update?(request)
  end
end

#finalizeObject

Call finalize on all trackers. Saves a YAML dump if this is set in the options.



91
92
93
94
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 91

def finalize
  @trackers.each { |tracker| tracker.finalize }
  save_results_dump(options[:dump]) if options[:dump]
end

#has_log_ordering_warnings?Boolean

Returns true if there were any log ordering warnings

Returns:

  • (Boolean)


162
163
164
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 162

def has_log_ordering_warnings?
  @warnings_encountered[:no_current_request] && @warnings_encountered[:no_current_request] > 0
end

#has_warnings?Boolean

Returns true if there were any warnings generated by the trackers

Returns:

  • (Boolean)


157
158
159
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 157

def has_warnings?
  @warnings_encountered.inject(0) { |result, (key, value)| result += value } > 0
end

#prepareObject

Call prepare on all trackers.



77
78
79
80
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 77

def prepare
  raise "No trackers set up in Summarizer!" if @trackers.nil? || @trackers.empty?
  @trackers.each { |tracker| tracker.prepare }
end

#report(output) ⇒ Object

Call report on all trackers. output RequestLogAnalyzer::Output object to output to



114
115
116
117
118
119
120
121
122
123
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 114

def report(output)
  report_header(output)
  if source.parsed_requests > 0
    @trackers.each { |tracker| tracker.report(output) }
  else
    output.puts
    output.puts('There were no requests analyzed.')
  end
  report_footer(output)
end

Generate report footer. output RequestLogAnalyzer::Output object to output to



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 144

def report_footer(output)
  if has_log_ordering_warnings?
    output.title("Parse warnings")
    
    output.puts "Parseable lines were ancountered without a header line before it. It"
    output.puts "could be that logging is not setup correctly for your application."
    output.puts "Visit this website for logging configuration tips:"
    output.puts output.link("http://github.com/wvanbergen/request-log-analyzer/wikis/configure-logging")
    output.puts
  end
end

#report_header(output) ⇒ Object

Generate report header. output RequestLogAnalyzer::Output object to output to



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 127

def report_header(output)
  output.title("Request summary")
  
  output.with_style(:cell_separator => false) do 
    output.table({:width => 20}, {:font => :bold}) do |rows|
      rows << ['Parsed lines:',    source.parsed_lines]
      rows << ['Skipped lines:',   source.skipped_lines]
      rows << ['Parsed requests:', source.parsed_requests]
      rows << ['Skipped requests:', source.skipped_requests]
      rows << ["Warnings:", @warnings_encountered.map { |(key, value)| "#{key}: #{value}" }.join(', ')] if has_warnings?
    end
  end
  output << "\n"
end

#save_results_dump(filename) ⇒ Object

Saves the results of all the trackers in YAML format to a file. filename The file to store the YAML dump in.



98
99
100
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 98

def save_results_dump(filename)
  File.open(filename, 'w') { |file| file.write(to_yaml) }
end

#setupObject



73
74
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 73

def setup
end

#to_yamlObject

Exports all the tracker results to YAML. It will call the to_yaml_object method for every tracker and combines these into a single YAML export.



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

def to_yaml
  require 'yaml'
  trackers_export = @trackers.inject({}) do |export, tracker|
    export[tracker.title] = tracker.to_yaml_object; export
  end
  YAML::dump(trackers_export)
end

#warning(type, message, lineno) ⇒ Object

Store an encountered warning type Type of warning message Warning message lineno The line on which the error was encountered



170
171
172
173
# File 'lib/request_log_analyzer/aggregator/summarizer.rb', line 170

def warning(type, message, lineno)
  @warnings_encountered[type] ||= 0
  @warnings_encountered[type] += 1
end