class RequestLogAnalyzer::Tracker::Frequency
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%) |
Attributes
Public Instance Methods
Return the amount of times a HTTP method has been encountered cat
The HTTP method (:get, :put, :post or :delete)
# File lib/request_log_analyzer/tracker/frequency.rb 60 def frequency(cat) 61 categories[cat] || 0 62 end
Return the overall frequency
# File lib/request_log_analyzer/tracker/frequency.rb 65 def overall_frequency 66 categories.reduce(0) { |carry, item| carry + item[1] } 67 end
Check if categories are set up
# File lib/request_log_analyzer/tracker/frequency.rb 26 def prepare 27 options[:category] = options[:value] if options[:value] && !options[:category] 28 fail "No categorizer set up for category tracker #{inspect}" unless options[:category] 29 30 @categorizer = create_lambda(options[:category]) unless options[:multiple] 31 32 # Initialize the categories. Use the list of category names to 33 @categories = {} 34 options[:all_categories].each { |cat| @categories[cat] = 0 } if options[:all_categories].is_a?(Enumerable) 35 end
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
# File lib/request_log_analyzer/tracker/frequency.rb 77 def report(output) 78 output.title(options[:title]) if options[:title] 79 80 if @categories.empty? 81 output << "None found.\n" 82 else 83 sorted_categories = output.slice_results(sorted_by_frequency) 84 total_hits = overall_frequency 85 86 output.table({ align: :left }, { align: :right }, { align: :right }, { type: :ratio, width: :rest }) do |rows| 87 sorted_categories.each do |(cat, count)| 88 rows << [cat, "#{count} hits", '%0.1f%%' % ((count.to_f / total_hits.to_f) * 100.0), (count.to_f / total_hits.to_f)] 89 end 90 end 91 92 end 93 end
Return the methods sorted by frequency
# File lib/request_log_analyzer/tracker/frequency.rb 70 def sorted_by_frequency 71 @categories.sort { |a, b| b[1] <=> a[1] } 72 end
Returns the title of this tracker for reports
# File lib/request_log_analyzer/tracker/frequency.rb 102 def title 103 options[:title] || 'Request frequency' 104 end
Returns a hash with the categories of every category that can be exported to YAML
# File lib/request_log_analyzer/tracker/frequency.rb 96 def to_yaml_object 97 return nil if @categories.empty? 98 @categories 99 end
Check HTTP method of a request and store that in the categories hash. request
The request.
# File lib/request_log_analyzer/tracker/frequency.rb 39 def update(request) 40 if options[:multiple] 41 cats = request.every(options[:category]) 42 cats.each do |cat| 43 if cat || options[:nils] 44 @categories[cat] ||= 0 45 @categories[cat] += 1 46 end 47 end 48 49 else 50 cat = @categorizer.call(request) 51 if cat || options[:nils] 52 @categories[cat] ||= 0 53 @categories[cat] += 1 54 end 55 end 56 end