Sha256: 6c409d2c63da018a59c966e9b10e2614ef4b68dee645edeb5c5379f44e87cf72

Contents?: true

Size: 1.92 KB

Versions: 7

Compression:

Stored size: 1.92 KB

Contents

module RequestLogAnalyzer::Tracker

  # Determines the datetime of the first request and the last request
  # Also determines the amount of days inbetween these.
  #
  # Accepts the following options:
  # * <tt>:line_type</tt> The line type that contains the duration field (determined by the category proc).
  # * <tt>:if</tt> Proc that has to return !nil for a request to be passed to the tracker.
  # * <tt>:field</tt> The timestamp field that is looked at. Defaults to :timestamp.
  # * <tt>:title</tt> Title do be displayed above the report.
  #
  # Expects the following items in the update request hash
  # * <tt>:timestamp</tt> 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
  class Timespan < RequestLogAnalyzer::Tracker::Base

    attr_reader :first, :last, :request_time_graph
  
    def prepare
      options[:field] ||= :timestamp
    end
              
    def update(request)
      timestamp = request[options[:field]]

      @first = timestamp if @first.nil? || timestamp < @first
      @last  = timestamp if @last.nil?  || timestamp > @last
    end
  
    def report(output = STDOUT, report_width = 80, color = false)
      if options[:title]
        output << "\n#{options[:title]}\n"
        output << green('━' * options[:title].length, color) + "\n"
      end
      
      first_date  = DateTime.parse(@first.to_s, '%Y%m%d%H%M%S') rescue nil
      last_date   = DateTime.parse(@last.to_s, '%Y%m%d%H%M%S') rescue nil
      
      if @last && @first
        days        = (@last && @first) ? (last_date - first_date).ceil : 1

        output << "First request:        #{first_date.strftime('%Y-%m-%d %H:%M:%I')}\n"
        output << "Last request:         #{last_date.strftime('%Y-%m-%d %H:%M:%I')}\n"
        output << "Total time analyzed:  #{days} days\n"
      end
      output << "\n"
      
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
wvanbergen-request-log-analyzer-1.0.1 lib/request_log_analyzer/tracker/timespan.rb
wvanbergen-request-log-analyzer-1.0.2 lib/request_log_analyzer/tracker/timespan.rb
wvanbergen-request-log-analyzer-1.0.3 lib/request_log_analyzer/tracker/timespan.rb
wvanbergen-request-log-analyzer-1.0.4 lib/request_log_analyzer/tracker/timespan.rb
request-log-analyzer-1.0.4 lib/request_log_analyzer/tracker/timespan.rb
request-log-analyzer-1.0.2 lib/request_log_analyzer/tracker/timespan.rb
request-log-analyzer-1.0.3 lib/request_log_analyzer/tracker/timespan.rb