Module: OsCtl::Lib::Utils::Humanize

Included in:
CGroup::PathReader::Base
Defined in:
lib/libosctl/utils/humanize.rb

Instance Method Summary collapse

Instance Method Details

#break_interval(interval) ⇒ Object (protected)



118
119
120
121
122
123
124
# File 'lib/libosctl/utils/humanize.rb', line 118

def break_interval(interval)
  d = interval / 86_400
  h = interval / 3600 % 24
  m = interval / 60 % 60
  s = interval % 60
  [d, h, m, s]
end

#format_long_duration(interval) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/libosctl/utils/humanize.rb', line 64

def format_long_duration(interval)
  d, h, m, s = break_interval(interval)

  if d > 0
    format('%d days, %02d:%02d:%02d', d, h, m, s)
  else
    format('%02d:%02d:%02d', h, m, s)
  end
end

#format_percent(v) ⇒ Object



91
92
93
# File 'lib/libosctl/utils/humanize.rb', line 91

def format_percent(v)
  v.round(1)
end

#format_short_duration(interval) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/libosctl/utils/humanize.rb', line 74

def format_short_duration(interval)
  d, h, m, s = break_interval(interval)

  if d == 0 && h == 0 && m == 0
    "#{s}s"

  elsif d == 0 && h == 0
    format('%02d:%02d', m, s)

  elsif d == 0
    format('%02d:%02d:%02d', h, m, s)

  else
    format('%dd, %02d:%02d:%02d', d, h, m, s)
  end
end

#humanize_data(v) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/libosctl/utils/humanize.rb', line 3

def humanize_data(v)
  bits = 39
  units = %i[T G M K]

  units.each do |u|
    threshold = 2 << bits

    if v >= threshold
      division = v / threshold.to_f

      return "#{division.round}#{u}" if division >= 1000

      return "#{division.round(1)}#{u}"

    end

    bits -= 10
  end

  if v >= 1000
    v.round.to_s
  elsif v >= 100
    v.round(1).to_s
  else
    v.round(2).to_s
  end
end

#humanize_number(v) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/libosctl/utils/humanize.rb', line 31

def humanize_number(v)
  divider = 10.0**12
  units = %i[T G M K]

  units.each do |u|
    division = v / divider

    if division >= 1000
      return "#{division.round}#{u}"
    elsif division >= 1
      return "#{division.round(1)}#{u}"
    end

    divider /= 1000
  end

  if v >= 1000
    v.round.to_s
  elsif v >= 100
    v.round(1).to_s
  else
    v.round(2).to_s
  end
end

#humanize_percent(v) ⇒ Object



95
96
97
# File 'lib/libosctl/utils/humanize.rb', line 95

def humanize_percent(v)
  "#{format_percent(v)}%"
end

#humanize_time_ns(v) ⇒ Object



60
61
62
# File 'lib/libosctl/utils/humanize.rb', line 60

def humanize_time_ns(v)
  format_short_duration(v / 1_000_000_000)
end

#humanize_time_us(v) ⇒ Object



56
57
58
# File 'lib/libosctl/utils/humanize.rb', line 56

def humanize_time_us(v)
  format_short_duration(v / 1_000_000)
end

#parse_data(v) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/libosctl/utils/humanize.rb', line 99

def parse_data(v)
  units = %w[k m g t]

  if /^\d+$/ =~ v
    v.to_i

  elsif /^(\d+)(#{units.join('|')})$/i =~ v
    n = ::Regexp.last_match(1).to_i
    i = units.index(::Regexp.last_match(2).downcase)

    n * (2 << (9 + (10 * i)))

  else
    v
  end
end