Module: LogStash::Util::ByteValue

Defined in:
lib/logstash/util/byte_value.rb

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.human_readable(number) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/logstash/util/byte_value.rb', line 43

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

.multiplier(text) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/logstash/util/byte_value.rb', line 24

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

.parse(text) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/logstash/util/byte_value.rb', line 14

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