Sha256: e958c4280eba03e0b8f269a0cd2b512896a4db73333f7a58959dc874f942e824

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

module RequestLogAnalyzer::Tracker

  # Analyze the average and total traffic of requests
  #
  # === Options
  # * <tt>:category</tt> Proc that handles request categorization for given fileformat (REQUEST_CATEGORIZER)
  # * <tt>:traffic</tt> The field containing the duration in the request hash.
  # * <tt>:if</tt> Proc that has to return !nil for a request to be passed to the tracker.
  # * <tt>:line_type</tt> The line type that contains the duration field (determined by the category proc).
  # * <tt>:title</tt> Title do be displayed above the report
  # * <tt>:unless</tt> Handle request if this proc is false for the handled request.
  class Traffic < NumericValue

    # Check if duration and catagory option have been received,
    def prepare
      options[:value] = options[:traffic] if options[:traffic]
      options[:total] = true
      super
    end

    # Formats the traffic number using x B/kB/MB/GB etc notation
    def display_value(bytes)
      return "-"   if bytes.nil?
      return "0 B" if bytes.zero?
      
      case [Math.log10(bytes.abs).floor, 0].max
      when  0...4  then '%d B'  % bytes
      when  4...7  then '%d kB' % (bytes / 1000)
      when  7...10 then '%d MB' % (bytes / 1000_000)
      when 10...13 then '%d GB' % (bytes / 1000_000_000)
      else              '%d TB' % (bytes / 1000_000_000_000)
      end
    end

    # Returns the title of this tracker for reports
    def title
      options[:title]  || 'Request traffic'
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
request-log-analyzer-1.8.1 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.8.0 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.7.0 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.6.4 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.6.3 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.6.2 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.6.1 lib/request_log_analyzer/tracker/traffic.rb