From f9f9bb40258bf4b27e02053e83f55bdab3fc0f8f Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sun, 19 Mar 2023 11:34:38 +0100 Subject: [PATCH] Allow arbitrary number of stops --- dashboard.py | 43 +++++++++++------------------- tests/test_healthscore.py | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 tests/test_healthscore.py diff --git a/dashboard.py b/dashboard.py index 840be2d..6cb6631 100644 --- a/dashboard.py +++ b/dashboard.py @@ -87,34 +87,21 @@ class Widget: if value == None: value = self.lastvalue log.debug("stops = %s", self.stops) - brightness = 30 - if self.stops[0] < self.stops[2]: - if value < self.stops[0]: - log.debug("definitely ok") - return 100 - elif value < self.stops[1]: - log.debug("mostly ok") - return 100 - (value - self.stops[0]) / (self.stops[1] - self.stops[0]) * 50 - elif value < self.stops[2]: - log.debug("maybe fail") - return 50 - (value - self.stops[1]) / (self.stops[2] - self.stops[1]) * 50 - else: - log.debug("definitely fail") - return 0 - else: - log.debug("the other side") - if value > self.stops[0]: - log.debug("definitely ok") - return 100 - elif value > self.stops[1]: - log.debug("mostly ok") - return 100 - (value - self.stops[0]) / (self.stops[1] - self.stops[0]) * 50 - elif value > self.stops[2]: - log.debug("maybe fail") - return 50 - (value - self.stops[1]) / (self.stops[2] - self.stops[1]) * 50 - else: - log.debug("definitely fail") - return 0 + stops = self.stops + if stops[-1] < stops[0]: + value = -value + stops = [-v for v in stops] + + if value <= stops[0]: + log.debug("ok") + return 100 + if value >= stops[-1]: + log.debug("fail") + return 0 + for i in range(0, len(stops) - 1): + if stops[i] <= value < stops[i+1]: + log.debug("at stop %d", i) + return 100 - ((value - stops[i]) / (stops[i+1] - stops[i]) + i) * 100 / (len(stops) - 1) def criticalcolor(self, value=None): healthscore = self.healthscore(value) diff --git a/tests/test_healthscore.py b/tests/test_healthscore.py new file mode 100644 index 0000000..d9591a3 --- /dev/null +++ b/tests/test_healthscore.py @@ -0,0 +1,56 @@ +import pytest + +from dashboard import Widget + +def test_healthscore_1_asc(): + w = Widget( + { + "type": "gauge", + "stops": [1, 5], + "data": [ "605da6f41f58b122f41283823a99faa36286961a106ac901bb2b2d730fddc778" ] # required by API, not used for tests + } + ) + # stops are ordered from best to worst + assert w.healthscore(0) == 100 + assert w.healthscore(1) == 100 + assert w.healthscore(2) == 75 + assert w.healthscore(3) == 50 + assert w.healthscore(4) == 25 + assert w.healthscore(5) == 0 + assert w.healthscore(6) == 0 + + +def test_healthscore_2_asc(): + w = Widget( + { + "type": "gauge", + "stops": [1, 10, 100], + "data": [ "605da6f41f58b122f41283823a99faa36286961a106ac901bb2b2d730fddc778" ] # required by API, not used for tests + } + ) + # stops are ordered from best to worst + assert w.healthscore(0) == 100 + assert w.healthscore(1) == 100 + assert w.healthscore(5.5) == 75 + assert w.healthscore(10) == 50 + assert w.healthscore(55) == 25 + assert w.healthscore(100) == 0 + assert w.healthscore(1000) == 0 + +def test_healthscore_2_desc(): + w = Widget( + { + "type": "gauge", + "stops": [100, 10, 1], + "data": [ "605da6f41f58b122f41283823a99faa36286961a106ac901bb2b2d730fddc778" ] # required by API, not used for tests + } + ) + # stops are ordered from best to worst + assert w.healthscore(0) == 0 + assert w.healthscore(1) == 0 + assert w.healthscore(5.5) == 25 + assert w.healthscore(10) == 50 + assert w.healthscore(55) == 75 + assert w.healthscore(100) == 100 + assert w.healthscore(1000) == 100 +