Sha256: 938a10897455d98e25c1d472f8838fbfd3ffe98a6ed651b1f97590f8d5b1da94

Contents?: true

Size: 1.1 KB

Versions: 105

Compression:

Stored size: 1.1 KB

Contents

# encoding: utf-8
require "logstash/namespace"

module LogStash; module Util; module ByteValue
  module_function

  B = 1
  KB = B << 10
  MB = B << 20
  GB = B << 30
  TB = B << 40
  PB = B << 50

  def parse(text)
    if !text.is_a?(String)
      raise ArgumentError, "ByteValue::parse takes a String, got a `#{text.class.name}`"
    end
    number = text.to_f
    factor = multiplier(text)

    (number * factor).to_i
  end

  def multiplier(text)
    case text
      when /(?:k|kb)$/ 
        KB
      when /(?:m|mb)$/
        MB
      when /(?:g|gb)$/
        GB
      when /(?:t|tb)$/
        TB
      when /(?:p|pb)$/
        PB
      when /(?:b)$/
        B
      else 
        raise ArgumentError, "Unknown bytes value '#{text}'"
    end
  end

  def human_readable(number)
    value, unit = if number > PB
      [number / PB, "pb"]
    elsif number > TB
      [number / TB, "tb"]
    elsif number > GB
      [number / GB, "gb"]
    elsif number > MB
      [number / MB, "mb"]
    elsif number > KB
      [number / KB, "kb"]
    else
      [number, "b"]
    end

    format("%.2d%s", value, unit)
  end
end end end

Version data entries

105 entries across 102 versions & 11 rubygems

Version Path
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb
logstash-output-scalyr-0.1.10.beta vendor/bundle/jruby/2.5.0/gems/logstash-core-5.6.4-java/lib/logstash/util/byte_value.rb