diff --git a/dashboard.py b/dashboard.py index 6996eb6..7624584 100644 --- a/dashboard.py +++ b/dashboard.py @@ -56,6 +56,10 @@ class Dashboard: else: 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): return render_template("dashboard.html", dashboard=self) @@ -70,14 +74,16 @@ class Widget: self.extra = {} log.debug("data = %s", d["data"]) 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): log.debug("") - self.lastvalue = self.lts.data[-1][1] 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: value = self.lastvalue log.debug("stops = %s", self.stops) @@ -85,50 +91,36 @@ class Widget: if self.stops[0] < self.stops[2]: if value < self.stops[0]: log.debug("definitely ok") - return f"hsl(120, 100%, {brightness}%)" + return 100 elif value < self.stops[1]: log.debug("mostly ok") - hue = 120 - round( - (value - self.stops[0]) - / (self.stops[1] - self.stops[0]) - * 60 - ) - return f"hsl({hue}, 100%, {brightness}%)" + return 100 - (value - self.stops[0]) / (self.stops[1] - self.stops[0]) * 50 elif value < self.stops[2]: log.debug("maybe fail") - hue = 60 - round( - (value - self.stops[1]) - / (self.stops[2] - self.stops[1]) - * 60 - ) - return f"hsl({hue}, 100%, {brightness}%)" + return 50 - (value - self.stops[1]) / (self.stops[2] - self.stops[1]) * 50 else: log.debug("definitely fail") - return f"hsl(0, 100%, {brightness}%)" + return 0 else: log.debug("the other side") if value > self.stops[0]: log.debug("definitely ok") - return f"hsl(120, 100%, {brightness}%)" + return 100 elif value > self.stops[1]: log.debug("mostly ok") - hue = 120 - round( - (value - self.stops[0]) - / (self.stops[1] - self.stops[0]) - * 60 - ) - return f"hsl({hue}, 100%, {brightness}%)" + return 100 - (value - self.stops[0]) / (self.stops[1] - self.stops[0]) * 50 elif value > self.stops[2]: log.debug("maybe fail") - hue = 60 - round( - (value - self.stops[1]) - / (self.stops[2] - self.stops[1]) - * 60 - ) - return f"hsl({hue}, 100%, {brightness}%)" + return 50 - (value - self.stops[1]) / (self.stops[2] - self.stops[1]) * 50 else: 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 def description_formatted(self):