Implement log scale for y axis
This commit is contained in:
parent
d6bdaa4128
commit
fedc4c66ac
117
dashboard.py
117
dashboard.py
|
@ -63,8 +63,10 @@ class Dashboard:
|
||||||
|
|
||||||
class Widget:
|
class Widget:
|
||||||
def __init__(self, d):
|
def __init__(self, d):
|
||||||
|
log.debug("")
|
||||||
self.type = d["type"]
|
self.type = d["type"]
|
||||||
self.stops = d["stops"]
|
self.stops = d["stops"]
|
||||||
|
self.yscale = d.get("yscale", "linear")
|
||||||
log.debug("data = %s", d["data"])
|
log.debug("data = %s", d["data"])
|
||||||
self.lts = LTS(id=d["data"][0]) # by default we handle only one data source
|
self.lts = LTS(id=d["data"][0]) # by default we handle only one data source
|
||||||
pass
|
pass
|
||||||
|
@ -140,6 +142,7 @@ class Widget:
|
||||||
|
|
||||||
class TimeSeries(Widget):
|
class TimeSeries(Widget):
|
||||||
def __init__(self, d):
|
def __init__(self, d):
|
||||||
|
log.debug("")
|
||||||
super().__init__(d)
|
super().__init__(d)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -149,31 +152,18 @@ class TimeSeries(Widget):
|
||||||
x = n - math.log((t_last - t) / (n*dt) + 1) * n / k
|
x = n - math.log((t_last - t) / (n*dt) + 1) * n / k
|
||||||
return x
|
return x
|
||||||
|
|
||||||
data = self.lts.data
|
def v2y(v):
|
||||||
n = len(data)
|
if self.yscale == "log":
|
||||||
t_last = data[-1][0]
|
return (1 - math.log(v / min_value)
|
||||||
if len(data) < 5:
|
/ math.log(max_value / min_value)
|
||||||
return "(not enough data)"
|
) * 200
|
||||||
dt = (t_last - data[-5][0]) / 4
|
elif self.yscale == "linear":
|
||||||
k = math.log((t_last - data[0][0]) / dt / n + 1)
|
return (1 - v/max_value) * 200,
|
||||||
|
else:
|
||||||
max_value = max([d[1] for d in self.lts.data])
|
raise ValueError(f"Unknown yscale {self.yscale}")
|
||||||
max_value = max(max_value, 0.001) # ensure positive
|
|
||||||
v_data = []
|
|
||||||
for i in range(n):
|
|
||||||
t = data[i][0]
|
|
||||||
x = t2x(t)
|
|
||||||
t_h = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(t))
|
|
||||||
#print(t, t_h, x)
|
|
||||||
v_data.append(
|
|
||||||
{
|
|
||||||
"t": t,
|
|
||||||
"v": data[i][1],
|
|
||||||
"x": x,
|
|
||||||
"y": (1 - data[i][1]/max_value) * 200,
|
|
||||||
"color": self.criticalcolor(data[i][1]),
|
|
||||||
})
|
|
||||||
|
|
||||||
|
def set_x_tickmarks():
|
||||||
|
log.debug("")
|
||||||
tickmarks = []
|
tickmarks = []
|
||||||
t = v_data[-1]["t"]
|
t = v_data[-1]["t"]
|
||||||
x = v_data[-1]["x"]
|
x = v_data[-1]["x"]
|
||||||
|
@ -310,14 +300,89 @@ class TimeSeries(Widget):
|
||||||
d = datetime.datetime.fromtimestamp(t)
|
d = datetime.datetime.fromtimestamp(t)
|
||||||
tickmarks.append({"t": t, "t_h": d.strftime("%Y-%m-%d %H:%M:%S"), "x": x})
|
tickmarks.append({"t": t, "t_h": d.strftime("%Y-%m-%d %H:%M:%S"), "x": x})
|
||||||
self.x_tickmarks = tickmarks
|
self.x_tickmarks = tickmarks
|
||||||
|
log.debug("")
|
||||||
|
|
||||||
|
def set_y_tickmarks():
|
||||||
|
log.debug("")
|
||||||
self.y_tickmarks = []
|
self.y_tickmarks = []
|
||||||
|
if self.yscale == "linear":
|
||||||
|
log.debug("")
|
||||||
step = 10 ** math.floor(math.log10(max_value))
|
step = 10 ** math.floor(math.log10(max_value))
|
||||||
v = 0
|
v = 0
|
||||||
while v < max_value:
|
while v < max_value:
|
||||||
self.y_tickmarks.append({"y": (1 - v/max_value) * 200, "v_h": str(v)})
|
self.y_tickmarks.append({"y": v2y(v), "v_h": str(v)})
|
||||||
v += step
|
v += step
|
||||||
|
log.debug("")
|
||||||
|
elif self.yscale == "log":
|
||||||
|
log.debug("")
|
||||||
|
v = 10 ** math.ceil(math.log10(min_value))
|
||||||
|
log.debug("v = %s", v)
|
||||||
|
if v > max_value:
|
||||||
|
# Implement that when it happens
|
||||||
|
log.warning("No tickmark between %s and %s", min_value, max_value)
|
||||||
|
return
|
||||||
|
while v <= max_value:
|
||||||
|
y = v2y(v)
|
||||||
|
log.debug("v = %s, y = %s", v, y)
|
||||||
|
self.y_tickmarks.append({"y": y, "v_h": str(v)})
|
||||||
|
v *= 10
|
||||||
|
log.debug("")
|
||||||
|
else:
|
||||||
|
log.debug("")
|
||||||
|
raise ValueError(f"Unknown yscale {self.yscale}")
|
||||||
|
log.debug("")
|
||||||
|
|
||||||
|
|
||||||
|
log.debug("in graph")
|
||||||
|
data = self.lts.data
|
||||||
|
n = len(data)
|
||||||
|
t_last = data[-1][0]
|
||||||
|
if len(data) < 5:
|
||||||
|
return "(not enough data)"
|
||||||
|
dt = (t_last - data[-5][0]) / 4
|
||||||
|
k = math.log((t_last - data[0][0]) / dt / n + 1)
|
||||||
|
|
||||||
|
max_value = max([d[1] for d in self.lts.data])
|
||||||
|
max_value = max(max_value, 0.001) # ensure positive
|
||||||
|
if self.yscale == "log":
|
||||||
|
try:
|
||||||
|
min_value = min(d[1] for d in self.lts.data if d[1] > 0)
|
||||||
|
except ValueError:
|
||||||
|
# no non-negative values
|
||||||
|
min_value = max_value / 2
|
||||||
|
if any(True for d in self.lts.data if d[1] < min_value):
|
||||||
|
# if there are values smaller than the minimum (i.e.
|
||||||
|
# 0 or negative unless we have a configured minimum)
|
||||||
|
# reduce the minimum again so that we have a distinct
|
||||||
|
# value to clamp to.
|
||||||
|
min_value /= 2
|
||||||
|
if min_value == max_value:
|
||||||
|
# Make sure min_value is less than max_value
|
||||||
|
min_value /= 2
|
||||||
|
log.debug("min_value = %s, max_value = %s", min_value, max_value)
|
||||||
|
log.debug("collecting data")
|
||||||
|
v_data = []
|
||||||
|
for i in range(n):
|
||||||
|
t = data[i][0]
|
||||||
|
v = data[i][1]
|
||||||
|
x = t2x(t)
|
||||||
|
t_h = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(t))
|
||||||
|
y = v2y(v)
|
||||||
|
#print(t, t_h, x)
|
||||||
|
v_data.append(
|
||||||
|
{
|
||||||
|
"t": t,
|
||||||
|
"v": v,
|
||||||
|
"x": x,
|
||||||
|
"y": y,
|
||||||
|
"color": self.criticalcolor(v),
|
||||||
|
})
|
||||||
|
|
||||||
|
log.debug("setting tickmarks")
|
||||||
|
set_x_tickmarks()
|
||||||
|
set_y_tickmarks()
|
||||||
|
|
||||||
|
log.debug("assembling svg")
|
||||||
html = ""
|
html = ""
|
||||||
html += "<svg width=1150 height=300>"
|
html += "<svg width=1150 height=300>"
|
||||||
for tm in self.x_tickmarks:
|
for tm in self.x_tickmarks:
|
||||||
|
@ -329,9 +394,11 @@ class TimeSeries(Widget):
|
||||||
for v in v_data:
|
for v in v_data:
|
||||||
html += f"<circle cx={v['x']} cy={v['y']} r=3 fill='{v['color']}' />"
|
html += f"<circle cx={v['x']} cy={v['y']} r=3 fill='{v['color']}' />"
|
||||||
html += "</svg>"
|
html += "</svg>"
|
||||||
|
log.debug("len(html) = %s", len(html))
|
||||||
return Markup(html)
|
return Markup(html)
|
||||||
|
|
||||||
def as_html(self):
|
def as_html(self):
|
||||||
|
log.debug("in as_html")
|
||||||
return Markup(render_template("timeseries.html", widget=self))
|
return Markup(render_template("timeseries.html", widget=self))
|
||||||
|
|
||||||
class Gauge(Widget):
|
class Gauge(Widget):
|
||||||
|
|
Loading…
Reference in New Issue