Module: OsCtl::Lib::Utils::Humanize
- Included in:
- CGroup::PathReader::Base
- Defined in:
- lib/libosctl/utils/humanize.rb
Instance Method Summary collapse
- #break_interval(interval) ⇒ Object protected
- #format_long_duration(interval) ⇒ Object
- #format_percent(v) ⇒ Object
- #format_short_duration(interval) ⇒ Object
- #humanize_data(v) ⇒ Object
- #humanize_number(v) ⇒ Object
- #humanize_percent(v) ⇒ Object
- #humanize_time_ns(v) ⇒ Object
- #humanize_time_us(v) ⇒ Object
- #parse_data(v) ⇒ Object
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 / 86400 h = interval / 3600 % 24 m = interval / 60 % 60 s = interval % 60 [d, h, m, s] end |
#format_long_duration(interval) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/libosctl/utils/humanize.rb', line 65 def format_long_duration(interval) d, h, m, s = break_interval(interval) if d > 0 "%d days, %02d:%02d:%02d" % [d, h, m, s] else "%02d:%02d:%02d" % [h, m, s] end end |
#format_percent(v) ⇒ Object
92 93 94 |
# File 'lib/libosctl/utils/humanize.rb', line 92 def format_percent(v) v.round(1) end |
#format_short_duration(interval) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/libosctl/utils/humanize.rb', line 75 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 '%02d:%02d' % [m, s] elsif d == 0 '%02d:%02d:%02d' % [h, m, s] else '%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 30 |
# 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 if division >= 1000 return "#{division.round}#{u}" else return "#{division.round(1)}#{u}" end 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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/libosctl/utils/humanize.rb', line 32 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
96 97 98 |
# File 'lib/libosctl/utils/humanize.rb', line 96 def humanize_percent(v) "#{format_percent(v)}%" end |
#humanize_time_ns(v) ⇒ Object
61 62 63 |
# File 'lib/libosctl/utils/humanize.rb', line 61 def humanize_time_ns(v) format_short_duration(v / 1_000_000_000) end |
#humanize_time_us(v) ⇒ Object
57 58 59 |
# File 'lib/libosctl/utils/humanize.rb', line 57 def humanize_time_us(v) format_short_duration(v / 1_000_000) end |
#parse_data(v) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/libosctl/utils/humanize.rb', line 100 def parse_data(v) units = %w(k m g t) if /^\d+$/ =~ v v.to_i elsif /^(\d+)(#{units.join('|')})$/i =~ v n = $1.to_i i = units.index($2.downcase) n * (2 << (9 + (10*i))) else v end end |