From e921031e647aa46748af6175cdcf65bbeca007d1 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sat, 24 Aug 2024 22:49:43 +0200 Subject: [PATCH] Format time velues in graph descriptions --- dashboard.py | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/dashboard.py b/dashboard.py index 8e4c2d4..0f83a24 100644 --- a/dashboard.py +++ b/dashboard.py @@ -391,7 +391,10 @@ class TimeSeries(Widget): if self.yscale == "log": try: min_value = min(d[1] for d in self.lts.data if d[1] > 0) - self.extra["min"] = "%g" % min_value + if unit == "s": + self.extra["min"] = "%g" % min_value + " (" + self.format_time(min_value) + ")" + else: + self.extra["min"] = "%g" % min_value except ValueError: # no non-negative values min_value = max_value / 2 @@ -405,8 +408,12 @@ class TimeSeries(Widget): # Make sure min_value is less than max_value min_value /= 2 log.debug("min_value = %s, max_value = %s", min_value, max_value) - self.extra["max"] = "%g" % max_value - self.extra["last"] = "%g" % data[-1][1] + if unit == "s": + self.extra["max"] = "%g" % max_value + " (" + self.format_time(max_value) + ")" + self.extra["last"] = "%g" % data[-1][1] + " (" + self.format_time(data[-1][1]) + ")" + else: + self.extra["max"] = "%g" % max_value + self.extra["last"] = "%g" % data[-1][1] log.debug("collecting data") v_data = [] for i in range(n): @@ -460,6 +467,28 @@ class TimeSeries(Widget): log.debug("in as_html") return Markup(render_template("timeseries.html", widget=self)) + def format_time(seconds): + unit = "s" + if value >= 365.25 * 86400: + value /= 365.25 * 86400 + unit = "years" + elif value >= 86400: + value /= 86400 + unit = "days" + elif value >= 3600: + value /= 3600 + unit = "h" + elif value >= 60: + value /= 60 + unit = "m" + elif value >= 1: + pass + elif value >= 0.001: + value *= 1000 + unit = "ms" + return f"{value:.2f} {unit}" + + class Gauge(Widget): def __init__(self, d): super().__init__(d) @@ -501,3 +530,4 @@ class Gauge(Widget): self.lastvalue_formatted = Markup(f"{value:.2f}{unit}") return Markup(render_template("gauge.html", widget=self)) +# vim: sw=4