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
-
:categoryProc that handles the request categorization. -
:ifProc that has to return !nil for a request to be passed to the tracker. -
:line_typeThe line type that contains the duration field (determined by the category proc). -
:nilsTrack undetermined methods. -
:titleTitle do be displayed above the report. -
:unlessProc 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
catThe 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.
23 24 25 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 23 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)
60 61 62 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 60 def frequency(cat) categories[cat] || 0 end |
#overall_frequency ⇒ Object
Return the overall frequency
65 66 67 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 65 def overall_frequency categories.reduce(0) { |carry, item| carry + item[1] } end |
#prepare ⇒ Object
Check if categories are set up
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 26 def prepare [:category] = [:value] if [:value] && ![:category] fail "No categorizer set up for category tracker #{inspect}" unless [:category] @categorizer = create_lambda([:category]) unless [:multiple] # Initialize the categories. Use the list of category names to @categories = {} [:all_categories].each { |cat| @categories[cat] = 0 } if [:all_categories].is_a?(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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 77 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
70 71 72 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 70 def sorted_by_frequency @categories.sort { |a, b| b[1] <=> a[1] } end |
#title ⇒ Object
Returns the title of this tracker for reports
102 103 104 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 102 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
96 97 98 99 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 96 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 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 39 def update(request) if [:multiple] cats = request.every([:category]) cats.each do |cat| if cat || [:nils] @categories[cat] ||= 0 @categories[cat] += 1 end end else cat = @categorizer.call(request) if cat || [:nils] @categories[cat] ||= 0 @categories[cat] += 1 end end end |