Class: RequestLogAnalyzer::Tracker::Frequency
- Defined in:
- lib/request_log_analyzer/tracker/frequency.rb
Overview
Catagorize requests by frequency. Count and analyze requests for a specific attribute
Options
-
:category
Proc that handles the request categorization. -
:if
Proc that has to return !nil for a request to be passed to the tracker. -
:line_type
The line type that contains the duration field (determined by the category proc). -
:nils
Track undetermined methods. -
:title
Title do be displayed above the report. -
:unless
Proc that has to return nil for a request to be passed to the tracker.
The items in the update request hash are set during the creation of the Duration tracker.
Example output:
HTTP methods
----------------------------------------------------------------------
GET | 22248 hits (46.2%) |=================
PUT | 13685 hits (28.4%) |===========
POST | 11662 hits (24.2%) |=========
DELETE | 512 hits (1.1%) |
Instance Attribute Summary collapse
-
#categories ⇒ Object
readonly
Returns the value of attribute categories.
Attributes inherited from Base
Instance Method Summary collapse
-
#frequency(cat) ⇒ Object
Return the amount of times a HTTP method has been encountered
cat
The HTTP method (:get, :put, :post or :delete). -
#overall_frequency ⇒ Object
Return the overall frequency.
-
#prepare ⇒ Object
Check if categories are set up.
-
#report(output) ⇒ Object
Generate a HTTP method frequency report to the given output object.
-
#sorted_by_frequency ⇒ Object
Return the methods sorted by frequency.
-
#title ⇒ Object
Returns the title of this tracker for reports.
-
#to_yaml_object ⇒ Object
Returns a hash with the categories of every category that can be exported to YAML.
-
#update(request) ⇒ Object
Check HTTP method of a request and store that in the categories hash.
Methods inherited from Base
#create_lambda, #finalize, #initialize, #setup_should_update_checks!, #should_update?
Constructor Details
This class inherits a constructor from RequestLogAnalyzer::Tracker::Base
Instance Attribute Details
#categories ⇒ Object (readonly)
Returns the value of attribute categories.
25 26 27 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 25 def categories @categories end |
Instance Method Details
#frequency(cat) ⇒ Object
Return the amount of times a HTTP method has been encountered cat
The HTTP method (:get, :put, :post or :delete)
49 50 51 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 49 def frequency(cat) categories[cat] || 0 end |
#overall_frequency ⇒ Object
Return the overall frequency
54 55 56 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 54 def overall_frequency categories.inject(0) { |carry, item| carry + item[1] } end |
#prepare ⇒ Object
Check if categories are set up
28 29 30 31 32 33 34 35 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 28 def prepare raise "No categorizer set up for category tracker #{self.inspect}" unless [:category] @categorizer = create_lambda([:category]) # Initialize the categories. Use the list of category names to @categories = {} [:all_categories].each { |cat| @categories[cat] = 0 } if [:all_categories].kind_of?(Enumerable) end |
#report(output) ⇒ Object
Generate a HTTP method frequency report to the given output object. Any options for the report should have been set during initialize. output
The output object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 66 def report(output) output.title([:title]) if [:title] if @categories.empty? output << "None found.\n" else sorted_categories = output.slice_results(sorted_by_frequency) total_hits = overall_frequency output.table({:align => :left}, {:align => :right }, {:align => :right}, {:type => :ratio, :width => :rest}) do |rows| sorted_categories.each do |(cat, count)| rows << [cat, "#{count} hits", '%0.1f%%' % ((count.to_f / total_hits.to_f) * 100.0), (count.to_f / total_hits.to_f)] end end end end |
#sorted_by_frequency ⇒ Object
Return the methods sorted by frequency
59 60 61 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 59 def sorted_by_frequency @categories.sort { |a, b| b[1] <=> a[1] } end |
#title ⇒ Object
Returns the title of this tracker for reports
91 92 93 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 91 def title [:title] || 'Request frequency' end |
#to_yaml_object ⇒ Object
Returns a hash with the categories of every category that can be exported to YAML
85 86 87 88 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 85 def to_yaml_object return nil if @categories.empty? @categories end |
#update(request) ⇒ Object
Check HTTP method of a request and store that in the categories hash. request
The request.
39 40 41 42 43 44 45 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 39 def update(request) cat = @categorizer.call(request) if cat || [:nils] @categories[cat] ||= 0 @categories[cat] += 1 end end |