Module: check_mk
Branch: master
Commit: 06890782873b05c64b16db2be314f3e11d22fde7
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=06890782873b05…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Tue Mar 24 17:37:28 2015 +0100
metrics: better output of percentages
---
web/htdocs/lib.py | 16 ++++++++++++----
web/plugins/metrics/check_mk.py | 4 ++--
2 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/web/htdocs/lib.py b/web/htdocs/lib.py
index df64ec7..9e6616a 100644
--- a/web/htdocs/lib.py
+++ b/web/htdocs/lib.py
@@ -481,11 +481,8 @@ def num_split(s):
# exp -= 1
# return mantissa, exp
-def logb(x, base):
- return math.log(x) / math.log(base)
-
def frexpb(x, base):
- exp = int(logb(x, base))
+ exp = int(math.log(x, base))
mantissa = x / base**exp
if mantissa < 1:
mantissa *= base
@@ -578,6 +575,17 @@ def number_human_readable(n, precision=1, unit="B"):
return (f + "%s") % (n, unit)
+def percent_human_redable(perc, precision=2, drop_zeroes=True):
+ if perc > 0:
+ perc_precision = max(1, 2 - int(round(math.log(perc, 10))))
+ else:
+ perc_precision = 1
+ text = "%%.%df" % perc_precision % perc
+ if drop_zeroes:
+ text = text.rstrip("0").rstrip(".")
+ return text + "%"
+
+
def age_human_readable(secs, min_only=False):
if secs < 0:
return "- " + age_human_readable(-secs, min_only)
diff --git a/web/plugins/metrics/check_mk.py b/web/plugins/metrics/check_mk.py
index 217fd96..ca746ca 100644
--- a/web/plugins/metrics/check_mk.py
+++ b/web/plugins/metrics/check_mk.py
@@ -55,7 +55,7 @@ unit_info["count"] = {
unit_info["%"] = {
"title" : _("%"),
"symbol" : _("%"),
- "render" : lambda v: "%s%%" % drop_dotzero(v),
+ "render" : lambda v: percent_human_redable(v, 3),
}
# Similar as %, but value ranges from 0.0 ... 1.0
@@ -63,7 +63,7 @@ unit_info["ratio"] = {
"title" : _("%"),
"symbol" : _("%"),
"render_scale" : 100.0, # Scale by this before rendering if
"render" not being used
- "render" : lambda v: "%s%%" % drop_dotzero(100.0 * v),
+ "render" : lambda v: percent_human_redable(v, 3),
}
unit_info["s"] = {