Class: RequestLogAnalyzer::Tracker::Frequency

Inherits:
Base
  • Object
show all
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

Attributes inherited from Base

#options

Instance Method Summary collapse

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

#categoriesObject (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_frequencyObject

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

#prepareObject

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
  options[:category] = options[:value] if options[:value] && !options[:category]
  fail "No categorizer set up for category tracker #{inspect}" unless options[:category]

  @categorizer = create_lambda(options[:category]) unless options[:multiple]

  # Initialize the categories. Use the list of category names to
  @categories = {}
  options[:all_categories].each { |cat| @categories[cat] = 0 } if options[: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(options[:title]) if options[: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_frequencyObject

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

#titleObject

Returns the title of this tracker for reports



102
103
104
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 102

def title
  options[:title] || 'Request frequency'
end

#to_yaml_objectObject

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 options[:multiple]
    cats = request.every(options[:category])
    cats.each do |cat|
      if cat || options[:nils]
        @categories[cat] ||= 0
        @categories[cat] += 1
      end
    end

  else
    cat = @categorizer.call(request)
    if cat || options[:nils]
      @categories[cat] ||= 0
      @categories[cat] += 1
    end
  end
end