Sort by healthscore

This commit is contained in:
Peter J. Holzer 2023-01-10 21:34:44 +01:00 committed by Peter J. Holzer
parent 7ab45d3e90
commit 22c790080a
1 changed files with 23 additions and 31 deletions

View File

@ -56,6 +56,10 @@ class Dashboard:
else: else:
self.widgets.append(Widget(w)) self.widgets.append(Widget(w))
# Sort widgets by ascending healthscore to get the most critical at the
# top.
self.widgets.sort(key=lambda w: w.healthscore())
def as_html(self): def as_html(self):
return render_template("dashboard.html", dashboard=self) return render_template("dashboard.html", dashboard=self)
@ -70,14 +74,16 @@ class Widget:
self.extra = {} self.extra = {}
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 self.lastvalue = self.lts.data[-1][1]
def as_html(self): def as_html(self):
log.debug("") log.debug("")
self.lastvalue = self.lts.data[-1][1]
return Markup(render_template("widget.html", widget=self)) return Markup(render_template("widget.html", widget=self))
def criticalcolor(self, value=None): def healthscore(self, value=None):
"""
Return a score between 0 (unhealthy) and 100 (healthy)
"""
if value == None: if value == None:
value = self.lastvalue value = self.lastvalue
log.debug("stops = %s", self.stops) log.debug("stops = %s", self.stops)
@ -85,50 +91,36 @@ class Widget:
if self.stops[0] < self.stops[2]: if self.stops[0] < self.stops[2]:
if value < self.stops[0]: if value < self.stops[0]:
log.debug("definitely ok") log.debug("definitely ok")
return f"hsl(120, 100%, {brightness}%)" return 100
elif value < self.stops[1]: elif value < self.stops[1]:
log.debug("mostly ok") log.debug("mostly ok")
hue = 120 - round( return 100 - (value - self.stops[0]) / (self.stops[1] - self.stops[0]) * 50
(value - self.stops[0])
/ (self.stops[1] - self.stops[0])
* 60
)
return f"hsl({hue}, 100%, {brightness}%)"
elif value < self.stops[2]: elif value < self.stops[2]:
log.debug("maybe fail") log.debug("maybe fail")
hue = 60 - round( return 50 - (value - self.stops[1]) / (self.stops[2] - self.stops[1]) * 50
(value - self.stops[1])
/ (self.stops[2] - self.stops[1])
* 60
)
return f"hsl({hue}, 100%, {brightness}%)"
else: else:
log.debug("definitely fail") log.debug("definitely fail")
return f"hsl(0, 100%, {brightness}%)" return 0
else: else:
log.debug("the other side") log.debug("the other side")
if value > self.stops[0]: if value > self.stops[0]:
log.debug("definitely ok") log.debug("definitely ok")
return f"hsl(120, 100%, {brightness}%)" return 100
elif value > self.stops[1]: elif value > self.stops[1]:
log.debug("mostly ok") log.debug("mostly ok")
hue = 120 - round( return 100 - (value - self.stops[0]) / (self.stops[1] - self.stops[0]) * 50
(value - self.stops[0])
/ (self.stops[1] - self.stops[0])
* 60
)
return f"hsl({hue}, 100%, {brightness}%)"
elif value > self.stops[2]: elif value > self.stops[2]:
log.debug("maybe fail") log.debug("maybe fail")
hue = 60 - round( return 50 - (value - self.stops[1]) / (self.stops[2] - self.stops[1]) * 50
(value - self.stops[1])
/ (self.stops[2] - self.stops[1])
* 60
)
return f"hsl({hue}, 100%, {brightness}%)"
else: else:
log.debug("definitely fail") log.debug("definitely fail")
return f"hsl(0, 100%, {brightness}%)" return 0
def criticalcolor(self, value=None):
healthscore = self.healthscore(value)
hue = round(healthscore * 120 / 100)
brightness = 30
return f"hsl({hue}, 100%, {brightness}%)"
@property @property
def description_formatted(self): def description_formatted(self):