Module: check_mk
Branch: master
Commit: d33e465541e969f06585be70446a70862a6dabd4
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=d33e465541e969…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Feb 16 17:09:50 2018 +0100
Moved percent_human_redable() to cmk.render.percent()
Change-Id: I1be8ab644065b19e93e61a680d429fcd0b383495
---
cmk/render.py | 45 +++++++++++++++++++++++++++++++++++++++++
web/htdocs/lib.py | 11 ----------
web/plugins/metrics/check_mk.py | 4 ++--
web/plugins/views/painters.py | 3 ++-
4 files changed, 49 insertions(+), 14 deletions(-)
diff --git a/cmk/render.py b/cmk/render.py
index b470398..f17a0a6 100644
--- a/cmk/render.py
+++ b/cmk/render.py
@@ -32,6 +32,7 @@ are just for optical output purposes."""
# THIS IS STILL EXPERIMENTAL
import time
+import math
# TODO: Clean this up one day by using the way recommended by gettext.
# (See
https://docs.python.org/2/library/gettext.html). For this we
@@ -41,6 +42,15 @@ try:
except NameError:
_ = lambda x: x # Fake i18n when not available
+#.
+# .--Date/Time-----------------------------------------------------------.
+# | ____ _ _______ _ |
+# | | _ \ __ _| |_ ___ / /_ _(_)_ __ ___ ___ |
+# | | | | |/ _` | __/ _ \ / / | | | | '_ ` _ \ / _ \ |
+# | | |_| | (_| | || __// / | | | | | | | | | __/ |
+# | |____/ \__,_|\__\___/_/ |_| |_|_| |_| |_|\___| |
+# | |
+# '----------------------------------------------------------------------'
def date(timestamp):
return time.strftime(_("%Y-%m-%d"), time.localtime(timestamp))
@@ -88,6 +98,15 @@ class Age(object):
def __float__(self):
return float(self.__secs)
+#.
+# .--Bits/Bytes----------------------------------------------------------.
+# | ____ _ _ ______ _ |
+# | | __ )(_) |_ ___ / / __ ) _ _| |_ ___ ___ |
+# | | _ \| | __/ __| / /| _ \| | | | __/ _ \/ __| |
+# | | |_) | | |_\__ \/ / | |_) | |_| | || __/\__ \ |
+# | |____/|_|\__|___/_/ |____/ \__, |\__\___||___/ |
+# | |___/ |
+# '----------------------------------------------------------------------'
def bytes(b, base=1024.0, bytefrac=True, unit="B"):
"""Formats byte values to be used in texts for humans.
@@ -130,3 +149,29 @@ def filesize(size):
return str(size)[:-6] + dec_sep + str(size)[-6:-3] + dec_sep + str(size)[-3:]
else:
return str(size)[:-9] + dec_sep + str(size)[-9:-6] + dec_sep + str(size)[-6:-3] +
dec_sep + str(size)[-3:]
+
+
+#.
+# .--Misc.Numbers--------------------------------------------------------.
+# | __ __ _ _ _ _ |
+# | | \/ (_)___ ___ | \ | |_ _ _ __ ___ | |__ ___ _ __ ___ |
+# | | |\/| | / __|/ __| | \| | | | | '_ ` _ \| '_ \ / _ \ '__/ __| |
+# | | | | | \__ \ (__ _| |\ | |_| | | | | | | |_) | __/ | \__ \ |
+# | |_| |_|_|___/\___(_)_| \_|\__,_|_| |_| |_|_.__/ \___|_| |___/ |
+# | |
+# '----------------------------------------------------------------------'
+
+def percent(perc, precision=2, drop_zeroes=True):
+ """Renders a given number as string"""
+ 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 + "%"
+
diff --git a/web/htdocs/lib.py b/web/htdocs/lib.py
index 438ae65..e8620c5 100644
--- a/web/htdocs/lib.py
+++ b/web/htdocs/lib.py
@@ -384,17 +384,6 @@ 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 2725392..e3de931 100644
--- a/web/plugins/metrics/check_mk.py
+++ b/web/plugins/metrics/check_mk.py
@@ -126,7 +126,7 @@ unit_info["%"] = {
"title" : _("%"),
"description" : _("Percentage (0...100)"),
"symbol" : _("%"),
- "render" : lambda v: percent_human_redable(v, 3),
+ "render" : lambda v: cmk.render.percent(v, precision=3),
}
unit_info["s"] = {
@@ -279,7 +279,7 @@ unit_info["ppm"] = {
unit_info["%/m"] = {
"title" : _("Percent Per Meter"),
"symbol" : _("%/m"),
- "render" : lambda v: percent_human_redable(v, 3) + _("/m"),
+ "render" : lambda v: cmk.render.percent(v, precision=3) +
_("/m"),
}
unit_info["bar"] = {
diff --git a/web/plugins/views/painters.py b/web/plugins/views/painters.py
index 0710d41..80c5c69 100644
--- a/web/plugins/views/painters.py
+++ b/web/plugins/views/painters.py
@@ -70,6 +70,7 @@ import bi # Needed for BI Icon. For arkane reasons (ask
htdocs/module.py) this
import config
import cmk.paths
+import cmk.render
import cmk.man_pages as man_pages
from cmk.regex import regex
from cmk.defines import short_service_state_name, short_host_state_name
@@ -820,7 +821,7 @@ def render_cache_info(what, row):
if cache_interval:
percentage = 100.0 * cache_age / cache_interval
- text += _(", elapsed cache lifespan: %s") %
percent_human_redable(percentage)
+ text += _(", elapsed cache lifespan: %s") %
cmk.render.percent(percentage)
return text