Class: RequestLogAnalyzer::Tracker::Timespan

Inherits:
Base
  • Object
show all
Defined in:
lib/request_log_analyzer/tracker/timespan.rb

Overview

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

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

#firstObject (readonly)

Returns the value of attribute first.



22
23
24
# File 'lib/request_log_analyzer/tracker/timespan.rb', line 22

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



22
23
24
# File 'lib/request_log_analyzer/tracker/timespan.rb', line 22

def last
  @last
end

#request_time_graphObject (readonly)

Returns the value of attribute request_time_graph.



22
23
24
# File 'lib/request_log_analyzer/tracker/timespan.rb', line 22

def request_time_graph
  @request_time_graph
end

Instance Method Details

#first_timestampObject

First timestamp encountered



39
40
41
# File 'lib/request_log_analyzer/tracker/timespan.rb', line 39

def first_timestamp
  DateTime.parse(@first.to_s, '%Y%m%d%H%M%S') rescue nil
end

#last_timestampObject

Last timestamp encountered



44
45
46
# File 'lib/request_log_analyzer/tracker/timespan.rb', line 44

def last_timestamp
  DateTime.parse(@last.to_s, '%Y%m%d%H%M%S') rescue nil      
end

#prepareObject

Check if timestamp field is set in the options.



25
26
27
# File 'lib/request_log_analyzer/tracker/timespan.rb', line 25

def prepare
  options[:field] ||= :timestamp
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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/request_log_analyzer/tracker/timespan.rb', line 56

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

  if @last && @first
    output.with_style(:cell_separator => false) do         
      output.table({:width => 20}, {}) do |rows|
        rows << ['First request:', first_timestamp.strftime('%Y-%m-%d %H:%M:%I')]
        rows << ['Last request:', last_timestamp.strftime('%Y-%m-%d %H:%M:%I')]
        rows << ['Total time analyzed:', "#{timespan.ceil} days"]                    
      end
    end
  end
end

#timespanObject

Difference between last and first timestamp.



49
50
51
# File 'lib/request_log_analyzer/tracker/timespan.rb', line 49

def timespan
  last_timestamp - first_timestamp
end

#titleObject

Returns the title of this tracker for reports



71
72
73
# File 'lib/request_log_analyzer/tracker/timespan.rb', line 71

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

#to_yaml_objectObject

A hash that can be exported to YAML with the first and last timestamp encountered.



76
77
78
# File 'lib/request_log_analyzer/tracker/timespan.rb', line 76

def to_yaml_object
  { :first => first_timestamp, :last  =>last_timestamp }
end

#update(request) ⇒ Object

Check if the timestamp in the request and store it. request The request.



31
32
33
34
35
36
# File 'lib/request_log_analyzer/tracker/timespan.rb', line 31

def update(request)
  timestamp = request[options[:field]]

  @first = timestamp if @first.nil? || timestamp < @first
  @last  = timestamp if @last.nil?  || timestamp > @last
end