Module: check_mk
Branch: master
Commit: 190d9109e9b8c55d013fcf38e87dfbba06a20765
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=190d9109e9b8c5…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Mar 4 12:00:34 2016 +0100
Introduced support for the unified vertical axis scaled unit
---
web/htdocs/lib.py | 38 +++++++++++++++++++++++++++++---------
web/plugins/metrics/check_mk.py | 24 ++++++++++++++++++++----
2 files changed, 49 insertions(+), 13 deletions(-)
diff --git a/web/htdocs/lib.py b/web/htdocs/lib.py
index 3725f19..b99acab 100644
--- a/web/htdocs/lib.py
+++ b/web/htdocs/lib.py
@@ -529,11 +529,36 @@ def frexp10(x):
# Note if the type of v is integer, then the precision cut
# down to the precision of the actual number
def physical_precision(v, precision, unit_symbol):
- if v == 0:
- return "%%.%df %%s" % (precision - 1) % (v, unit_symbol)
- elif v < 0:
+ if v < 0:
return "-" + physical_precision(-v, precision, unit_symbol)
+ scale_symbol, places_after_comma, scale_factor = calculate_physical_precision(v,
precision)
+
+ scaled_value = float(v) / scale_factor
+ return (u"%%.%df %%s%%s" % places_after_comma) % (scaled_value,
scale_symbol, unit_symbol)
+
+
+def physical_precision_list(values, precision, unit_symbol):
+ if not values:
+ reference = 0
+ else:
+ reference = min([ abs(v) for v in values ])
+
+ scale_symbol, places_after_comma, scale_factor =
calculate_physical_precision(reference, precision)
+
+ units = []
+ scaled_values = []
+ for value in values:
+ scaled_value = float(value) / scale_factor
+ scaled_values.append(("%%.%df" % places_after_comma) % scaled_value)
+
+ return "%s%s" % (scale_symbol, unit_symbol), scaled_values
+
+
+def calculate_physical_precision(v, precision):
+ if v == 0:
+ return "", precision - 1, 1
+
# Splitup in mantissa (digits) an exponent to the power of 10
# -> a: (2.23399998, -2) b: (4.5, 6) c: (1.3756, 2)
mantissa, exponent = frexp10(float(v))
@@ -541,10 +566,6 @@ def physical_precision(v, precision, unit_symbol):
if type(v) == int:
precision = min(precision, exponent + 1)
- # Round the mantissa to the required number of digits
- # -> a: 2.23 b: 4.5 c: 1.38
- mant_rounded = round(mantissa, precision-1) * 10**exponent
-
# Choose a power where no artifical zero (due to rounding) needs to be
# placed left of the decimal point.
scale_symbols = {
@@ -575,9 +596,8 @@ def physical_precision(v, precision, unit_symbol):
places_before_comma = exponent + 1
places_after_comma = precision - places_before_comma
- value = mantissa * 10**exponent
+ return scale_symbols[scale], places_after_comma, 1000**abs(scale)
- return u"%%.%df %%s%%s" % places_after_comma % (value,
scale_symbols[scale], unit_symbol)
def nic_speed_human_readable(bits_per_second):
if bits_per_second == 10000000:
diff --git a/web/plugins/metrics/check_mk.py b/web/plugins/metrics/check_mk.py
index cf60440..f9aae56 100644
--- a/web/plugins/metrics/check_mk.py
+++ b/web/plugins/metrics/check_mk.py
@@ -42,6 +42,22 @@
# +----------------------------------------------------------------------+
# | Definition of units of measurement. |
# '----------------------------------------------------------------------'
+# Optional attributes of units:
+#
+# stepping: FIXME: Describe this
+#
+# graph_unit: Compute a common unit for the whole graph. This is an optional
+# feature to solve the problem that some unit names are too long
+# to be shown on the left of the screen together with the values.
+# For fixing this the "graph unit" is available which is
displayed
+# on the top left of the graph and is used for the whole graph. So
+# once a "graph unit" is computed, it does not need to be shown
+# beside each label.
+# This has to be set to a function which recevies a list of values,
+# then computes the optimal unit for the given values and then
+# returns a two element tuple. The first element is the "graph
unit"
+# and the second is a list containing all of the values rendered with
+# the graph unit.
# TODO: Move fundamental units like "" to main file.
@@ -67,7 +83,6 @@ unit_info["%"] = {
"render" : lambda v: percent_human_redable(v, 3),
}
-
unit_info["s"] = {
"title" : _("sec"),
"description" : _("Timespan or Duration in seconds"),
@@ -106,9 +121,10 @@ unit_info["bytes/s"] = {
}
unit_info["bits/s"] = {
- "title" : _("Bits per second"),
- "symbol" : _("bits/s"),
- "render" : lambda v: physical_precision(v, 3, _("bit/s")),
+ "title" : _("Bits per second"),
+ "symbol" : _("bits/s"),
+ "render" : lambda v: physical_precision(v, 3, _("bit/s")),
+ "graph_unit" : lambda v: physical_precision_list(v, 3,
_("bit/s")),
}
# Output in bytes/days, value is in bytes/s