class RequestLogAnalyzer::Tracker::Timespan
Determines the datetime of the first request and the last request Also determines the amount of days inbetween these.
Accepts the following options:
-
:field
The timestamp field that is looked at. Defaults to :timestamp. -
: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). -
:title
Title do be displayed above the report. -
: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:
First request: 2008-07-13 06:25:06 Last request: 2008-07-20 06:18:06 Total time analyzed: 7 days
Attributes
Public Instance Methods
First timestamp encountered
# File lib/request_log_analyzer/tracker/timespan.rb 37 def first_timestamp 38 DateTime.parse(@first.to_s, '%Y%m%d%H%M%S') rescue nil 39 end
Last timestamp encountered
# File lib/request_log_analyzer/tracker/timespan.rb 42 def last_timestamp 43 DateTime.parse(@last.to_s, '%Y%m%d%H%M%S') rescue nil 44 end
Check if timestamp field is set in the options.
# File lib/request_log_analyzer/tracker/timespan.rb 23 def prepare 24 options[:field] ||= :timestamp 25 @first, @last = 99_999_999_999_999, 0 26 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/timespan.rb 54 def report(output) 55 output.title(options[:title]) if options[:title] 56 57 if @last > 0 && @first < 99_999_999_999_999 58 output.with_style(cell_separator: false) do 59 output.table({ width: 20 }, {}) do |rows| 60 rows << ['First request:', first_timestamp.strftime('%Y-%m-%d %H:%M:%I')] 61 rows << ['Last request:', last_timestamp.strftime('%Y-%m-%d %H:%M:%I')] 62 rows << ['Total time analyzed:', "#{timespan.ceil} days"] 63 end 64 end 65 end 66 end
Difference between last and first timestamp.
# File lib/request_log_analyzer/tracker/timespan.rb 47 def timespan 48 last_timestamp - first_timestamp 49 end
Returns the title of this tracker for reports
# File lib/request_log_analyzer/tracker/timespan.rb 69 def title 70 options[:title] || 'Request timespan' 71 end
A hash that can be exported to YAML with the first and last timestamp encountered.
# File lib/request_log_analyzer/tracker/timespan.rb 74 def to_yaml_object 75 { first: first_timestamp, last: last_timestamp } 76 end
Check if the timestamp in the request and store it. request
The request.
# File lib/request_log_analyzer/tracker/timespan.rb 30 def update(request) 31 timestamp = request[options[:field]] 32 @first = timestamp if timestamp < @first 33 @last = timestamp if timestamp > @last 34 end