class RequestLogAnalyzer::Tracker::HourlySpread
Determines the average hourly spread of the parsed requests. This spread is shown in a graph form.
Accepts the following options:
-
: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). -
:output
Direct output here (defaults to STDOUT) -
:unless
Proc that has to return nil for a request to be passed to the tracker.
Expects the following items in the update request hash
-
:timestamp
in 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 : == ................
Attributes
Public Instance Methods
First timestamp encountered
# File lib/request_log_analyzer/tracker/hourly_spread.rb 55 def first_timestamp 56 DateTime.parse(@first.to_s, '%Y%m%d%H%M%S') rescue nil 57 end
Last timestamp encountered
# File lib/request_log_analyzer/tracker/hourly_spread.rb 60 def last_timestamp 61 DateTime.parse(@last.to_s, '%Y%m%d%H%M%S') rescue nil 62 end
Check if timestamp field is set in the options and prepare the result time graph.
# File lib/request_log_analyzer/tracker/hourly_spread.rb 34 def prepare 35 options[:field] ||= :timestamp 36 @hour_frequencies = (0...24).map { 0 } 37 @first, @last = 99_999_999_999_999, 0 38 end
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
# File lib/request_log_analyzer/tracker/hourly_spread.rb 72 def report(output) 73 output.title(title) 74 75 if total_requests == 0 76 output << "None found.\n" 77 return 78 end 79 80 days = [1, timespan].max 81 output.table({}, { align: :right }, { type: :ratio, width: :rest, treshold: 0.15 }) do |rows| 82 @hour_frequencies.each_with_index do |requests, index| 83 ratio = requests.to_f / total_requests.to_f 84 requests_per_day = (requests / days).ceil 85 rows << ["#{index.to_s.rjust(3)}:00", '%d hits/day' % requests_per_day, ratio] 86 end 87 end 88 end
Difference between last and first timestamp.
# File lib/request_log_analyzer/tracker/hourly_spread.rb 65 def timespan 66 last_timestamp - first_timestamp 67 end
Returns the title of this tracker for reports
# File lib/request_log_analyzer/tracker/hourly_spread.rb 91 def title 92 options[:title] || 'Request distribution per hour' 93 end
Returns the found frequencies per hour as a hash for YAML exporting
# File lib/request_log_analyzer/tracker/hourly_spread.rb 96 def to_yaml_object 97 yaml_object = {} 98 @hour_frequencies.each_with_index do |freq, hour| 99 yaml_object["#{hour}:00 - #{hour + 1}:00"] = freq 100 end 101 yaml_object 102 end
Total amount of requests tracked
# File lib/request_log_analyzer/tracker/hourly_spread.rb 50 def total_requests 51 @hour_frequencies.reduce(0) { |sum, value| sum + value } 52 end
Check if the timestamp in the request and store it. request
The request.
# File lib/request_log_analyzer/tracker/hourly_spread.rb 42 def update(request) 43 timestamp = request.first(options[:field]) 44 @hour_frequencies[timestamp.to_s[8..9].to_i] += 1 45 @first = timestamp if timestamp < @first 46 @last = timestamp if timestamp > @last 47 end