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

  • :amount The amount of lines in the report

  • :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

#finalize, #initialize, #setup_should_update_checks!, #should_update?

Constructor Details

This class inherits a constructor from RequestLogAnalyzer::Tracker::Base

Instance Attribute Details

#frequenciesObject (readonly)

Returns the value of attribute frequencies.



26
27
28
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 26

def frequencies
  @frequencies
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)
  frequencies[cat] || 0
end

#overall_frequencyObject

Return the overall frequency



54
55
56
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 54

def overall_frequency
  frequencies.inject(0) { |carry, item| carry + item[1] }
end

#prepareObject

Check if categories are set up



29
30
31
32
33
34
35
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 29

def prepare
  raise "No categorizer set up for category tracker #{self.inspect}" unless options[:category]
  @frequencies = {}
  if options[:all_categories].kind_of?(Enumerable)
    options[:all_categories].each { |cat| @frequencies[cat] = 0 }
  end
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
83
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 66

def report(output)
  output.title(options[:title]) if options[:title]

  if @frequencies.empty?
    output << "None found.\n" 
  else
    sorted_frequencies = @frequencies.sort { |a, b| b[1] <=> a[1] }
    total_hits         = sorted_frequencies.inject(0) { |carry, item| carry + item[1] }
    sorted_frequencies = sorted_frequencies.slice(0...options[:amount]) if options[:amount]

    output.table({:align => :left}, {:align => :right }, {:align => :right}, {:type => :ratio, :width => :rest}) do |rows|        
      sorted_frequencies.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



59
60
61
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 59

def sorted_by_frequency
  @frequencies.sort { |a, b| b[1] <=> a[1] }
end

#titleObject

Returns the title of this tracker for reports



92
93
94
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 92

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

#to_yaml_objectObject

Returns a hash with the frequencies of every category that can be exported to YAML



86
87
88
89
# File 'lib/request_log_analyzer/tracker/frequency.rb', line 86

def to_yaml_object
  return nil if @frequencies.empty?
  @frequencies
end

#update(request) ⇒ Object

Check HTTP method of a request and store that in the frequencies 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 = options[:category].respond_to?(:call) ? options[:category].call(request) : request[options[:category]]
  if !cat.nil? || options[:nils]
    @frequencies[cat] ||= 0
    @frequencies[cat] += 1
  end
end