Compare commits

...

3 Commits

3 changed files with 71 additions and 39 deletions

View File

@ -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)

56
tests/test_healthscore.py Normal file
View File

@ -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

View File

@ -3,17 +3,6 @@ import pytest
from dashboard import TimeSeries
def test_timeseries_1():
data = {"description": {"fstype": "tmpfs", "hostname": "ehoiai.int.wsr.at", "measure": "time_until_disk_full", "mountpoint": "/run/user/1054", "node": "rorschach.hjp.at", "remote_addr": "", "unit": "s"},
"data": [
[1674806402.1014915, 1000000000.0],
[1674814201.3452954, 1000000000.0],
[1675164602.4004557, 1000000000.0],
[1675165201.6291275, 1000000000.0],
[1675165802.2402072, 1000000000.0],
[1675166401.542857, 1000000000.0],
[1675167002.2809808, 1000000000.0],
[1675167601.2632012, 1000000000.0],
[1675168201.8321788, 1000000000.0]]}
config = {
"type": "timeseries",
"multi": True,