Format time velues in graph descriptions

This commit is contained in:
Peter J. Holzer 2024-08-24 22:49:43 +02:00
parent 9a84e191be
commit e921031e64
1 changed files with 33 additions and 3 deletions

View File

@ -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"<span class='value'>{value:.2f}</span><span class='unit'>{unit}</unit>")
return Markup(render_template("gauge.html", widget=self))
# vim: sw=4