Class: RequestLogAnalyzer::Tracker::HourlySpread
- Defined in:
- lib/request_log_analyzer/tracker/hourly_spread.rb
Overview
Determines the average hourly spread of the parsed requests. This spread is shown in a graph form.
Accepts the following options:
-
: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). -
:outputDirect output here (defaults to STDOUT) -
:unlessProc that has to return nil for a request to be passed to the tracker.
Expects the following items in the update request hash
-
:timestampin YYYYMMDDHHMMSS format.
Example output:
Requests graph - average per day per hour
--------------------------------------------------
7:00 - 330 hits : =======
8:00 - 704 hits : =================
9:00 - 830 hits : ====================
10:00 - 822 hits : ===================
11:00 - 823 hits : ===================
12:00 - 729 hits : =================
13:00 - 614 hits : ==============
14:00 - 690 hits : ================
15:00 - 492 hits : ===========
16:00 - 355 hits : ========
17:00 - 213 hits : =====
18:00 - 107 hits : ==
................
Instance Attribute Summary collapse
-
#first ⇒ Object
readonly
Returns the value of attribute first.
-
#hour_frequencies ⇒ Object
readonly
Returns the value of attribute hour_frequencies.
-
#last ⇒ Object
readonly
Returns the value of attribute last.
Attributes inherited from Base
Instance Method Summary collapse
-
#first_timestamp ⇒ Object
First timestamp encountered.
-
#last_timestamp ⇒ Object
Last timestamp encountered.
-
#prepare ⇒ Object
Check if timestamp field is set in the options and prepare the result time graph.
-
#report(output) ⇒ Object
Generate an hourly spread report to the given output object.
-
#timespan ⇒ Object
Difference between last and first timestamp.
-
#title ⇒ Object
Returns the title of this tracker for reports.
-
#to_yaml_object ⇒ Object
Returns the found frequencies per hour as a hash for YAML exporting.
-
#total_requests ⇒ Object
Total amount of requests tracked.
-
#update(request) ⇒ Object
Check if the timestamp in the request and store it.
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
#first ⇒ Object (readonly)
Returns the value of attribute first.
31 32 33 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 31 def first @first end |
#hour_frequencies ⇒ Object (readonly)
Returns the value of attribute hour_frequencies.
31 32 33 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 31 def hour_frequencies @hour_frequencies end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
31 32 33 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 31 def last @last end |
Instance Method Details
#first_timestamp ⇒ Object
First timestamp encountered
55 56 57 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 55 def DateTime.parse(@first.to_s, '%Y%m%d%H%M%S') rescue nil end |
#last_timestamp ⇒ Object
Last timestamp encountered
60 61 62 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 60 def DateTime.parse(@last.to_s, '%Y%m%d%H%M%S') rescue nil end |
#prepare ⇒ Object
Check if timestamp field is set in the options and prepare the result time graph.
34 35 36 37 38 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 34 def prepare [:field] ||= :timestamp @hour_frequencies = (0...24).map { 0 } @first, @last = 99_999_999_999_999, 0 end |
#report(output) ⇒ Object
Generate an hourly spread report to the given output object. Any options for the report should have been set during initialize. output The output object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 72 def report(output) output.title(title) if total_requests == 0 output << "None found.\n" return end days = [1, timespan].max output.table({}, { align: :right }, { type: :ratio, width: :rest, treshold: 0.15 }) do |rows| @hour_frequencies.each_with_index do |requests, index| ratio = requests.to_f / total_requests.to_f requests_per_day = (requests / days).ceil rows << ["#{index.to_s.rjust(3)}:00", '%d hits/day' % requests_per_day, ratio] end end end |
#timespan ⇒ Object
Difference between last and first timestamp.
65 66 67 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 65 def timespan - end |
#title ⇒ Object
Returns the title of this tracker for reports
91 92 93 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 91 def title [:title] || 'Request distribution per hour' end |
#to_yaml_object ⇒ Object
Returns the found frequencies per hour as a hash for YAML exporting
96 97 98 99 100 101 102 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 96 def to_yaml_object yaml_object = {} @hour_frequencies.each_with_index do |freq, hour| yaml_object["#{hour}:00 - #{hour + 1}:00"] = freq end yaml_object end |
#total_requests ⇒ Object
Total amount of requests tracked
50 51 52 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 50 def total_requests @hour_frequencies.reduce(0) { |sum, value| sum + value } end |
#update(request) ⇒ Object
Check if the timestamp in the request and store it. request The request.
42 43 44 45 46 47 |
# File 'lib/request_log_analyzer/tracker/hourly_spread.rb', line 42 def update(request) = request.first([:field]) @hour_frequencies[.to_s[8..9].to_i] += 1 @first = if < @first @last = if > @last end |