Allow arbitrary number of stops
This commit is contained in:
parent
f0e0ea2d73
commit
f9f9bb4025
39
dashboard.py
39
dashboard.py
|
@ -87,34 +87,21 @@ class Widget:
|
||||||
if value == None:
|
if value == None:
|
||||||
value = self.lastvalue
|
value = self.lastvalue
|
||||||
log.debug("stops = %s", self.stops)
|
log.debug("stops = %s", self.stops)
|
||||||
brightness = 30
|
stops = self.stops
|
||||||
if self.stops[0] < self.stops[2]:
|
if stops[-1] < stops[0]:
|
||||||
if value < self.stops[0]:
|
value = -value
|
||||||
log.debug("definitely ok")
|
stops = [-v for v in stops]
|
||||||
|
|
||||||
|
if value <= stops[0]:
|
||||||
|
log.debug("ok")
|
||||||
return 100
|
return 100
|
||||||
elif value < self.stops[1]:
|
if value >= stops[-1]:
|
||||||
log.debug("mostly ok")
|
log.debug("fail")
|
||||||
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
|
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):
|
def criticalcolor(self, value=None):
|
||||||
healthscore = self.healthscore(value)
|
healthscore = self.healthscore(value)
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue