Sha256: a18fc8bbf9ef73922f822b1584008762f3bc06f2c3738f85dea55a65d5603e03

Contents?: true

Size: 1.86 KB

Versions: 29

Compression:

Stored size: 1.86 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
      
      @number_of_buckets = options[:number_of_buckets] || 1000
      @min_bucket_value  = options[:min_bucket_value] ? options[:min_bucket_value].to_f : 1
      @max_bucket_value  = options[:max_bucket_value] ? options[:max_bucket_value].to_f : 1000_000_000_000

      # precalculate the bucket size
      @bucket_size = (Math.log(@max_bucket_value) - Math.log(@min_bucket_value)) / @number_of_buckets.to_f
    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

29 entries across 29 versions & 1 rubygems

Version Path
request-log-analyzer-1.13.1 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.13.0 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.11 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.10 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.9 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.8 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.7 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.6 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.5 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.4 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.3 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.2 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.1 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.12.0 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.11.1 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.11.0 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.10.1 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.10.0 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.9.10 lib/request_log_analyzer/tracker/traffic.rb
request-log-analyzer-1.9.9 lib/request_log_analyzer/tracker/traffic.rb