Module: check_mk
Branch: master
Commit: b1ac75b0a39f3b57cf22d4d74e7d8d4f454ec352
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=b1ac75b0a39f3b…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Thu Mar 12 16:15:40 2015 +0100
Better decision of how many digits to output in get_bytes_human_readable
---
web/htdocs/lib.py | 42 +++++++++++++++++++++++++++++++-----------
1 file changed, 31 insertions(+), 11 deletions(-)
diff --git a/web/htdocs/lib.py b/web/htdocs/lib.py
index e77db8c..7e0ffe5 100644
--- a/web/htdocs/lib.py
+++ b/web/htdocs/lib.py
@@ -435,6 +435,7 @@ def num_split(s):
first_word = regex("[0-9]").split(s)[0]
return ( first_word.lower(), ) + num_split(s[len(first_word):])
+
def number_human_readable(n, precision=1, unit="B"):
base = 1024.0
if unit == "Bit":
@@ -451,6 +452,7 @@ def number_human_readable(n, precision=1, unit="B"):
else:
return (f + "%s") % (n, unit)
+
def age_human_readable(secs, min_only=False):
if secs < 0:
return "- " + age_human_readable(-secs, min_only)
@@ -476,6 +478,7 @@ def age_human_readable(secs, min_only=False):
days = hours / 24
return "%d %s" % (days, _("days"))
+
def bytes_human_readable(b, base=1024.0, bytefrac=True, unit="B"):
base = float(base)
# Handle negative bytes correctly
@@ -484,19 +487,36 @@ def bytes_human_readable(b, base=1024.0, bytefrac=True,
unit="B"):
prefix = '-'
b *= -1
- if b >= base * base * base * base:
- return '%s%.2f T%s' % (prefix, b / base / base / base / base, unit)
- elif b >= base * base * base:
- return '%s%.2f G%s' % (prefix, b / base / base / base, unit)
- elif b >= base * base:
- return '%s%.2f M%s' % (prefix, b / base / base, unit)
+ digits = 1
+ if b >= base ** 4:
+ symbol = "T"
+ b /= base ** 4
+
+ elif b >= base ** 3:
+ symbol = "G"
+ b /= base ** 3
+
+ elif b >= base ** 2:
+ symbol = "M"
+ b /= base ** 2
+
elif b >= base:
- return '%s%.2f k%s' % (prefix, b / base, unit)
- elif bytefrac:
- return '%s%.2f %s' % (prefix, b, unit)
- else: # Omit byte fractions
- return '%s%.0f %s' % (prefix, b, unit)
+ symbol = "k"
+ b /= base
+
+ else:
+ symbol = ""
+
+ if not bytefrac:
+ digits = 0
+ elif b >= 100:
+ digits = 0
+ elif b >= 10:
+ digits = 1
+ else:
+ digits = 2
+ return "%%s%%.%df %%s%%s" % digits % (prefix, b, symbol, unit)